需要手机号码格式的正则表达式 [英] Need Regular expression for Mobile number format

查看:99
本文介绍了需要手机号码格式的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要手机号码的正则表达式,只允许2个空格和11位数字。 ...请尽快回复我..



喜欢这样



021 123 4567是有效

0211234567有效

0211 234 567有效

I need regular expression for mobile number which only allow 2 spaces and 11 digits number. ... Please reply me as soon as possible..

Like that

021 123 4567 is valid
0211234567 is valid
0211 234 567 is valid

推荐答案

您的示例未按照您的提问。看起来您的有效手机号码由10位数字组成,中间有两个单独的空格。是的,尝试示例代码:

Your examples did not follow your question. Look like your valid mobile number consists of 10 digits with or without 2 single spaces in between. Right, the try the sample code:
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Validate...</button>
<script>
function myFunction() {

    var result = "Failed!";
    var isMatched = false;

    // First validate the pattern, ignoring digits count
    var mobileNum = "0211 234 567";
    var pattern = /^\d+\s?\d+\s?\d+


/ g;
var isMatched = pattern.test(mobileNum);

//接下来做数字计数
if(isMatched){
var digits = mobileNum.replace(/ + / g,);
if(digits.length == 10){
result =Passed!;
}
}

alert(result);
}
< / script >

< / body >
< / html < span class =code-keyword>>
/g; var isMatched = pattern.test(mobileNum ); // Next do digits count if (isMatched){ var digits = mobileNum.replace(/ +/g, ""); if(digits.length == 10){ result = "Passed!"; } } alert(result); } </script> </body> </html>


参见 https://www.google.com/search?q=regular+expression [ ^ ]。


这篇关于需要手机号码格式的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆