DeniseBarker
asked on
Validate a Non-Required Phone Number Using Javascript
I have a javascript function that is supposed to validate Required Phone Numbers, checking for length and numbers/characters, etc... I need to modify this to validate a Non-Required Phone Number (if the field is blank, do nothing; if a user enters a phone number, THEN validate it correctly). I've tried different variations, but don't understand javascript well enough to make it work. Here's the original script:
function validateForm(f){
if(f.userPhone1.value == ""){
alert("Please enter the Presenter's full phone number (xxx-xxx-xxxx).");
f.userPhone1.focus();
return false;
} else if (!validateUSPhone(f.userPh one1.value )) {
alert("Please enter the Presenter's phone number in format xxx-xxx-xxxx.");
f.userPhone1.focus();
return false;
}
if (CheckPhoneNumber(f.userPh one1.value )) {
alert("Please enter the Presenter's phone number in format xxx-xxx-xxxx.");
f.userPhone1.focus();
return false;
}
f.submit();
}
function CheckPhoneNumber(TheNumber ) {
var valid = 1
var GoodChars = "0123456789-"
var i = 0
if (TheNumber=="") {
// Return false if number is empty
return false;
}
for (i =0; i <= TheNumber.length -1; i++) {
if (GoodChars.indexOf(TheNumb er.charAt( i)) == -1) {
// Note: Remove the comments from the following line to see this
// for loop in action.
// alert(TheNumber.charAt(i) + " is no good.")
return false;
} // End if statement
if (TheNumber.charAt(i) == "-" && i != 3 && i != 7) {
return false;
} // End if statement
} // End for loop
return true;
}
function validateUSPhone(str){
str = strip("*() -./_\n\r\t\\",str);
if(str.length == 10)
return true;
else
return false;
}
Any help is very much appreciated!!! I will award full points, as I need to solve this quickly! Thank you!
function validateForm(f){
if(f.userPhone1.value == ""){
alert("Please enter the Presenter's full phone number (xxx-xxx-xxxx).");
f.userPhone1.focus();
return false;
} else if (!validateUSPhone(f.userPh
alert("Please enter the Presenter's phone number in format xxx-xxx-xxxx.");
f.userPhone1.focus();
return false;
}
if (CheckPhoneNumber(f.userPh
alert("Please enter the Presenter's phone number in format xxx-xxx-xxxx.");
f.userPhone1.focus();
return false;
}
f.submit();
}
function CheckPhoneNumber(TheNumber
var valid = 1
var GoodChars = "0123456789-"
var i = 0
if (TheNumber=="") {
// Return false if number is empty
return false;
}
for (i =0; i <= TheNumber.length -1; i++) {
if (GoodChars.indexOf(TheNumb
// Note: Remove the comments from the following line to see this
// for loop in action.
// alert(TheNumber.charAt(i) + " is no good.")
return false;
} // End if statement
if (TheNumber.charAt(i) == "-" && i != 3 && i != 7) {
return false;
} // End if statement
} // End for loop
return true;
}
function validateUSPhone(str){
str = strip("*() -./_\n\r\t\\",str);
if(str.length == 10)
return true;
else
return false;
}
Any help is very much appreciated!!! I will award full points, as I need to solve this quickly! Thank you!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you... I'll enter this new code and test and keep you posted... Thanks again -- much appreciated!
ASKER
Well... I plugged this newer version in and it still fails as before... The CheckPhoneNumber appears to work fine as long as the field is required; we've never used this in an "unrequired" situation before, so it may be that this function needs rewriting for this particular situation... Unfortunately, I don't know how it needs to be re-written...
As with a required phone number, we need it to continue to validate for numbers and dashes only (and in the appropriate sequence), and for exactly 10 digits, and again only if there is something entered in the field (not blank); if the field is blank, it should be ignored... Your help is again much appreciated... Thank you so much!
As with a required phone number, we need it to continue to validate for numbers and dashes only (and in the appropriate sequence), and for exactly 10 digits, and again only if there is something entered in the field (not blank); if the field is blank, it should be ignored... Your help is again much appreciated... Thank you so much!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It continues... I entered the changes you specified above, including the ! ... Now it operates as if the field must be required. When I leave it blank, I get the 'CheckPhoneNumber' alert; otherwise it works correctly -- if I enter less than 10 digits, or letters, it will give me the alert, which is correct. But it won't let me leave the field blank... Any other ideas? Thanks so much...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried the first function above and the results were the same as before -- fails when field is empty. So I entered the 2nd function above; now I get a "null" dialog box followed by the same 'CheckPhoneNumber' alert as before, whenever the field is left empty... Sorry!! Other changes to try?? Thanks again!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Fabulous! Wonderful!! Now it accepts the field as empty correctly and validates the field as 10 digits, etc. The final issue --- when I enter a 10 digit number correctly, it pops up a dialog box showing what I entered. Can you make that disappear? We would like for the user to just enter the number and validate without that extra alert... Thanks so very much! I appreciate YOUR EXTREME PATIENCE on this! THANK YOU!
ASKER
b0lsc0tt: ...
I fixed that last issue mentioned above by commenting out:
alert(TheNumber.match(/^\d {3}-\d{3}- \d{4}$/));
I fixed that last issue mentioned above by commenting out:
alert(TheNumber.match(/^\d
ASKER
b0lssc0tt:
Thank you so very very much for all your assistance and determination!! Your code is working beautifully now. We greatly appreciate your assistance -- I never would have figured all that out. Thanks so very much!!
I've actually got a very similar problem which I'm posting in a separate question... Now I need to get the email address validated, and just like with the phone number, this field is not required... Be on the lookout for it! Thanks again!! I'm sure many users will benefit from your efforts...
Thank you so very very much for all your assistance and determination!! Your code is working beautifully now. We greatly appreciate your assistance -- I never would have figured all that out. Thanks so very much!!
I've actually got a very similar problem which I'm posting in a separate question... Now I need to get the email address validated, and just like with the phone number, this field is not required... Be on the lookout for it! Thanks again!! I'm sure many users will benefit from your efforts...
Your welcome! I'm glad that did it. The alert can be deleted, it was just added for troubleshooting earlier.
I'll take a look at the other question. Hopefully I can redeem myself but it is even later now. :D
Thanks for the grade, the points and the fun question.
bol
I'll take a look at the other question. Hopefully I can redeem myself but it is even later now. :D
Thanks for the grade, the points and the fun question.
bol
ASKER