Javascript Regex - What to use to validate a phone number?

后端 未结 6 624
你的背包
你的背包 2020-12-05 21:04

Could anyone tell me what RegEx would work to validate an international phone number including white space between the numbers and also allowing for these chars: - ( )

相关标签:
6条回答
  • 2020-12-05 21:17
    /*
    @isValidUSPhoneFormat function will check valid US Format
        Allowed US Format
    (123) 456-7890
    123-456-7890
    123.456.7890
    1234567890
    (734) 555.1212
    */   
    
        function isValidUSPhoneFormat(elementValue){  
                var phoneNumberPattern = /^[(]{0,1}[0-9]{3}[)]{0,1}[-\s.]{0,1}[0-9]{3}[-\s.]{0,1}[0-9]{4}$/;  
                if(phoneNumberPattern.test(elementValue) == false)
                {
                     var phoneNumberPattern = /^(\()?\d{3}(\))?(.|\s)?\d{3}(.|\s)\d{4}$/; 
                     return phoneNumberPattern.test(elementValue);   
                }
                return phoneNumberPattern.test(elementValue);  
            }
    

    May this will help you to understand JavaScript RegEx..

    0 讨论(0)
  • 2020-12-05 21:18

    Try this code

    HTML Code

    <input type="text" id="phone"/>
    

    JS Code

    $("#phone").blur(function() {
      var regexp = /^[\s()+-]*([0-9][\s()+-]*){6,20}$/
      var no = $("#phone").val();
      if (!regexp.test(no) && no.length < 0) {
        alert("Wrong phone no");
      }
    });
    
    0 讨论(0)
  • 2020-12-05 21:21

    See A comprehensive regex for phone number validation

    Quick cheat sheet

    • Start the expression: /^
    • If you want to require a space, use: [\s] or \s
    • If you want to require parenthesis, use: [(] and [)] . Using \( and \) is ugly and can make things confusing.
    • If you want anything to be optional, put a ? after it
    • If you want a hyphen, just type - or [-] . If you do not put it first or last in a series of other characters, though, you may need to escape it: \-
    • If you want to accept different choices in a slot, put brackets around the options: [-.\s] will require a hyphen, period, or space. A question mark after the last bracket will make all of those optional for that slot.
    • \d{3} : Requires a 3-digit number: 000-999. Shorthand for [0-9][0-9][0-9].
    • [2-9] : Requires a digit 2-9 for that slot.
    • (\+|1\s)? : Accept a "plus" or a 1 and a space (pipe character, |, is "or"), and make it optional. The "plus" sign must be escaped.
    • If you want specific numbers to match a slot, enter them: [246] will require a 2, 4, or 6. [77|78] will require 77 or 78.
    • $/ : End the expression
    0 讨论(0)
  • 2020-12-05 21:27

    Don't even try. Trying to guard against what you think is invalid input can result in angry users who can't enter perfectly valid phone numbers. And if the user really wants to enter an invalid phone number, he/she will be able to do it anyway.

    0 讨论(0)
  • 2020-12-05 21:36
    function checkPhoneNumber(val) {
        var num = document.getElementById(val).value;
        var mob=/^[+]*[(]{0,1}[0-9]{1,3}[)]{0,1}[-\s\./0-9]*$/g;
        if (mob.test(num) == false) {
            alert("Please Enter Valid Phone Number.");
            document.getElementById(val).value = "";
            return false;
        }
         if (num.length > 15) {
            alert("Only 15 characters allowed for Phone Number field.");
            document.getElementById(val).value = "";
            return false;
        }
    
        return true;
    }
    

    Try it Ones

    0 讨论(0)
  • 2020-12-05 21:37

    This is a long regex, but it supports both formats (for example 2 to be a valid international number, is MUST start with either + or 00):

    /^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/i
    

    This allows extensions and a multiple choice of formats and separators.

    Matches:

    • (+351) 282 43 50 50
    • 90191919908
    • 555-8909
    • 001 6867684
    • 001 6867684x1
    • 1 (234) 567-8901
    • 1-234-567-8901 x1234
    • 1-234-567-8901 ext1234
    • 1-234 567.89/01 ext.1234
    • 1(234)5678901x1234
    • (123)8575973
    • (0055)(123)8575973

    On $n, it saves:

    1. Country indicator
    2. Phone number
    3. Extention

    This same answer was given here: A comprehensive regex for phone number validation (direct link to my answer)

    0 讨论(0)
提交回复
热议问题