简体   繁体   English

如何验证此格式的电话号码

[英]How to validate a phone number in this format

So in my check phone number function, I have: the variable pn represents the textfield where the client inputs the phone number. 所以在我的支票电话号码功能中,我有:变量pn代表客户输入电话号码的文本字段。

The desired format is: 000-0000 (obviously not only zeroes, but any digit 0-9). 所需的格式为:000-0000(显然不仅是零,而是任何数字0-9)。

How can I use regex to validate that it is in the desired format, and give an alert if it isn't? 如何使用正则表达式验证它是否为所需格式,如果不是,则发出警报? This is what i have 这就是我所拥有的

var pn = document.getElementById('ph').value;
//var vd = pn.search?

//if(not valid, alert("Not a valid number!");

The desired format is: 000-0000

Then following regex should work: 然后跟着正则表达式应该工作:

/^\d{3}-\d{4}$/

TEST: 测试:

re = /^\d{3}-\d{4}$/;

re.test('123-4567'); // true
re.test('123-45678'); // false

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

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