有没有Javascript LIKE语句?

Eti*_*nne 0 html javascript

我想这样做,但不知道如何在JavaScript中做到这一点.

if (Email == "*aol.com" || Email == "*hotmail*" || Email == "*gmail*" || Email == "*yahoo*") 
    {
     alert("No Hotmail, Gmail, Yahoo or AOL emails are allowed!");  
     return false;
    }
Run Code Online (Sandbox Code Playgroud)

有什么方法吗?

Jam*_*mes 6

首先,我认为您的搜索有点过于通用.(例如,如果有人有电子邮件地址," shotmail@mydomain.com "?

试试这个:

var notAllowed = /@(?:aol|hotmail|g(?:oogle)?mail|yahoo)\.com$/i;
// You may want to cover other domains under .co.uk etc.

if ( notAllowed.test(emailAddress) ) {
    alert("No Hotmail, Gmail, Yahoo or AOL emails are allowed!");  
    return false;
}
Run Code Online (Sandbox Code Playgroud)

我不得不问,你为什么禁止人们使用这些电子邮件地址?(不感兴趣)

此外,这真的应该在服务器端完成(我假设你没有使用SSJS)

  • 我认为你需要教育你的管理层"更好地收集和解释统计数据而不是惹恼你的用户,不允许他们使用他们的个人电子邮件地址. (3认同)
  • 如果你不能在服务器上做,至少知道每个人都可以编辑你的javascript并只是删除测试或更改返回false返回true ...客户端只是不安全. (2认同)