Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 06, 2012 06:22 AM by Danny Gokey
Participant
967 Points
733 Posts
Mar 05, 2012 03:21 PM|LINK
Question at bottom of post.
html:-
<form id="form" name="myForm" method="post" action="" onsubmit="return(validate());">
javascript:-
function validate() { if(document.myForm.name.value.length == 0){ alert("Please enter Firstname"); document.myForm.name.focus(); return false; } var check = /^[A-Za-z ]{3,20}$/; if (!check.test(document.myForm.name.value)) { alert("Firstname cannot contain numbers"); document.myForm.name.focus(); return false; } } </script>
Question:-
Why do we have to say onsubmit="return(validate());" ?? (it works correctly)
if I say onsubmit="validate();" then the page refreshes.. whats the difference?
Star
9339 Points
1448 Posts
Mar 05, 2012 03:27 PM|LINK
onsubmit can be used to validate data in HTML forms before sending off the content to a server.
if validation fails, returing false from validation method must return to the page that stops it from posting.
hence, onsubmit="return(validate());" that returns false to page.
in case of onsubmit="validate();" even though validation fails, you return nothing to form to stop it from post.
http://www.w3schools.com/js/js_form_validation.asp
All-Star
20277 Points
3349 Posts
Mar 05, 2012 03:28 PM|LINK
Hi,
The return value is important there!
If you didn't return false, then it will consider that, the return value from the validate function is true!
NOTE: Default javascript function will return true.
Hence, if you didn't specify the return value - true/false, then it will consider true!
Then it will submit the page! If you wish to stop the submit behaviour then you have to return false incase of invalid form entries!
Hope it helps u...
Mar 05, 2012 05:33 PM|LINK
i think i missed out a line in the code:_ i have made it bold below.
Even after including this line do i require the onsubmit="return(validate());" ?? or is onsubmit="validate();" enough ??
function validate() { if(document.myForm.name.value.length == 0){ alert("Please enter Firstname"); document.myForm.name.focus(); return false; } var check = /^[A-Za-z ]{3,20}$/; if (!check.test(document.myForm.name.value)) { alert("Firstname cannot contain numbers"); document.myForm.name.focus(); return false; }
return true; } </script>
Mar 06, 2012 02:54 AM|LINK
you require onsubmit="return validate();".
Member
290 Points
56 Posts
Mar 06, 2012 06:22 AM|LINK
When you use the onsubmit="validate()", the validate() will retrun the ture or false , however, onsubmit attribute looks like a
mehod of a html, its value(the string) is the body of the method, and by default returns true, which equivalent to automatically
submit form and have already been validated
In summary, if form validation must be written like this: onsubmit= return function()
Hope it helps
ogsim07
Participant
967 Points
733 Posts
regarding calling javascript function from html form
Mar 05, 2012 03:21 PM|LINK
Question at bottom of post.
html:-
<form id="form" name="myForm" method="post" action="" onsubmit="return(validate());">
javascript:-
function validate() {
if(document.myForm.name.value.length == 0){
alert("Please enter Firstname");
document.myForm.name.focus();
return false;
}
var check = /^[A-Za-z ]{3,20}$/;
if (!check.test(document.myForm.name.value))
{
alert("Firstname cannot contain numbers");
document.myForm.name.focus();
return false;
}
}
</script>
Question:-
Why do we have to say onsubmit="return(validate());" ?? (it works correctly)
if I say onsubmit="validate();" then the page refreshes.. whats the difference?
me_ritz
Star
9339 Points
1448 Posts
Re: regarding calling javascript function from html form
Mar 05, 2012 03:27 PM|LINK
onsubmit can be used to validate data in HTML forms before sending off the content to a server.
if validation fails, returing false from validation method must return to the page that stops it from posting.
hence, onsubmit="return(validate());" that returns false to page.
in case of onsubmit="validate();" even though validation fails, you return nothing to form to stop it from post.
http://www.w3schools.com/js/js_form_validation.asp
roopeshreddy
All-Star
20277 Points
3349 Posts
Re: regarding calling javascript function from html form
Mar 05, 2012 03:28 PM|LINK
Hi,
The return value is important there!
If you didn't return false, then it will consider that, the return value from the validate function is true!
NOTE: Default javascript function will return true.
Hence, if you didn't specify the return value - true/false, then it will consider true!
Then it will submit the page! If you wish to stop the submit behaviour then you have to return false incase of invalid form entries!
Hope it helps u...
Roopesh Reddy C
Roopesh's Space
ogsim07
Participant
967 Points
733 Posts
Re: regarding calling javascript function from html form
Mar 05, 2012 05:33 PM|LINK
i think i missed out a line in the code:_ i have made it bold below.
Even after including this line do i require the onsubmit="return(validate());" ?? or is onsubmit="validate();" enough ??
javascript:-
function validate() {
if(document.myForm.name.value.length == 0){
alert("Please enter Firstname");
document.myForm.name.focus();
return false;
}
var check = /^[A-Za-z ]{3,20}$/;
if (!check.test(document.myForm.name.value))
{
alert("Firstname cannot contain numbers");
document.myForm.name.focus();
return false;
}
return true;
}
</script>
me_ritz
Star
9339 Points
1448 Posts
Re: regarding calling javascript function from html form
Mar 06, 2012 02:54 AM|LINK
you require onsubmit="return validate();".
Danny Gokey
Member
290 Points
56 Posts
Re: regarding calling javascript function from html form
Mar 06, 2012 06:22 AM|LINK
Hi,
When you use the onsubmit="validate()", the validate() will retrun the ture or false , however, onsubmit attribute looks like a
mehod of a html, its value(the string) is the body of the method, and by default returns true, which equivalent to automatically
submit form and have already been validated
In summary, if form validation must be written like this: onsubmit= return function()
Hope it helps