| <script> function emailcheck() { var string1=document.example.email.value if (string1.indexOf("@")==-1) .{ .alert("Please input a valid email address!") .document.example.email.focus() .} } </script> <form name="example"><input type="text" size="20" name="email" onblur="emailcheck()"> <strong>Feedback please:</strong> <textarea name="S1" rows="2" cols="20"></textarea> <input type="submit" name="B1" value="Submit"> </form> Ok, lets have a look at what's inside the <script> tags.
If you've been playing around with the onblur event handler, even with simply the above example, it can be very annoying at times; the better way is to simply defer the checking until a user presses the submit button, and check for errors all at once in one scoop. That's how the onsubmit handler comes in. The "onsubmit" handler is inserted inside the <form> tag, and not inside any one element, unlike "onblur." For example: <form name="george" onsubmit="what_ever"> Lets do an example: Enter Your name (*required) If you leave the required sections blank, as you click submit, you will be forced to come back and fill this box in. |
| <script> <!-- function validate() { if ((document.example2.naming.value=="")|| (document.example2.feed.value=="")) .{ .alert ("You must fill in all of the required .fields!") .return false .} } //--> </script> <form name="example2" onsubmit="return validate()"> <input type="text" size="20" name="naming"> <strong>Feedback please: (*required)</strong> <textarea name="feed" rows="3" cols="25"></textarea> <strong>Your home address (*NOT required)</strong> <input type="text" size="35" name="address"> <input type="submit" name="B1" value="Submit"> </form> Some every important things here:
Ok, in this section, we ran into the string object, so lets list all its properties and methods for quick glance:
What's next? Time is precious, so lets look at that... |
||||||||||||||||||||||||