简体   繁体   中英

ASP.NET MVC Data Validation for Phone Number

hope you are doing well, I am new to ASP.NET MVC and I am doing data validation for phoneNumber attribute which is of type int .

I want that the phone number to start from 0 and after that 9 digits long but it is important to start from 0.

So, I made this regular expression ^[0]{1}[0-9]{9}$

but the issue is that whenever the http post request comes and I see the data, I figured out that the phone number does not start from zero and start from the number after the 0. Like this

0557998765 (This is what is supposed to appear) 557998765 (unfortunately that is what appearing)

Can anyone help? This is the code for phoneNumber attribute in my Model.

[Required(ErrorMessage = "You must provide a phone number")]
[DataType(DataType.PhoneNumber)]
[RegularExpression("^[0]{1}[0-9]{9}$", ErrorMessage = "Phone Number must be 10 Digits Long.")]
public int PhoneNumber { get; set; }

你可以试试这条线而不是你的线:

[RegularExpression("^(0)(\d{9})$", ErrorMessage = "Phone Number must be 10 Digits Long.")]

a 32 bit integer is not big enough to hold the value of a phone number if the first digit is greater than '2'. the maximum being 2147483647. So if you phone number starts with a 3 or greater it won't validate for sure. this field needs to be a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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