开发者

How to make sure white space after 3 characters using regular expressions

开发者 https://www.devze.com 2023-03-30 02:18 出处:网络
Actually, I want to validate the Canadianpostal code form field using jQuery validation. so i add the belowmethod to validate Canadian postal code

Actually, I want to validate the Canadian postal code form field using jQuery validation.

so i add the below method to validate Canadian postal code

//Canada zipcode validation methode 
    jQuery.validator.addMethod("canadazipRegex", function(value, element) {
            return this.optional(element) || /^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$/i.test(value);
    }, "<br>You must enter your postal code in this format: A#A  #A#");

using this regular Expression

^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?开发者_开发百科[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$

the field should allow only this format

A#A #A#

like :A1A 1A1

Not like:A1A1A1

So,How can i make sure white space after 3 characters.

I am not good with regular Expressions. if you give me good tutorial links also appreciated

Thanks in advance !


Don't make the space optional :

^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z]\s[0-9][ABCEGHJ-NPRSTV-Z][0-9]$

Also there are no needs to use quantifier {1}


\x20 matches will spaces. Try using that

0

精彩评论

暂无评论...
验证码 换一张
取 消