Reputation: 165
here is html form :
<form id="form1" name="form1" action="toSQL.php" method="get" accept-charset="utf-8" onsubmit="return submitTest();">
<p><input type="text" name="Cliname" id="textfield" maxlength = "10" /></p>
<p><textarea name="message" id="message" maxlength = "20" ></textarea>
<input type="submit" value="submit" id="btn"/>
</form>
here is js:
<script type="text/javascript" >
function submitTest() {
if ( form1.textfield.value == "" ) {
alert( "enter your name!!" ) ;
form1.name.focus();
return false ;
}
if (form1.message.value == "" ) {
alert( "type the content!!" ) ;
form1.message.focus();
return false ;
}
}
</script>
alert is showed but it still send the form to sql. i have no idea :(
Upvotes: 1
Views: 2117
Reputation: 1439
You can as well use required
for HTML5
<input type="text" required name="Cliname" id="textfield" maxlength = "10" />
Upvotes: 0
Reputation: 1571
The form submitted because there's error on your code. Browser can't find form child with id name
on form1.name.focus();
. Change it to form1.textfield.focus();
.
Upvotes: 1
Reputation: 1457
In normal (non-jQuery) event handlers, return false does not stop the event from bubbling up.
Please see event.preventDefault() vs. return false for more details.
Upvotes: 0