简体   繁体   中英

JQuery validate - pattern rule is not working as well as custom regex method

I'm using JQuery Validate plugin to validate if an input contains any spaces or not (No spaces are allowed).

I've tried pattern rule from the additional-methods.js file but it doesn't work at all:

"fos_user_registration_form[plainPassword][first]": {
    required: true,
    minlength: 6,
    pattern: "/^\S*$/"
},

And tried the solution here

"fos_user_registration_form[plainPassword][first]": {
    required: true,
    minlength: 6,
    regex: "/^\S*$/"
},

Bit still it doesn't fire at all when, for example, I type " this is test ".

For anyone who faced the same problem. Actually it was a typo from my side.

Simply you'll have to remove the quotations in the regex. Because RegExp constructor accepts it without quotations. So it'll be:

"fos_user_registration_form[plainPassword][first]": {
    required: true,
    minlength: 6,
    pattern: /^\S*$/
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM