验证英国邮政编码

时间:2014-04-26 10:09:21

标签: c#

我是编程新手。

我正在尝试为英国邮政编码创建验证。

我找到了我相信会做的工作(见下文),但我不知道我需要输入哪些其他线来完成这个过程。

^(([gG][iI][rR] {0,}0[aA]{2})|((([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y]?[0-9][0-9]?|(([a-pr-uwyzA-PR-UWYZ][0-9][a-hjkstuwA-HJKSTUW]|([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y][0-9[abehmnprv-yABEHMNPRV-Y])))&[0-9][abd-hjlnp-uw-zABD-HJLNP-UW-Z]{2}))$

任何人都可以帮我解决我需要输入的其他行以使此验证工作。

我用源检查了上面的代码并将其输入我的程序(见下文),当我尝试运行程序时出现了54个错误。我错过了什么?

命名空间Moondog_odering_system {     public partial class CustomerDetails:Form     {         public CustomerDetails()         {             的InitializeComponent();         }

    private void textBox11_TextChanged(object sender, EventArgs e)
    {
        ^(([gG][iI][rR] {0,}0[aA]{2})|((([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y]?[0-9][0-9]?)|(([a-pr-uwyzA-PR-UWYZ][0-9][a-hjkstuwA-HJKSTUW])|([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y][0-9][abehmnprv-yABEHMNPRV-Y]))) {0,}[0-9][abd-hjlnp-uw-zABD-HJLNP-UW-Z]{2}))$
    }
}

}

3 个答案:

答案 0 :(得分:0)

/// <summary>
/// The order of the UK postcode regex patterns needs to be as it is. Do not mix the order !!!
/// </summary>
private static readonly Regex[] _uk_postcode = new Regex[] {
        new Regex("(^[A-PR-UWYZa-pr-uwyz][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)", RegexOptions.Compiled),
        new Regex("(^[A-PR-UWYZa-pr-uwyz][0-9][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)", RegexOptions.Compiled),
        new Regex("(^[A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)", RegexOptions.Compiled),
        new Regex("(^[A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)", RegexOptions.Compiled),
        new Regex("(^[A-PR-UWYZa-pr-uwyz][0-9][A-HJKS-UWa-hjks-uw][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)", RegexOptions.Compiled),
        new Regex("(^[A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][A-Za-z][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)", RegexOptions.Compiled),
        new Regex("(^[Gg][Ii][Rr][]*0[Aa][Aa])") };

public static bool IsPostcodeValid(string text)
{
    return (_uk_postcode[0].IsMatch(text) ||
            _uk_postcode[1].IsMatch(text) ||
            _uk_postcode[2].IsMatch(text) ||
            _uk_postcode[3].IsMatch(text) ||
            _uk_postcode[4].IsMatch(text) ||
            _uk_postcode[5].IsMatch(text) ||
            _uk_postcode[6].IsMatch(text));
}

答案 1 :(得分:0)

Regex r = new Regex(pattern);
Match m = r.Match(postcode);
if(m.Success)
{
//valid postcode
}

答案 2 :(得分:0)

这取决于它的ASP.Net控件是否可以像

那样
<form id="form1" runat="server">
    <asp:TextBox ID="txtName" runat="server"/>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    <asp:RegularExpressionValidator ID="regexpName" runat="server"     
                                    ErrorMessage="This expression does not validate." 
                                    ControlToValidate="txtName"     
                                    ValidationExpression="^[a-zA-Z'.\s]{1,40}$" />

使用javascript你可以这样做

str.match(regexExpression);

如果您使用C#进行服务器端验证,则可以执行

System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))

编辑(基于评论)::您可以在文本框离开事件或表单提交上添加以下方法。这个例子用于电子邮件验证,您只需要替换strRegex值并需要将文本框值传递给方法。如果这在MessageBox中返回false show或在错误标签中显示

public static bool IsValidEmail(string inputEmail)
{
   string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
         @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + 
         @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
   Regex re = new Regex(strRegex);
   if (re.IsMatch(inputEmail))
    return (true);
   else
    return (false);
}