是否有正则表达式来验证邮政编码?



问题是我的邮政编码没有经过验证,所以只能有四位数字,第一个数字不能9

这是针对我的新网站的。我试过使用它:

[0-9]{5}(?:-[0-9]{4})?$

我的代码:

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-9a-zA-Z' ]{3,20}")]
public string firstName { get; set; }
[Display(Name = "FamilyName")]
[MinLength(2), StringLength(20), Required(ErrorMessage = 
@"The Family Name is required.")]
[RegularExpression(@"[0-9a-zA-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 in the format 04xx xxx xxx.")]
[RegularExpression(@"^(+d{1,2}s)?(?d{4})?[s.-]d{3}        
[s.-]d{3}$")]
public string mobileNumber { get; set; }
[MinLength(4), StringLength(4), Required(ErrorMessage =         
@"The Postal Code is required.")]
[RegularExpression(@"")]
public string postalCode { get; set; }
}
}

预期结果是所有以9开头或不是 4 位数字的开机自检代码将显示错误消息。实际结果是,它目前接受任何东西。

使用模式^[0-8]d{3}$

解释

^- 匹配字符串的开头

[0-8]- 匹配未9的数字

d{3}- 匹配三位数字(任意(

$- 匹配字符串的结尾

演示

最新更新