表单验证某些输入必须互不相同 [英] Form validate some input must be different from each other

查看:138
本文介绍了表单验证某些输入必须互不相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在验证表单时遇到了一些问题。
我想要做的是检查一些输入字段是否相等,如果为真则停止表单输入。
像这样:

 < form id =myForm> 
< input type =textid =id1/>
< input type =textid =id2/>
< input type =textid =id3/>
< br />
< input type =textid =id4/>
< input type =textid =id5/>
< input type =textid =id6/>
...其他一些输入(id7,id8,id9 - id10,id11,id12 - ...)
< p id =errorhidden =hidden>
错误
< / p>
< input id =submittype =submitvalue =submit/>
< / form>

如果id1,id2和id3具有相同的值(按相同顺序)id4,id5 id6必须停止表单输入。
Id1,id2和id3可以具有相同的值,并且Id4可以具有相同的id1值,因此一个,但是三个一起(id1,id2,id3)不能具有相同的id4值, id5,id6。
例如,一个错误是:


  • id1:One

  • id2: / li>
  • id3:三张

  • id4:一张

  • li> id6:Three



所有其他情况都可以。

我正在尝试这样的事情: http://jsfiddle.net/g2kBm/283/ 但它不起作用。

解决方案

http://jsfiddle.net/g2kBm/284/



您不需要on模糊事件,因为您正在检查提交。此外,您需要检查#1 ==#4 AND#2 ==#5 AND#3 ==#6:

 <$ c $ $('#id1')。val()== $('#id4)$($#$ my $) ').val()&& 
$('#id2')。val()== $('#id5')。val()&&
$('# ();
$(#error)。show();
errors = true;
} else {
errors = false;
$(#error)。hide();
}
if(error){
event.preventDefault() ;
}
});


I'm having some problems validation my form. What I want to do is to check if some input field are equals, and stop the form input if true. Something like this:

<form id="myForm">
<input type="text" id="id1" />
<input type="text" id="id2" />
<input type="text" id="id3" />
<br/>
<input type="text" id="id4" />
<input type="text" id="id5" />
<input type="text" id="id6" />
...some other inputs (id7, id8, id9 - id10, id11, id12 - ...)
<p id="error" hidden="hidden">
Error
</p>
<input id="submit" type="submit" value="submit" />   
</form>

If id1, id2 and id3 have the same value (in the same order) of id4, id5 id6 must stop the form input. Id1, id2 and id3 can have the same value, and Id4 can have the same value of id1, and so one, but the three together (id1, id2, id3) can't have the same values of id4, id5, id6. For example an error is:

  • id1: One
  • id2: Two
  • id3: Three
  • id4: One
  • id5: Two
  • id6: Three

All the other cases are fine.

I'm trying something like this: http://jsfiddle.net/g2kBm/283/ but it's not working.

解决方案

http://jsfiddle.net/g2kBm/284/

You don't need the on blur event, as you're checking on submit. Also, you need to check if #1 == #4 AND #2 == #5 AND #3 == #6:

$('#myForm').submit(function (event) {
    var errors = false;
    if ($('#id1').val() == $('#id4').val() &&
        $('#id2').val() == $('#id5').val() &&
        $('#id3').val() == $('#id6').val()) {
            $("#error").show(); 
            errors = true;
    } else { 
      errors = false; 
      $("#error").hide();
    }
    if (error) {
      event.preventDefault();
    }
});

这篇关于表单验证某些输入必须互不相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆