admin管理员组文章数量:1023204
I need a simple regex to validate a phone number of the form x-y, where x and y can represent any number of digits and the dash is optional, but if it does show up it most be within the string (the dash must have digits at its left and right)
I need a simple regex to validate a phone number of the form x-y, where x and y can represent any number of digits and the dash is optional, but if it does show up it most be within the string (the dash must have digits at its left and right)
Share Improve this question asked Oct 15, 2009 at 20:41 kjvkjv 11.3k35 gold badges102 silver badges141 bronze badges 1- 1 Maybe post what have you so far? – Dana Commented Oct 15, 2009 at 20:42
2 Answers
Reset to default 6/^\d+(?:-\d+)?$/
should do the trick.
/^\d+(-\d+)?$/) seems to work. It matches one or more leading digits, with an optional "hyphen followed by one or more digits".
#!/usr/bin/perl
#
@A = ( "1-2",
"-12",
"12-",
"123-1234",
"1-",
"-1",
"123",
"1",
"foo-bar",
"12foo34",
"foo12-34",
"12f-o34",
);
foreach (@A) {
if (/^\d+(-\d+)?$/) {
print "\"$_\" is a phone number\n";
} else{
print "\"$_\" is NOT a phone number\n";
}
}
gives:
$ ./phone.pl
"1-2" is a phone number
"-12" is NOT a phone number
"12-" is NOT a phone number
"123-1234" is a phone number
"1-" is NOT a phone number
"-1" is NOT a phone number
"123" is a phone number
"1" is a phone number
"foo-bar" is NOT a phone number
"12foo34" is NOT a phone number
"foo12-34" is NOT a phone number
"12f-o34" is NOT a phone number
I need a simple regex to validate a phone number of the form x-y, where x and y can represent any number of digits and the dash is optional, but if it does show up it most be within the string (the dash must have digits at its left and right)
I need a simple regex to validate a phone number of the form x-y, where x and y can represent any number of digits and the dash is optional, but if it does show up it most be within the string (the dash must have digits at its left and right)
Share Improve this question asked Oct 15, 2009 at 20:41 kjvkjv 11.3k35 gold badges102 silver badges141 bronze badges 1- 1 Maybe post what have you so far? – Dana Commented Oct 15, 2009 at 20:42
2 Answers
Reset to default 6/^\d+(?:-\d+)?$/
should do the trick.
/^\d+(-\d+)?$/) seems to work. It matches one or more leading digits, with an optional "hyphen followed by one or more digits".
#!/usr/bin/perl
#
@A = ( "1-2",
"-12",
"12-",
"123-1234",
"1-",
"-1",
"123",
"1",
"foo-bar",
"12foo34",
"foo12-34",
"12f-o34",
);
foreach (@A) {
if (/^\d+(-\d+)?$/) {
print "\"$_\" is a phone number\n";
} else{
print "\"$_\" is NOT a phone number\n";
}
}
gives:
$ ./phone.pl
"1-2" is a phone number
"-12" is NOT a phone number
"12-" is NOT a phone number
"123-1234" is a phone number
"1-" is NOT a phone number
"-1" is NOT a phone number
"123" is a phone number
"1" is a phone number
"foo-bar" is NOT a phone number
"12foo34" is NOT a phone number
"foo12-34" is NOT a phone number
"12f-o34" is NOT a phone number
本文标签: javascriptSingle dash phone numberregex validationStack Overflow
版权声明:本文标题:javascript - Single dash phone number - regex validation - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745542008a2155223.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论