[英]Phone number regex - Merge three conditions
I need to create a regex function which will validate a phone number field based on certain conditions and show alerts for each of the three cases. 我需要创建一个正则表达式函数,该函数将根据某些条件验证电话号码字段,并针对这三种情况分别显示警报。
Practically I have this 3 regex functions which I want to combine in a single one. 实际上,我有这3个正则表达式函数,我想将它们合并到一个函数中。
/^3\d{9}$/; //If it starts with 3 and has another 9 numbers it's a cellphone
/^0\d{7,10}$/; //If it starts with 0 and has another 7-10 numbers it's a landline
/^(?:00|\+)/; //If it starts with 00 or a + sign, it's an international number
What I am trying to achieve, is to have a javascript function which will combine this three regex functions and show a certain message in case the number is not valid. 我要实现的目标是具有一个javascript函数,该函数将结合这三个regex函数并在数字无效的情况下显示特定消息。
So for example if the number starts with 3 but it has less or more than 9 numbers after the 3, probably it's a wrong cellphone number so I want to warn the user about that. 因此,例如,如果数字以3开头,但在3后面有少于或大于9个数字,则可能是错误的手机号码,因此我想警告用户。 Same thing for the Landline.
固定电话也是如此。 For the international numbers I simply want to make them aware that it might be an international number because it starts with double 00 or + sign.
对于国际号码,我只是想让他们知道它可能是国际号码,因为它以双00或+号开头。
My problem is that I don't know how to combine this three regex values in a single one, which will allow me to build a simple and clean javascript code. 我的问题是,我不知道如何将这三个regex值组合到一个值中,这将使我能够构建一个简单干净的javascript代码。
I think this will work for you: /^(?:3\\d{9}|0\\d{7,10}|(?:00|\\+)\\d+)$/g
我认为这对您
/^(?:3\\d{9}|0\\d{7,10}|(?:00|\\+)\\d+)$/g
: /^(?:3\\d{9}|0\\d{7,10}|(?:00|\\+)\\d+)$/g
const testPhoneNumber = number => /^(?:3\\d{9}|0\\d{7,10}|(?:00|\\+)\\d+)$/.test(number) const numbers = [123, "+48667065144", 3111222333, "001234567", "00123456879", 6473812354, 3475456389, 7483925821] for (const number of numbers) { console.log(`${number} is ${testPhoneNumber(number) ? "valid": "not valid"} phone number`) }
match(/(^3\d{9}$)|(^0\d{7,10}$)|(^(?:00|\+))/)
this will capture the matched data in an array (size 4). 这将捕获匹配的数据到一个数组中(大小为4)。
position 0 : the matched data 位置0:匹配的数据
position 1 : matched data of the 1st regexp 位置1:第一个正则表达式的匹配数据
.... ....
function check(number) { //var number = '3123456789'; //var number = '01234567'; //var number = '001'; var regexList = [ '^3\\\\d{9}$', //If it starts with 3 and has another 9 numbers it's a cellphone '^0\\\\d{7,10}$', //If it starts with 0 and has another 7-10 numbers it's a landline '^[0]{2}|[\\+]' //If it starts with 00 or a + sign, it's an international number ]; for (var r in regexList) if (number.match(new RegExp(regexList[r]))) break; else r = null; switch (r) { case "0": alert('If it starts with 3 and has another 9 numbers it\\'sa cellphone'); break; case "1": alert('If it starts with 0 and has another 7-10 numbers it\\'sa landline'); break; case "2": alert('If it starts with 00 or a + sign, it\\'s an international number'); break; default: alert('Invalid number'); } }
<input type="text" id="numberToCheck" /> <input type="button" onclick="check(document.getElementById('numberToCheck').value)" />
As "elegant" as I can make it... 我可以做到的“优雅”
var input = '00372553253';
var matches = [];
// The following is technically one line!
[
{regex: /^3\d{9}$/, type: "cellphone"},
{regex: /^0\d{7,10}$/, type: "landline"},
{regex: /^(?:00|\+)/, type: "international"}
].forEach(
function(element) {
if (element.regex.test(input))
matches.push(element.type);
}
);
alert(matches);
The test case will actually match two regexes! 测试用例实际上将匹配两个正则表达式!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.