使用jquery验证验证两个单词

时间:2014-01-31 06:27:48

标签: javascript jquery jquery-validate

我有一个输入字段,用户可以输入任何单词。我希望通过jquery验证这个词。下面是我的js代码。我正在正确加载js验证。

现在我想要验证StringA和StringB(也就是说,如果用户在任何情况下输入任何单词,它应该验证,否则将给出错误消息)。我该怎么做两个单词..下面是第一个单词

我的js如下

$(document).ready(function () {

jQuery.validator.addMethod("weekText", function(val) {
    var string1 = "StringA";
    if(val.toLowerCase()==string1.toLowerCase()){
        return true;
    }
    return false;
    }, "Please enter the valid correct code word.");

$('#submitDetails').validate({
    rules: {
        entry: {
            required: true,
            weekText: true
        },
        // 2. Validation fail messages
        messages: {
            entry: {
                required: "You must enter correct code word.",
                weekText: "Please enter the valid correct code word"
            }
       }
    });

1 个答案:

答案 0 :(得分:0)

试试这个,

$(document).ready(function () {

$.validator.addMethod("weekText", function(val) {
    var strings = ["StringA","StringB"];
    flag = false
    for(i=0;i<strings.length;i++){
  if(val.toLowerCase()==strings[i].toLowerCase()){
        flag=true;
    };

    };
    if(flag){
        return true;
    }
    else{
    return false;
    };
    }, "Please enter the valid correct code word.");

$('#submitDetails').validate({
    rules: {
        entry: {
            required: true,
            weekText: true
        }
    },
        // 2. Validation fail messages
        messages: {
            entry: {
                required: "You must enter correct code word.",
                weekText: "Please enter the valid correct code word"
            }
       }
    });

});

演示http://jsfiddle.net/dhana36/hzSvc/