简体   繁体   中英

JavaScript regular expression mobile phone number format

1.XXX-XXX-XXXX

2.XXXXXXXXXX

I would like to know the regular expression of the format. Modifying the existing sources will yield results.


var regExp = /^01([016789]?)-([0-9]{3})-([0-9]{4})$/;

var regExp = /^01([016789]?)[0-9]{3}[0-9]{4}$/;

A statement to check the condition. I wonder if the contact form is also correct. var test is a text field that receives input.

if(!regExp.text) {
        alert(""phone number format is not valid.");
        document.getElementById('phone').focus();
        return ;
    }

I'm not quite sure what you are trying to achieve, but maybe this example helps:

https://jsfiddle.net/xu9fcbxt/

Notice: jQuery required

Code:

JS:

$(document).ready(function(){
    var regExp = /^01[5-7][1-9]-[0-9]{3}-[0-9]{4}/;         

  $('#phone').focusout(function(){
    var text = $('#phone').val();   
    if(!regExp.test(text)){
        alert('not a valid phone number');
    }
  });
});

HTML:

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

This would check if the number has a format like 0151-123-4567

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM