简体   繁体   English

同时验证多个输入

[英]validating multiple inputs at the same time

I have three text inputs : 我有三个文本输入:

  • First name 名字
  • Last Name
  • User Name 用户名

In the form I have only these inputs are of type: text. 在表格中,我只有以下输入类型:文本。
I want each one of them to be at least 4 characters long so I decided to validate them together. 我希望每个字符至少要有4个字符长,所以我决定一起验证它们。
I want to use Jquery to display an error message that is red when the length is less than 4 and green when it is greater. 我想使用Jquery来显示一条错误消息,当长度小于4时为红色,而当长度大于4时为绿色。 I put three error messages respectively with the following Ids: 我分别用以下ID放置了三个错误消息:

  • flength 长度
  • llength 长度
  • ulength 长度

(the first letter corresponds to the input , example first name : flength and so on) (第一个字母对应于input,示例名字:flength等)
so here is my code to do this: 所以这是我的代码来做到这一点:

$('input [type= text]').keyup(function ({
    var l = $(this).val();
    var x = l.id;
    x = x.charAt(0);
    x = '#' + x + 'length';
    if (l.length < 4) {
        $(x).removeClass('valid').addClass('invalid');
    } else {
        $(x).removeClass('invalid').addClass('valid');
    }
});

Why wouldn't this script work? 为什么该脚本不起作用? what should I modify? 我应该修改什么?
Edit 编辑
demo 演示

After seeing your comments 看到您的评论后

changing var x = $(this).attr("id"); 更改var x = $(this).attr("id"); wont fix the problem too 也不会解决问题

since $(this).attr("id") gives your current element id and your current element is your input tag element and you did not set the id attribute, Instead you have set it to div tags as you have mentioned in your comments, since you are trying to retrieve id attribute which you have not set and your getting an error. 因为$(this).attr("id")给出了您当前的元素ID,而您当前的元素是您的输入标签元素,并且您没有设置id属性,而是将其设置为div标签,就像您在注释中提到的那样,因为您尝试检索尚未设置的id属性,并且收到错误消息。

One solution I could give is this way 我可以给出的一种解决方案是这种方式

<input type="text" name="flength"/> // set name attribute same as div ids
<input type="text" name="llength"/>
<input type="text" name="ulength"/>

 $('input[type=text]').keyup(function ({
    var l = $(this).val(); // get the input string
    var x = $(this).attr('name'); // get the current input element name attribute 
    if (l.length < 4) {
        $('#' + x).removeClass('valid').addClass('invalid');
    } else {
        $('#' + x).removeClass('invalid').addClass('valid');
    }
});

Check this http://jsfiddle.net/Q2y8m/4/ 检查此http://jsfiddle.net/Q2y8m/4/

Try this 尝试这个

$(document).ready(function () {
$('input[type=text]').keyup(function () {
    var l = $(this).val();
    var x = $(this).attr('id'); // notice it is not l.id
    x = x.charAt(0);
    x = '#' + x + 'length';
    if (l.length < 4) {
        $(x).removeClass('valid').addClass('invalid');
    } else {
        $(x).removeClass('invalid').addClass('valid');
    }
});

}); });

Demo http://jsfiddle.net/3yqVg/2/ 演示 http://jsfiddle.net/3yqVg/2/

Try to change var x = l.id; 尝试更改var x = l.id; width var x = $(this).id; 宽度var x = $(this).id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM