Jquery Validation US Phone Numbers


Jquery Validation US Phone Numbers

jQuery.validator.addMethod("us_phone_numbers", function(phone_number, element) {
    var digits = "0123456789";
    var phoneNumberDelimiters = "()-.";
    var validWorldPhoneChars = phoneNumberDelimiters + "+";
    var minDigitsInIPhoneNumber = 10;
    s=stripCharsInBag(phone_number,validWorldPhoneChars);
    return this.optional(element) || isInteger(s) && s.length >= minDigitsInIPhoneNumber;
    }, "Please enter a valid phone number");
    
    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;
    }
    

Thanks to :
http://www.scriptygoddess.com/archives/2008/05/03/phone-number-validation-with-jquery/

Jquery Animated Validation

jQuery Animated Validation

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools