【问题标题】:check if string does not contain values检查字符串是否不包含值
【发布时间】:2021-11-07 06:36:14
【问题描述】:

我正在使用 indexOf 来查看电子邮件是否包含特定文本以外的任何内容。

例如,我想检查电子邮件是否在 @ 符号后不包含“usa”,并显示错误消息。

我首先拆分文本并删除@符号之前的所有内容:

var validateemailaddress = regcriteria.email.split('@').pop();

然后,我检查文本是否不包含“usa”:

if(validateemailaddress.indexOf('usa')){
    $('#emailError').show();
} 

上述检查的内容似乎不正确。它有效 - 我可以输入电子邮件,如果它不包含“美国”,则会显示错误消息。

无论如何,当我添加额外的检查时,例如如果电子邮件不包含“可以”,那么无论如何都会显示错误消息。

如下:

if(validateemailaddress.indexOf('usa') || validateemailaddress.indexOf('can')){
    $('#emailError').show();
} 

如上所述,使用上述方法,无论电子邮件是否包含文本,都会显示错误消息。

我要做的就是检查电子邮件是否包含“usa”或“can”,如果没有,则显示错误消息。

我怎样才能做到这一点?

【问题讨论】:

  • 使用includes 而不是indexOf
  • 但是我如何检查电子邮件是否不包含文本?
  • 只是为了解释塞巴斯蒂安所说的,当使用 indexOf 时,你总是得到一个数字,其中 -1 表示它没有找到,它仍然会进入你的 if,使用包含将返回 true 或 false,因此不会输入if,也可以这样做例如:validateemailaddress.indexOf('usa') > -1

标签: javascript jquery validation indexof


【解决方案1】:

这是一个简单的 JavaScript 函数,用于检查电子邮件地址是否包含“usa”或“can”。

function emailValid(email, words) {
    // Get the position of @ [indexOfAt = 3]
    let indexOfAt = email.indexOf('@');

    // Get the string after @ [strAfterAt = domain.usa]
    let strAfterAt = email.substring(indexOfAt + 1);

    for (let index in words) {
        // Check if the string contains one of the words from words array
        if (strAfterAt.includes(words[index])) {
            return true;
        }
    }

    // If the email does not contain any word of the words array
    // it is an invalid email
    return false;
}

let words = ['usa', 'can'];

if (!emailValid('abc@domain.usa', words)) {
    console.log("Invalid Email!");
    // Here you can show the error message
} else {
    console.log("Valid Email!");
}

【讨论】:

  • 这对我有用。非常感谢。
【解决方案2】:

你可以做类似的事情,使用包括:

const validateEmailAdress = (email) => {
  const splittedEmail = email.split('@').pop();
  
  return (splittedEmail.includes('usa') || splittedEmail.includes('can'))
}

console.log("Includes usa: ", validateEmailAdress("something@gmail.usa"))
console.log("Includes can: ", validateEmailAdress("something@gmail.can"))
console.log("Does not includes: ", validateEmailAdress("something@gmail.com"))

【讨论】:

    【解决方案3】:

    有几种方法可以检查字符串是否包含子字符串。

    String.prototype.includes

    'String'.includes(searchString); // returns true/false
    

    String.prototype.indexOf

    // returns values from -1 to last postion of string.
    'String'.indexOf(searchString); 
    
    // In combination with ~ this can work similar to includes()
    // for strings up to 2^31-1 byte length
    
    // returns 0 if string is not found and -pos if found.
    
    ~'String'.indexOf(searchString); 
    

    借助正则表达式:

    // substring must be escaped to return valid results
    new RegExp(escapedSearchString).test('String'); // returns true/false if the search string is found
    
    'String'.match(escapedSearchString); // returns null or an array if found
    

    所以总的来说你可以使用几乎所有的方法,比如:

    if ('String'.function(searchString)) {
      // 'String' includes search String
    } else {
      // 'String' does not include search String
    }
    

    或者在 indexOf 的情况下:

    if ('String'.indexOf(searchString) > -1) {
      // 'String' includes search String
    } else {
      // 'String' does not include search String
    }
    // OR
    if (~'String'.indexOf(searchString)) {
      // 'String' includes search String
    } else {
      // 'String' does not include search String
    }
    

    【讨论】:

      【解决方案4】:

      我相信这个正则表达式匹配就是你要找的

      System.out.println(myString.matches("(.)@(.)usa(.*)"));

      【讨论】:

      • 这不是 JavaScript。
      猜你喜欢
      • 2013-10-26
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 2013-05-18
      • 1970-01-01
      相关资源
      最近更新 更多