javascript - Regular expression not working with jquery form validation plugin -
i using form validation plugin , using regular expression validate uk post code. using this reference regular expression in author says can validate post code. when try submit form , every kind of string says invalid. have tried following (samples taken wikipedia here page
w1a 1hq ec1a 1bb m1 1aa b33 8th
here js code
$(document).ready(function(){ $.validator.addmethod("regex", function(value, element, regexp) { var re = new regexp(regexp); console.debug(re.test(value)) return this.optional(element) || re.test(value); }, "post code not valid" ); $("#addpropertyform").validate({ rules : { postcode : { required : true, regex: "(gir 0aa)|((([a-z-[qvx]][0-9][0-9]?)|(([a-z-[qvx]][a-z-[ijz]][0-9][0-9]?)|(([a-z-[qvx]][0-9][a-hjkstuw])|([a-z-[qvx]][a-z-[ijz]][0-9][abehmnprvwxy])))) [0-9][a-z-[cikmov]]{2})" } }, messages : { postcode : { required : 'post code required', } }, onkeyup: false, onblur: true, focuscleanup: true, focusinvalid: false }); });
can 1 kindly me wrong code
there strange bits this.
first of all, example postcode posted in fact concatenation of 4 postcodes: w1a 1hq, ec1a 1bb, m1 1aa , b33 8th.
second, regexp written in different dialext 1 used in javascript (i not know one). expression [a-z-[ijz]]
whould mean "any capital letter other i
, j
, z
". in javascript means "any capital letter or -
or [
followed ]
". there unecessary parantheses in it.
i did not take time fix regexp. removed bits check if there other issues. 1 should match valid postcodes (and many invalid ones):
/(gir 0aa|[a-z][0-9][0-9]?|[a-z][a-z][0-9][0-9]?|[a-z][0-9][a-hjkstuw]|[a-z][a-z][0-9][abehmnprvwxy]) [0-9][a-z]{2}/
Comments
Post a Comment