SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery Validator Postcode Regex

    Hi,

    I have the following jQuery validator that validates a UK postcode:

    Code:
    jQuery.validator.addMethod("UKPostcode", function(value, element) { 
    	return this.optional(element) || /[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}/.test(value);
    }, "Must be a valid Postcode");
    This matches against for e.g OL15 1HG, BUT it doesn't match against ol15 1hg. The problem is that it doesn't accept lowercase characters. I thought I could just use [A-Za-z] but that didn't work.

    Any ideas?

    Thanks
    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    You can tell the regular expression to ignore case, by placing the letter i after the closing delimiter for the regular expression.

    Before:

    Code javascript:
    /.../.test(value)

    After:

    Code javascript:
    /.../i.test(value)
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice one, thanks for that. Worked a treat
    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •