Link to home
Start Free TrialLog in
Avatar of DeniseBarker
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.userPhone1.value)) {
            alert("Please enter the Presenter's phone number in format xxx-xxx-xxxx.");
            f.userPhone1.focus();
            return false;      
      }
      
      if (CheckPhoneNumber(f.userPhone1.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(TheNumber.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!

SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DeniseBarker
DeniseBarker

ASKER

Well, I plugged the suggested code above into my .js file and still have a problem...  It works fine when the phone field is left blank, but when I enter any kind of phone #, be it 10 digits or less, it fails with the error:  "Please enter the Presenter's phone number... " etc....  (If I enter less than 10 digits, it fails on the first validation (!validateUSPhone), and if I correctly put in all 10 digits (like:  111-222-3333) it fails on the second part (CheckPhoneNumber)...    Help!   ???   Thanks!!!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you... I'll enter this new code and test and keep you posted... Thanks again -- much appreciated!
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!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
b0lsc0tt: ...
I fixed that last issue mentioned above by commenting out:  
 alert(TheNumber.match(/^\d{3}-\d{3}-\d{4}$/));
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...
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