Java Reference
In-Depth Information
<head>
<title>Chapter 6, Example 6</title>
</head>
<body>
<script src="ch6_example6.js"></script>
<script>
var phoneNumber = prompt("Please enter a phone number.", "");
if (isValidTelephoneNumber(phoneNumber)) {
alert("Valid Phone Number");
} else {
alert("Invalid Phone Number");
var postalCode = prompt("Please enter a postal code.", ""); 
if (isValidPostalCode(postalCode)) {
alert("Valid Postal Code");
} else {
alert("Invalid Postal Code");
}
 
var email = prompt("Please enter an email address.", "");
 
if (isValidEmail(email)) {
alert("Valid Email Address");
} else {
alert("Invalid Email Address");
}
</script>
</body>
</html>
Save this as ch6 _ example6.html and load it into your browser, and you'll be prompted to enter a phone
number. Enter a valid telephone number (for example, +1 (123) 123 4567), and you'll see a message that
states whether or not the phone number is valid.
You'll then be prompted to enter a postal code and an e‐mail. Enter those values to test those functions.
This is pretty basic, but it's sufficient for testing your code.
The actual code is very simple, but the regular expressions are tricky to create, so let's look at those
in depth starting with telephone number validation.
telephone number Validation
Telephone numbers are more of a challenge to validate. The problems are:
Phone numbers differ from country to country.
A valid number can be entered in different ways (for example, with or without the national
or international code).
 
Search WWH ::




Custom Search