Jquery验证检查输入中的单词是否与数组中的相同

时间:2015-03-10 13:16:07

标签: javascript jquery validation

好吧所以我有javascript变量,其中包含来自游戏不同名称等等的冠军数组,我想检查用户是否输入了必须要求的任何名称,这样你就无法提交任何你想提交的表格,它必须是阵中的一个冠军。

这是较小的数组,不是垃圾邮件

var champions = [
            "Aatrox", "Ahri", "Akali"
        ];

以下是表格:

case 2:

$formid = $_POST['formid'];
            $formid += 1;
            $champ_number = $_POST['champ_number'];
            echo '<form id="second_form" action="#" method="POST" style="margin: 0;"  >';
                echo '<input type="hidden" value="'.$formid.'" name="formid">';
                echo '<input type="hidden" value="'.$champ_number.'" name="champ_number">';
                echo '<input type="hidden" name="patch" value="'.$_POST['patch'].'">';
                echo '<h1>Champion names:</h1>';
                for($i=1;$champ_number>=$i;$i++){
                    if($i==1&&$champ_number!=1){
                        echo '<input class="champion" type="text" placeholder="Champion '.$i.'"name="champno'.$i.'" required autofocus><br/>';
                    }
                    elseif($champ_number!=$i){
                        echo '<input class="champion" type="text" placeholder="Champion '.$i.'"name="champno'.$i.'" required><br/>';
                    }
                    elseif($champ_number==1){
                        echo '<input class="champion" type="text" placeholder="Champion '.$i.'"name="champno'.$i.'" required autofocus>';
                    }
                    else{
                        echo '<input class="champion" type="text" name="champno'.$i.'" placeholder="Champion '.$i.'" required>';

                    }
                }

                echo '<br/><input type="submit" value="next">';
            echo '</form>';
        break;

这里还有一个关于它现在如何看的图片:

http://imgur.com/XN1xILK

根据这些建议有一个下一个按钮,我不希望用户能够在没有输入冠军的确切名称或选择一个

的情况下更进一步

2 个答案:

答案 0 :(得分:0)

使用jQuery.inArray()

if ($.inArray("inputValue", champions) !== -1) {
    //entered value is in the array
}

答案 1 :(得分:0)

您可以使用indexOf()检查数组中是否存在元素。

更新:这将检查多个输入并返回它们是否都存在于数组中。

&#13;
&#13;
var champions = ["one", "two", "three"];

//some inputs don't exist in champions
var inputs = ["four", "two", "one"];

var allFound = inputs.every(function(i) {
  return champions.indexOf(i) > -1;
});

console.log(allFound); //false

//All inputs now exist in champions
var inputs = [ "two", "one"];

var allFound = inputs.every(function(i) {
  return champions.indexOf(i) > -1;
});

console.log(allFound); //true
&#13;
&#13;
&#13;

这里的关键是确保indexOf(这是一个职位)的结果确保结果大于-1

更新2:我必须做一些研究才能让这个工作正常,但希望它会做你以后的事情。

&#13;
&#13;
var config = {
  fillAll: true
};

var champions = ["one", "two", "three"];

document.forms["myform"].oninput = function(e) {
  var champion = this["champion"];

  //Force into array
  if (champion[0] === undefined)
    champion = [this["champion"]];

  var reached = {
    valid: 0,
    unique: 0
  };

  var inputs = [].map.call(champion, function(n) {
    return n.value
  }).filter(function(n) {
    return n.length
  });

  var valid = [].every.call(champion, function(n, i) {
    n.setCustomValidity("");
    if (config.fillAll) return (reached.valid = i, champions.indexOf(n.value) > -1);
    else return n.value ? (
      reached.valid = i,
      champions.indexOf(n.value) > -1
    ) : true;
  });

  var unique = inputs.slice(0).sort().every(function(n, i, a) {
    reached.unique = inputs.lastIndexOf(n);
    return n != a[i - 1];
  });

  //Check for valid champions
  if (!valid) {
    champion[reached.valid].setCustomValidity("This is not a valid champion, please correct this field and resubmit.")
  }

  //Check for duplicates
  if (!unique) {
    champion[reached.unique].setCustomValidity("This champion has already been entered.")
  }

  this.checkValidity();
};
&#13;
<form name="myform">
  <input type="text" name="champion" placeholder="Champion 1">
  <input type="text" name="champion" placeholder="Champion 2">
  <input type="text" name="champion" placeholder="Champion 3">
  <input type="submit">
</form>
&#13;
&#13;
&#13;