简体   繁体   English

JavaScript正则表达式手机号码格式

[英]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. var test是接收输入的文本字段。

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/ https://jsfiddle.net/xu9fcbxt/

Notice: jQuery required 注意:需要jQuery

Code: 码:

JS: 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: HTML:

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

This would check if the number has a format like 0151-123-4567 这将检查数字是否具有像0151-123-4567的格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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