Link to home
Start Free TrialLog in
Avatar of strev
strev

asked on

Newbie Phone Validation

>Taller Mike, you provided me with a great regular >expression function:


function validatePhone( phoneNumber )
{
 var objRegExp  = /^\d{3}\-\d{4}$/;
 return objRegExp.test(phoneNumber);
}

This will validate 3 digits, then a dash, then 4 digits. If you want to make the dash option, you would change the expression like so:

var objRegExp  = /^\d{3}\-?\d{4}$/;

Either way, when you submit the form, you would validate the phone number by sending it's value to this function.



>Now I have one additional expression I would like to add >to this. Should the user desire NOT to enter the number >once the form phone input has focus and simply tab out, >how should I write the entire expression?

Thanks again
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark 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
Avatar of chimmi0912
chimmi0912

Follwoing javascript validation will check the Domestic and Internation Phone numbers with valid characters  and Delimiters like -,+,() and it won't accept the Phone number with less than 10 digits(Including Internationa and Domestic)


// Declaring required variables
     var digits = "0123456789";
     // non-digit characters which are allowed in phone numbers
     var phoneNumberDelimiters = "()- ";
     // characters which are allowed in international phone numbers
     // (a leading + is OK)
     var validWorldPhoneChars = phoneNumberDelimiters + "+";
     // Minimum no of digits in an international phone no.
     var minDigitsInIPhoneNumber = 10;
     
     function isInteger(s)
     {   var i;
          for (i = 0; i < s.length; i++)
          {  
               // Check that current character is number.
               var c = s.charAt(i);
               if (((c < "0") || (c > "9"))) return false;
          }
          // All characters are numbers.
          return true;
     }
     
     function stripCharsInBag(s, bag)
     {   var i;
          var returnString = "";
          // Search through string's characters one by one.
          // If character is not in bag, append to returnString.
          for (i = 0; i < s.length; i++)
          {  
               // Check that current character isn't whitespace.
               var c = s.charAt(i);
               if (bag.indexOf(c) == -1) returnString += c;
          }
          return returnString;
     }
     
     function checkInternationalPhone(strPhone){
     s=stripCharsInBag(strPhone,validWorldPhoneChars);
     return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
     }    
          var Phone=document.formname.Phone
         
          if ((Phone.value==null)||(Phone.value=="")){
                              alert("Valid Phone Number with Area Code or Country Code");
                   
          }
          else if (checkInternationalPhone(Phone.value)==false){
               alert("Valid Phone Number with Area Code or Country Code");
                   
          }
Avatar of strev

ASKER

Thanks, exactly what I needed...
Happy Holidays!