Download presentation
Presentation is loading. Please wait.
Published byLinda Robertson Modified over 9 years ago
1
FORM VALIDATION (Java Script )
2
Required Fields A required field in a form is a field that MUST have at least some content before the form will be processed.
3
Required Fields function validate() { mNv=mainform.Name.value; if (mNv=='') { alert('Your name is a required field. Please try again.'); event.returnValue=false; } if (!(mainform.Gender[0].checked || mainform.Gender[1].checked)) { alert(‘Gender is a required field. Please try again.'); event.returnValue=false; } } Please enter your name (required) Male Female This event handler ensures that when we submit the form, we get diverted to the internal Java Script function validate(), which in this case validates the form against any easy- to-remedy problems, namely empty fields.
4
Examines the Name field of the form mainform mNv=mainform.Name.value; if (mNv=='') { alert('Your name is a required field. Please try again.'); event.returnValue=false; } Setting the returnValue property of the event object to false rejects the form..
5
Checks that a Gender radio box has been selected if (!(mainform.Gender[0].checked || mainform.Gender[1].checked)) { alert(‘Gender is a required field. Please try again.'); event.returnValue=false; } Advanced Logic Techniques : The logic for the code works out that if either Male or Female is selected, then (mainform.Gender[0].checked || mainform.Gender[1].checked) is true. If neither is selected, then this expression is false ---- || (or) statement.
6
The if statement compiles if a condition is true, and so we must negate the entire thing. This gives the following logic table:
7
Required SELECT field Prevents the user from submitting the default option from a SELECT element. The default option must be a dummy option, which says something like 'Please choose'.
8
Required Select Field function validate() { if (mainform.Salary.options[0].selected) { alert('Please choose a Salary Range.'); event.returnValue=false; } } Salary Range Less Than $10,000 $10,000-$20,000 $20,000-$30,000 More than $30,000 If the code is true, then the viewer has not satisfactorily answered the question, and is prompted to try again.
9
Online Resource : http://www.webdevelopersjournal.com/article s/jscript_forms1.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.