1

I'm using jquery validate plugin to validate my php form, now i'm stuck with validating for a custom word, what i want to do is, disable user to input two words, for example:

word1, word2.

Is there any simple way to do it, i tried with some custom methods, but i have no idea what am i doing. Is there anyone able to provide me one?

custom: {
required: true,
words: "word1,word2",
},

And in my controller

custom: function( value, element ) {
return this.optional(element) || /^-?(?:\word1,word2\)?$/.test(value);
    },
Sparky
  • 98,165
  • 25
  • 199
  • 285
Jogn Smit
  • 87
  • 1
  • 12

1 Answers1

0

You need to add a method for words (same as 'words:') to declare a custom function which jqvalidate will use. This should be added before you call jQuery.validate();

        jQuery.validator.addMethod("words", function(value, element) {
return this.optional(element) || /^/(?!Hello)(?!World)$/.test(value);
    }, "Only Hello and World are allowed");

See validator AddMethod for reference. No controller code is required.

Try to search for answers within stackoverflow.

Community
  • 1
  • 1
crazydiv
  • 812
  • 9
  • 30
  • I don't need what you made mate, i need to disallow words such as Hello, Word, not only allow them, so i basically need opposite of what you made. – Jogn Smit Aug 12 '13 at 09:40
  • 1
    Try adding a not in the regex. it will do the opposite. – crazydiv Aug 12 '13 at 09:59
  • Do you mind writing it in the regex, i'm feeling dumb on this one. Thanks. I understand the addmethod method tho. – Jogn Smit Aug 12 '13 at 11:32