在 Javascript 中检查电子邮件地址

发布于 2024-09-24 18:09:59 字数 761 浏览 5 评论 0原文

我想很多人以前都做过一些类似的开发任务:

我想检查人们的电子邮件地址是否只匹配@tomtom.com或@stream.com。

目前,我想到了两个解决方案:

  1. 使用 indexof() 函数

    var checkTomTomEmail=eo.data.username.indexOf("@tomtom.com");
    var checkStreamEmail=eo.data.username.indexOf("@stream.com");
    
    
    if (checkTomTomEmail > 0 || checkStreamEmail > 0 )
    {
        //运行登录代码
    }
    
    
    别的 
    {
        //请使用您的tomtom或stream电子邮件登录 
    }
    
  2. 使用match

    var patt1=/@tomtom.com/gi;
    var patt2=/@stream.com/gi;
    var checkTomTomEmail=eo.data.username.match(patt1);
    var checkStreamEmail=eo.data.username.match(patt2);
    
    
    if(indexOf(checkTomTomEmail)> 1 ||indexOf (checkStreamEmail)>1)
    {
        //登录 
    }
    

我仍然认为我还没有考虑所有细节。有什么建议吗?
谢谢

I think many people have done some similar development tasks before:

I would like to check the people's email address whether only match @tomtom.com or @stream.com.

Currently, I have two solutions in my mind:

  1. Using indexof() function

    var checkTomTomEmail=eo.data.username.indexOf("@tomtom.com");
    var checkStreamEmail=eo.data.username.indexOf("@stream.com");
    
    
    if (checkTomTomEmail >0 || checkStreamEmail >0 )
    {
        //Run the login code
    }
    
    
    Else 
    {
        //Please login with your tomtom or stream email 
    }
    
  2. Using match

    var patt1=/@tomtom.com/gi;
    var patt2=/@stream.com/gi;
    var checkTomTomEmail=eo.data.username.match(patt1);
    var checkStreamEmail=eo.data.username.match(patt2);
    
    
    if(indexOf(checkTomTomEmail)> 1 ||indexOf (checkStreamEmail)>1)
    {
        //Login 
    }
    

I still think I do not consider all the detail yet. Any suggestions?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单调的奢华 2024-10-01 18:09:59

也许,如果人们只允许输入这两个地址的电子邮件,您应该只收集用户名,然后允许他们使用单选按钮选择@tomtom.com 或@stream.com。

如果您仍然想走 javascript 路线,那么您的正则表达式可以合并到单个语句中

var emailPatt=/@(tomtom|stream).com/gi;

if(emailPatt.test(eo.data.username))
{
    //Login 
}

Perhaps if people are only allowed to enter emails for those two addresses you should only collect the username and then allow them to choose @tomtom.com or @stream.com using radiobuttons.

If you still want to go the javascript route then your regex can be combined into a single statement

var emailPatt=/@(tomtom|stream).com/gi;

if(emailPatt.test(eo.data.username))
{
    //Login 
}
只是偏爱你 2024-10-01 18:09:59

这样怎么样...

var emailRegex = /^([0-9a-z])+@(tomtom|stream)\.com$/ig;
if (emailRegex.test(emailRegex)) {
    // Login
}

我们可以执行 .test(...) 来查看是否有匹配项,而不是执行 .match(...) - 您将得到一个字符串。

此模式保证以下几点:

  1. 电子邮件地址的“用户名”部分必须至少包含一个字符(例如,[电子邮件受保护])
  2. 用户名​​必须由数字或字母组成(大写/小写 - 无关紧要,因为末尾有 /i)
  3. 输入必须包含整个电子邮件地址没有前导或尾部空格。例如,“ [email protected] ”将会失败,它只会接受“[电子邮件受保护]”。)

您可以通过以下方式进一步自定义:也就是说,确保用户名必须至少有 3 个字符,您可以在电子邮件地址中使用下划线或破折号等。

要回答您的问题,这两种解决方案都不起作用。原因:

  1. 用户可以输入“[email protected] Hello”,然后就可以了将通过您的验证。
  2. 特别是在解决方案 #2 中,点“.”是正则表达式保留的字符,这意味着它将匹配任何内容,因此,如果用户输入“@tomtom1com”,它将通过...

有关正则表达式的更多信息:http://www.regular-expressions.info/reference.html

How about this...

var emailRegex = /^([0-9a-z])+@(tomtom|stream)\.com$/ig;
if (emailRegex.test(emailRegex)) {
    // Login
}

Instead of performing a .match(...) - Which you'll get a string back, we can perform a .test(...) to see if anything matches.

This pattern guarantees the following:

  1. The "username" part of the email address must at least have a SINGLE character (For example, [email protected])
  2. Username must be composed of a digit or an alphabet (Upper/Lower case - Doesn't matter because of the /i at the end)
  3. Input must contain the entire email address without leading or tailing spaces. For example, " [email protected] " will fail, it'll only accept "[email protected]".)

You can customize this further by, saying, making sure username must have at least 3 characters, you can use underscore or dashes in the email address, etc.

To answer your question, both solutions won't work. Reasons:

  1. User can enter "[email protected] Hello", and it'll pass both of your validation.
  2. Specifically on solution #2, the dot '.' is a Regex-reserved character, it means it'll match anything, so, if the user enters " @tomtom1com", it'll pass...

More on Regex: http://www.regular-expressions.info/reference.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文