简体   繁体   English

是否有用于电话号码验证的正则表达式?

[英]Is there a Regex expression for phone number validation?

I'm setting up a new website, and want to validate user input phone number.我正在建立一个新网站,并希望验证用户输入的电话号码。 What Regex expression do I need to use to get the format 04xx xxx xxx?我需要使用什么正则表达式来获取 04xx xxx xxx 格式? There needs to be a space between the fifth and ninth character.第五个和第九个字符之间需要有一个空格。

I have tried using this expression: ^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\\s\\./0-9]*$ .我试过使用这个表达式: ^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\\s\\./0-9]*$

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.ComponentModel.DataAnnotations;

    namespace InspiringMagazines.Models
    {
        public class Customer
        {
                public int customerID { get; set; }
                [Display(Name = "GivenName")]
                [MinLength(2), StringLength(20), Required(ErrorMessage =                                                         
    @"The Given Name is required.")]
                [RegularExpression(@"[0-9][a-z'-]{3,20}")]
                public string firstName { get; set; }
                [Display(Name = "FamilyName")]
                [MinLength(2), StringLength(20), Required(ErrorMessage =         
    @"The Family Name is required.")]
                [RegularExpression(@"[0-9][a-z'-]{3,20}")]
                public string lastName { get; set; }
                [Display(Name = "Date of Birth")]
                public string dateOfBirth { get; set; }
                public string emailAddress { get; set; }
                [MinLength(12), StringLength(12), Required(ErrorMessage =         
    @"The Phone Number is required.")]

                public string mobileNumber { get; set; }
                public string postalCode { get; set; }
        }
    }

Expected result is to have phone numbers not in the format of 04xx xxx xxx to show an error message.预期结果是电话号码不是 04xx xxx xxx 格式以显示错误消息。 The actual result right now is all the numbers are being accepted.现在的实际结果是所有数字都被接受了。

The simplest regex in terms of comprehension would be:就理解而言,最简单的正则表达式是:

^04\d\d \d\d\d \d\d\d$

But beware, this would probably let them type in Unicode numbers outside the ascii range too但请注意,这可能会让他们也输入 ascii 范围之外的 Unicode 数字

To be honest, excessive validation is more irritating to users than it is helpful to you, especially when your error messages are so poor.老实说,过度验证对用户的刺激性大于对您的帮助,尤其是当您的错误消息如此糟糕时。 Having some complicated requirement for input and then only telling them "the given name is required" is bad.对输入有一些复杂的要求,然后只告诉他们“需要给定的名称”是不好的。 What if their name really is "Q"?如果他们的名字真的是“Q”呢? You don't say "given name must be at least two characters" (better error message) but then my question in this is "why must a name be at least two characters?"您不会说“给定的名称必须至少为两个字符”(更好的错误消息),但是我的问题是“为什么名称必须至少为两个字符?” What do you care if a person puts one character in as their name?如果一个人把一个字符作为他们的名字,你在乎什么?

Likewise for phone numbers , let them type in whatever they like, any old how.同样对于电话号码,让他们输入他们喜欢的任何内容,任何旧的方式。 Strip all the crap they typed in when you validate and check they entered 10 digits starting with 04. The error message should say "phone number should start with 04 and have 10 digits in total".当您验证并检查他们输入了以 04 开头的 10 位数字时,去掉他们输入的所有废话。错误消息应该说“电话号码应该以 04 开头,总共有 10 位数字”。 Prepopulate the box with 04 to help them out, and run this on what they give you:用 04 预先填充框以帮助他们,并在他们给你的东西上运行它:

Regex.Replace(theirInput, "[^0-9]", "");

It strips out all the hyphens, brackets, spaces etc. Check the length of the result.它去掉所有的连字符、括号、空格等。检查结果的长度。 The message here is: "don't require your user to type spaces in because the spaces really don't matter. You can put them in yourself in code, after you strip the user input of (0400) 223-456 down to just numbers 0400223456 , simply using string substring这里的信息是:“不要要求你的用户输入空格,因为空格真的无关紧要。你可以把它们放在自己的代码中,在你把(0400) 223-456的用户输入(0400) 223-456到只是数字0400223456 ,只需使用字符串子字符串

"Be tolerant of what you accept and strict of what you send" is the mantra for writing servers that talk HTTP, but it's a good thing to apply to any user input; “容忍你接受的东西,严格你发送的东西”是编写使用 HTTP 的服务器的口头禅,但适用于任何用户输入是一件好事; Always aim to take as much varied junk input off the user and clean it up to meet your standards rather than forcing the user to meet it, and if you are forcing the user, definitely tell them exactly where they're going wrong.始终旨在消除用户的各种垃圾输入并对其进行清理以满足您的标准,而不是强迫用户满足它,如果您强迫用户,一定要准确地告诉他们他们哪里出错了。 Aim for validation that feeds back instantly- don't have them submit the whole form then reject it and sent them back to one that is missing the values for its complicated double passwords fields and have them redo the captcha, or worse blank out all their input that was invalid旨在进行立即反馈的验证 - 不要让他们提交整个表单然后拒绝它并将它们发送回一个缺少复杂双密码字段值的表单,并让他们重做验证码,或者更糟的是将他们的所有内容都清空无效的输入

I tried with this regex我试过这个正则表达式

^(04\d{2}\s\d{3}\s\d{3})$

Tested on regexr在正则表达式上测试

For Nigerian phone numbers the following regex pattern works [0]([7-9][0]|[8][0-1])[0-9]{8} .对于尼日利亚电话号码,以下正则表达式模式适用[0]([7-9][0]|[8][0-1])[0-9]{8} Just paste it in your Regex match只需将其粘贴到您的正则表达式匹配中

regex for nigerian phone number尼日利亚电话号码的正则表达式

without spacing没有间距

 /^234[0-9]{10}$/

with spacing有间距

/^234\s[0-9]{10}$/

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

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