简体   繁体   English

jQuery字段验证-是否为空,并且值正确吗?

[英]jQuery field validation - empty or not, and correct values?

I have a relatively simple set of inputs that need correct validation, and I seem to have a hiccup so far. 我有一组相对简单的输入,需要正确的验证,到目前为止,我似乎一直很打ic。 Upon clicking send, only the empty fields should be filled in with the .errorVal class to give them a red background. 单击“发送”后,仅应使用.errorVal类填充空白字段,以使其具有红色背景。 So that is the visual side of the validation. 这就是验证的视觉方面。 Also, I need these fields to have character validation. 另外,我需要这些字段进行字符验证。 So, First and Last name cannot contain numbers. 因此,名字和姓氏不能包含数字。 Email needs to have an @ symbol at some point. 电子邮件有时需要带有@符号。 Any ideas? 有任何想法吗?

http://jsbin.com/OlUGiXE/1/edit?html,css,js,output http://jsbin.com/OlUGiXE/1/edit?html,css,js,输出

$('.sendSupport').click(function () {
    if ($("table.forceCenter > input[type=text]:empty").length == 0 && $("textarea:empty").length == 0) {
        //alert('sent')
        $('.sentVal').fadeIn();
        $('.emptyVal').hide();
        // clear values
        $("input[type=text], textarea").val('').removeClass('errorVal');
    } else {
        //alert('empty')
        $('.emptyVal, .errorStar').fadeIn();
        $('.sentVal').hide();
        $("input[type=text]:empty, textarea:empty").addClass('errorVal');
    }
});

To check if every input is not empty you can do something like this: 要检查每个输入是否都不为空,可以执行以下操作:

jQuery("input[type=text]").each(function() 
    {  

if($(this).val()=="")
 {
 $(this).addClass("errorVal");
}   

});

To check if an input have a number take a look at the link below: 要检查输入是否有数字,请查看下面的链接:

Check whether an input string contains number 检查输入字符串是否包含数字

And if an input contains "@" read this Jquery check if input .val() contains certain characters 如果输入包含“ @”,请阅读此Jquery检查输入.val()是否包含某些字符

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

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