PDA

View Full Version : How can I format multiple phone numbers?


psalms1493
10-16-2002, 05:26 PM
I have a javascript that formats phone numbers, and I have called it using an onClick. I would like to use it for phone, fax, and cell fields, but I can only get it to work in one place at a time. I tried changing the variables, but that didn't work either.

Is there some other way that I can use the same thing in multiple places w/out causing a conflict?

beetle
10-16-2002, 05:57 PM
because you have hardcoded the form element into the function...

document.PlaceOrder.Phone

You need to pass this as a parameter.

Also...your phone validation code works, I'm sure, but could be done much easier with regular expressions<html>
<head>
<title>Phone filter</title>
<script>
function ValidatePhone(elem) {
var pn = elem.value;
if (pn == "") return;
pn = pn.replace(/\D/g,"");

if (pn.length == 7)
elem.value = pn.replace(/(\d{3})(\d{4})/,"$1-$2");
else if (pn.length == 10)
elem.value = pn.replace(/(\d{3})(\d{3})(\d{4})/,"($1) $2-$3");
else {
alert('The '+elem.name+' must be a 7 or 10 digit phone number');
elem.focus();
return false;
}
}
</script>
</head>

<body>
<form>
Phone: <input type="text" name="phone" onBlur="ValidatePhone(this)"><br>
Fax: <input type="text" name="fax" onBlur="ValidatePhone(this)"><br>
</form>
</body>
</html>