Jquery 检查输入 .val() 是否包含某些字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14999104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:11:29  来源:igfitidea点击:

Jquery check if input .val() contains certain characters

jquery

提问by Ortund

I need to check if an input field on my page contains characters.

我需要检查页面上的输入字段是否包含字符。

This is for very basic email address validation so I only want to check if the text is not empty, and contains @and .characters.

这是用于非常基本的电子邮件地址验证,因此我只想检查文本是否不为空,并且包含@.字符。

I've been trying this way:

我一直在尝试这种方式:

if (($("." + parentname.attr("class") + " #email").val().contains("@")) && ($("." + parentname.attr("class") + " #email").val().contains(".")))
{
    email = 1;
}

Assuming a value of [email protected], this code will throw the following error:

假设值为[email protected],此代码将抛出以下错误:

Object [email protected] has no method 'contains'

对象 [email protected] 没有方法“包含”

So I did some research and found that .contains is for DOM objects, not strings with a suggestion to try this:

所以我做了一些研究,发现 .contains 是用于 DOM 对象,而不是字符串,建议尝试这个:

if (($("." + parentname.attr("class") + " #email").val().IndexOf("@") != -1) && ($("." + parentname.attr("class") + " #email").val().IndexOf(".") != -1))
{
    email = 1;
}

Which results in a similar error:

这会导致类似的错误:

Object [email protected] has no method 'IndexOf'

对象 [email protected] 没有方法“IndexOf”

I'm basically out of ideas here especially considering that the following code works as I want it to in another site I've made:

我在这里基本上没有想法,特别是考虑到以下代码在我制作的另一个站点中可以正常工作:

if ($("#contact-email").val().contains("yahoo.com")) {
    $(".errmsg").text("Domain yahoo.com has been banned due to excessive spam, please use another domain.");
}

Can anyone suggest any other things I could try or, better yet, how to do this properly?

任何人都可以建议我可以尝试的任何其他事情,或者更好的是,如何正确地做到这一点?

采纳答案by toomanyredirects

indexOfdoes not use a capital Iat the start. Try this instead:

indexOf不使用大写字母在开始。试试这个:

if (($("." + parentname.attr("class") + " #email").val().indexOf("@") != -1) && ($("." + parentname.attr("class") + " #email").val().indexOf(".") != -1))
{
  email = 1;
}

回答by red_alert

I have a function to validate emails in Javascript:

我有一个功能来验证 Javascript 中的电子邮件:

function isEmail(emailV){
    if(emailV != null && emailV != undefined){
        var pattern = new RegExp(/^((([a-z]|\d|[!#$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
        return pattern.test(emailV);    
    }
    else{
        return false;
    }

}

It validate all the chars that emails allows, and you only have to call it like this:

它验证电子邮件允许的所有字符,您只需像这样调用它:

if(isEmail($("#contact-email").val())){
   //EMAIL IS VALID
}
else{
   //EMAIL IS NOT VALID
}

回答by Eich

The method is indexOfinstead of IndexOf. If you correct your code it should work like a charm :) .

方法是indexOf代替IndexOf。如果你更正你的代码,它应该像魅力一样工作:)。

Try this code snippet:

试试这个代码片段:

if (($("." + parentname.attr("class") + " #email").val().indexOf("@") !== -1) 
    && ($("." + parentname.attr("class") + " #email").val().indexOf(".") !== -1))
{
    email = 1;
}

回答by Roko C. Buljan

LIVE DEMO

LIVE DEMO

// VISUALIZE ERRORS
function showError(el, err){
    return err ?  $(el).addClass('error') :  $(el).removeClass('error');
}

// REGEXES
var isValidEmailAddress = function( emailAddress ) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
};

// "EMAIL"
function validate_email( el, val ){
    var err = !isValidEmailAddress( val );
    showError(el, err);
}

$('#email').on('input', function(){
  validate_email( this, this.value );
});