1

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 :(

user3927463
  • 165
  • 1
  • 2
  • 13

3 Answers3

1

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();.

Iqbal Fauzi
  • 1,561
  • 11
  • 12
0

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.

Community
  • 1
  • 1
olive_tree
  • 1,417
  • 16
  • 23
0

You can as well use required for HTML5

<input type="text" required name="Cliname" id="textfield" maxlength = "10" />
Richie
  • 1,398
  • 1
  • 19
  • 36