简体   繁体   中英

JQuery Validation plugin: input textbox content validation

I'm using the JQuery Validation plugin http://jqueryvalidation.org/ and I have the following scenario:

Example HTML:

   <input type="text" name="glass_name"  id="glass_name">
   <input type="text" name="glass_name_confirm"  id="glass_name_confirm">

Example Javascript:

      $("#form").validate({
        rules: {
            glass_name: {
                required: true
            },
            glass_name_confirm: {
                required: true,
                equalTo: "#glass_name"
            }, 

How can I validate the input box content against a string words array?. For instance, I don't want that the user in the textbox id=glass_name insert a list of innapropiate words: word1 or word2 or word3, etc.

Thanks in advance!

Make a custom rule:

//Add new method:

jQuery.validator.addMethod("validationFilterWords", function(value, element) {
var arrFilter = ["Word1","Word2"];
    return this.optional(element) || ($.inArray(value,arrFilter) === -1);
}, "* Not allowed");

// Add new rule

 rules : {
        glass_name: {
                      validationFilterWords: true,
                      required: true

                     }
    }

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