Posts: 40
Name: Bob Davis
Location: Los Angeles, CA
|
I'm trying to validate a phone number in a form. Everything works perfectly except that when someone puts in an incorrect number type, it will not focus after giving them the warning message. Any help would be wonderful.
Thanks
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JS - Form Object - Validating a Phone Number</title>
<script type="text/javascript">
<!--
function validatePhone () {
if (document.signup.Phone.value.search (/\d{3}\-\d{3}\-\d{4}/) == -1) {
window.alert ("Please enter a valid phone number with the format 555-555-5555");
Phone.focus ();
return false;
}
else {
window.alert ("That is a correct format for a phone number");
}
}
//-->
</script>
</head>
<body>
<FORM NAME="signup" action="" METHOD="post"
onSubmit="return validatePhone ()">
<div style="text-align: left; padding-right: 28px">
Phone (xxx-xxx-xxxx):
<INPUT TYPE="text" NAME="Phone" VALUE="" SIZE=20><br><br>
</div>
<br>
<input type="submit" name="Submit">
</FORM>
</body>
|