HTML and CSS Reference
In-Depth Information
Figure 17.37 Subpatterns are used in string replacement.
17.5 Form Validation with Regular Expressions
When you fill out a form on the Web, you are typically asked for your name, phone
number, address (a popup menu of all the states is usually provided), and then all sorts
of credit card information. Sometimes it takes four or five tries to get it right because you
didn't complete the form exactly the way you were asked. A message will appear and you
won't be allowed to submit the form until you get it right. Behind the scenes a JavaScript
program is validating the form.
17.5.1 Checking for Empty Fields
There's a form waiting to be filled out. Some of the fields are optional, and some are man-
datory. The question is this: Did the user fill in the mandatory fields? If he or she didn't,
the form can't be processed properly. Checking for empty or null fields is one of the first
things you might want to do.
EXAMPLE 17.36
<html>
<head><title>Checking for Empty Fields</title>
<script type="text/javascript">
1
function validate_text(form1) {
2
if ( form1.user_name.value == "" ||
form1.user_name.value == null ){
alert("You must enter your name.");
return false;
}
3
if ( form1.user_phone.value == "" ||
form1.user_phone.value == null){
alert("You must enter your phone.");
return false;
}
Continues
 
 
 
Search WWH ::




Custom Search