-4

I am new to programming.

I am trying to create validation for a UK postcode.

I have found what I believe will do the job (see below), but I do not know what other lines I need to enter to complete the process.

^(([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}))$

Can anyone please help me with the other lines I would need to type to get this validation to work.

I checked the above code with the source and typed it into my programme (see below) when I tried to run the programme it came up with 54 errors. What am I missing?

namespace 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}))$
    }
}

}

user3511106
  • 21
  • 1
  • 4
  • Search for `C# Regex` and you will find examples. – dognose Apr 26 '14 at 10:11
  • *Why* do you believe what you have "found" will do the job? What does it do? What exactly does it *not* do? – O. R. Mapper Apr 26 '14 at 10:11
  • 1
    Using regex validation is a very common task and there are a *lot* of resources on how to do it online. – Ant P Apr 26 '14 at 10:13
  • I found this on another thread on this site. It is designed to allow all standard forms of a UK postcode. Further to this not all characters are allowed in all positions, this code takes this into account. – user3511106 Apr 26 '14 at 10:24
  • UK Postcode Regex (Comprehensive) is where I got the code from. The problem is I know I need to type more than just this, but I don't know what that is. – user3511106 Apr 26 '14 at 10:32
  • You say "when I tried to run the programme it came up with 54 errors". Was that actually when you tried to compile it? What were the errors? What can you correct on your own? Visual Studio should be giving you a lot of help here by way of tooltips, an Error list, underlined sections, and so on. – ClickRick Apr 26 '14 at 13:02
  • It was when I clicked to run debug. I have used the error list to correct most of the errors myself, but there is still one I am struggling with. Only assignment, decrement, await and new object expressions can be used as a statement – user3511106 Apr 26 '14 at 13:20

3 Answers3

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));
}
HABJAN
  • 9,212
  • 3
  • 35
  • 59
0
Regex r = new Regex(pattern);
Match m = r.Match(postcode);
if(m.Success)
{
//valid postcode
}
leskovar
  • 661
  • 3
  • 8
0

It depends if its ASP.Net controls you can do like

<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}$" />

using javascript you can do like this

str.match(regexExpression);

If you are doing server side validation using C# you can do

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

EDIT(based on comment):: You can add call below method on textbox leave event or on form submit. This ex is for email verification, you just need to replace strRegex value and need to pass your textbox value to the method. If this returns false show in MessageBox or show this in your error label

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);
}
Rishikesh
  • 486
  • 6
  • 15