gpt4 book ai didi

c# - 电话验证属性不起作用

转载 作者:行者123 更新时间:2023-11-30 23:06:54 25 4
gpt4 key购买 nike

我正在尝试验证一些用户联系方式,如下所示:

public class Customer
{
[Display(Name = "Your e-mail :")]
[Required(ErrorMessage = "An email address is required")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string ContactEmail { get; set; }

[Display(Name = "Your contact number :")]
[Required(ErrorMessage = "A phone number is required")]
[Phone(ErrorMessage = "Invalid Phone Number")]
public string ContactNumber { get; set; }

}

在我看来,电子邮件验证非常有效,并且只允许有效的电子邮件地址。但是,电话验证根本不起作用,它允许各种字母和特殊字符。

文档指出 PhoneAttribute

Specifies that a data field value is a well-formed phone number using a regular expression for phone numbers

那么为什么这没有发生呢?

最佳答案

使用以下代码进行电话验证。

[Display(Name = "Your contact number :")]
[Required(ErrorMessage = "A phone number is required.")]
[DataType(DataType.PhoneNumber, ErrorMessage = "Invalid Phone Number")]
[RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid Phone Number.")]
public string ContactNumber { get; set; }

关于c# - 电话验证属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47792942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com