Switch to full style
JavaScript code examples
Post a reply

validate international phone number value

Wed Oct 26, 2011 12:17 am

Check an international phone number using JavaScript.

Code:
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 6;

function checkInternationalPhone(strPhone){
var bracket=3

if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-&& strPhone.indexOf("(")>bracket)return false
var brchr
=strPhone.indexOf("(")
if(
strPhone.indexOf("(")!=-&& strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-&& strPhone.indexOf(")")!=-1)return false
s
=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
 

Code:
function stripCharsInBag(sbag)
{   var 
i;
    var 
returnString "";
    
// Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    
for (0s.lengthi++)
    {   
        
// Check that current character isn't whitespace.
        
var s.charAt(i);
        if (
bag.indexOf(c) == -1returnString += c;
    }
    return 
returnString;
}
 


Code:
function isInteger(s)
{   var 
i;
    for (
0s.lengthi++)
    {   
        
// Check that current character is number.
        
var s.charAt(i);
        if (((
"0") || ("9"))) return false;
    }
    
// All characters are numbers.
    
return true;
}
 




Post a reply
  Related Posts  to : validate international phone number value
 Now Hiring International Data Entry Typists     -  
 convert integer number to octal,hexadecimal number systems     -  
 validate username     -  
 validate mail in jsp     -  
 validate e-mail in php     -  
 validate domain name in php     -  
 convert decimal number to octal number     -  
 convert octal number to decimal number     -  
 validate email address in asp     -  
 validate age entered as selection box in javascript     -  

Topic Tags

JavaScript Validation