Table of Contents
|
All Slides
|
Link List
|
Examples
|
CSCI E-12
<< previous
|
next >>
Validation onsubmit
Example 8.7
Example 8.7 Source:
<form action="http://minerva.dce.harvard.edu/cgi-bin/echo.cgi" onsubmit="return ValidateForm(this)" > <div> Email Address: <input type="text" size="32" name="email" id="email" /> <br/> <input type="submit" /> </div> </form>
In
example7.js
// function that calls separate validation functions function ValidateForm(thisform) { if(isNotEmpty(thisform.email)) { if (isEMailAddr(thisform.email)) { return true; } else { return false; } } else { return false; } } // validates that the field value string has one or more characters in it function isNotEmpty(elem) { var str = elem.value; var re = /.+/; if(!str.match(re)) { alert("Please fill in the required field."); return false; } else { return true; } } // validates that the entry is formatted as an email address function isEMailAddr(elem) { var str = elem.value; var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/; if (!str.match(re)) { alert("Verify the email address format."); return false; } else { return true; } }
In
head
element:
Example 8.7 Demonstrated
Table of Contents
|
All Slides
|
Link List
|
Examples
|
CSCI E-12
<< previous
|
next >>