【问题标题】:jQuery Validate - Validate contents of stringjQuery Validate - 验证字符串的内容
【发布时间】:2016-03-24 06:04:34
【问题描述】:

我有一个带有文本输入的表单,我想验证输入是否包含特定单词。我已经研究出如何验证specific string,但不仅仅是该字符串中的一个单词。

$.validator.addMethod("exampleword", function(value, element, string) {
 return value === string;
}, $.validator.format("Please enter '{0}'"));

$("#example-form").validate({
 rules: {
   address: {
     required: true,
     exampleword: "Foo"
   }
 },
 messages: {
   address: {
     required: "Please enter an address",
     exampleword: "Please include Foo"
   }
 }
})

【问题讨论】:

    标签: javascript jquery validation


    【解决方案1】:

    您可以为此使用 indexOf 函数。

    indexOf :: 返回字符串在另一个字符串中的位置。如果没有找到,它将返回 -1

    //Assuming  "string" is the word you are looking in your input
    $.validator.addMethod("exampleword", function(value, element, string) {
        return value.indexOf(string) > -1 ;
    }, $.validator.format("Please enter '{0}'"));
    

    请看工作演示https://jsfiddle.net/Prakash_Thete/3tu68rms/

    【讨论】:

      猜你喜欢
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多