PDA

View Full Version : validating phone number?


jas
09-25-2002, 10:12 PM
I'm trying to validate phone numbers for my form. I cant figure it out. I've seen where you can do it by matching, but not sure how to do that. Here is my sample form with what I got so far oh yeah I want the format to be (xxx)-xxx-xxxx. If someone can give me some insight that would be great!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>“assignment3</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 6.00.2716.2200" name=GENERATOR></HEAD>
<script language="Javascript">

function sampleEntries() {

document.trip.name.value = "Jason"
document.trip.address.value = "3200 Creek Road"
document.trip.city.value = "city"
document.trip.state.value = "AZ"
document.trip.zip.value = "86121"
document.trip.phone.value = "451-1239"
document.trip.rb[1].checked = true
document.trip.vacation.selectedIndex = 2
document.trip.nights.selectedIndex = 3
document.trip.box1.checked = true
document.trip.car.value = "I would like to have an economy class car."
}

function validateInput() {

var LB = "\n";
var msg1 = "Please fill out your:" + LB + LB;
var msg2 = "";
var status = "OK"




var name = document.trip.name.value
var address = document.trip.address.value
var city = document.trip.city.value
var state = document.trip.state.value
var zip = document.trip.zip.value
var phone = document.trip.phone.value
var vacation = document.trip.vacation.selectedIndex
var nights = document.trip.nights.selectedIndex
var car = document.trip.car.value
var box1 = document.trip.box1.checked
var box2 = document.trip.box2.checked

if (name == "") {
msg2 += "Name" + LB;

}

if (address == "") {
msg2 += "Address" + LB;

}

if (city == "") {
msg2 += "City" + LB;

}

if (state == "") {
msg2 += "State" + LB;

}

if (zip == "") {
msg2 += "Zip" + LB;

}

if ((zip != "") && (zip.length !=5 || isNaN(zip))) {
msg2 += "Zip needs to be in nnnnn format" + LB;
}

if (phone == "") {
msg2 += "Phone number" + LB;

}

if (vacation == 0) {
msg2 += "Vacation" + LB;

}

if (nights == 0) {
msg2 += "Nights" + LB;

}

if (box1 == "" && box2 =="") {
msg2 += "Select yes or no" + LB;

}

if (car == "") {
msg2 += "Car" + LB;

}

if (msg2.length > 0){
alert(msg1 + msg2);
return false;
}
else return true;

}

</script>

Mr J
09-25-2002, 10:48 PM
You could include something like this:


if (phone.charAt(3)!="-") {
msg2 += "The fourth character should be a "+ LB+" (dash) -" + LB;
}

jas
09-25-2002, 11:31 PM
Hey thanks alot!!!

whammy
09-26-2002, 12:49 AM
<script type="text/javascript">
<!--
var vphone = /^\D*([1-9]\d{2})\D*(\d{3})\D*(\d{4})\D*$/
function formatphone(x){
if(vphone.test(x.value)){
x.value=x.value.replace(vphone,'($1) $2-$3');
}
else {
alert('Please enter a 10 digit phone number!');
x.focus();
}
}
// -->
</script>


P.S. Look at my sig... where it says "Think regular expressions" !

joh6nn
09-26-2002, 03:14 AM
you might want to take a look at this:

http://www.evolt.org/article/rating/4090/15118/index.html

jas
09-26-2002, 05:40 AM
just cut & paste that script into mine with no changes, Whammy?
This is what I did so far from the other example I was given.
if (((((phone.charAt(0)!="(" || phone.charAt(4)!=")" || phone.charAt(5)!="-" || phone.charAt(9)!="-" || phone.length !=14 || isNaN(phone.charAt(1))))))) {
msg2 += "Phone number format needs to be in (nnn)-nnn-nnnn" + LB;
}
It works good, but when I try to add: isNaN(phone.charAt(2) is does not work.

if (((((phone.charAt(0)!="(" || phone.charAt(4)!=")" || phone.charAt(5)!="-" || phone.charAt(9)!="-" || phone.length !=14 || isNaN(phone.charAt(1) || isNaN(phone.charAt(2)))))))) {
msg2 += "Phone number format needs to be in (nnn)-nnn-nnnn" + LB;
}
I can put a letter at char1 and I get an error but when I put a letter at char2 it just lets me submit the form with no errors. I'm missing something. It only lets me use one isNaN.


function validateInput() {

var LB = "\n";
var msg1 = "Please fill out your:" + LB + LB;
var msg2 = "";
var status = "OK"




var name = document.trip.name.value
var address = document.trip.address.value
var city = document.trip.city.value
var state = document.trip.state.value
var zip = document.trip.zip.value
var phone = document.trip.phone.value
var vacation = document.trip.vacation.selectedIndex
var nights = document.trip.nights.selectedIndex
var car = document.trip.car.value
var box1 = document.trip.box1.checked
var box2 = document.trip.box2.checked
if (name == "") {
msg2 += "Name" + LB;

}

if (address == "") {
msg2 += "Address" + LB;

}

if (city == "") {
msg2 += "City" + LB;

}

if (state == "") {
msg2 += "State" + LB;

}

if (zip == "") {
msg2 += "Zip" + LB;

}

if ((zip != "") && (zip.length !=5 || isNaN(zip))) {
msg2 += "Zip needs to be in nnnnn format" + LB;
}

if (phone == "") {
msg2 += "Phone number" + LB;

}

if (((((phone.charAt(0)!="(" || phone.charAt(4)!=")" || phone.charAt(5)!="-" || phone.charAt(9)!="-" || phone.length !=14 || isNaN(phone.charAt(1))))))) {
msg2 += "Phone number format needs to be in (nnn)-nnn-nnnn" + LB;
}

if (vacation == 0) {
msg2 += "Vacation" + LB;

}

if (nights == 0) {
msg2 += "Nights" + LB;

}

if (box1 == "" && box2 =="") {
msg2 += "Select yes or no" + LB;

}

if (car == "") {
msg2 += "Car" + LB;

}

if (msg2.length > 0){
alert(msg1 + msg2);
return false;
}
else return true;

}

</script>

jas
09-26-2002, 06:53 PM
...........

whammy
09-26-2002, 11:18 PM
Of course, if you want to use it, go ahead - scripting is scripting, I wouldn't put it here if I didn't want anyone to use it! :)

Besides, jkd originally came up with that one pretty much. I just changed it a little bit.

Regular Expressions are extremely powerful though - I'd recommend to everyone to learn them.

P.S. Although, as the article John posted a link to points out, I'd make sure you only validate using that if you know the user is from the USA or CANADA. Anyone else, I usually just accept whatever input they have provided for the phone number and strip any non-numeric characters out of it.