HTML and CSS Reference
In-Depth Information
Figure 11.46 Using the onSubmit event handler to stop a form if the user didn't
enter anything in the field.
Checking for Alphabetic Characters. If checking input fields for alphabetic
characters, such as a user name, the following example will go through a loop evaluating
each character in a string to guarantee it is an alphabetic. See Chapter 17 for more on
this type of validation.
EXAMPLE 11.27
<html>
<head><title>Verifying a Name</title>
<script type="text/javascript">
1
function validate(form){
2
if(alpha(form.first) == false){
alert ("First name is invalid");
return false;
}
3
if(alpha(form.last) == false){
alert("Last name is invalid");
return false;
}
return true;
}
4
function alpha(textField ) {
5
if( textField.value.length != 0){
6
for (var i = 0; i < textField.value.length;i++){
7
var ch= textField.value.charAt(i);
/* alert(ch); Using alert to see what characters
are coming in
*/
8
if((ch < "A" || ch > "Z") && (ch< "a" || ch >"z")){
return false;
}
}
}
Continues
 
Search WWH ::




Custom Search