代码之家  ›  专栏  ›  技术社区  ›  Etienne

没有类似Javascript的语句吗?

  •  0
  • Etienne  · 技术社区  · 15 年前

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

    有办法吗?

    4 回复  |  直到 15 年前
        1
  •  6
  •   James    15 年前

    首先,我觉得你的搜索有点太泛化了(e、 g.如果有人有电子邮件地址怎么办 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;
    }
    

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

    而且,这确实应该在服务器端完成(我假设您没有使用SSJS)

        2
  •  3
  •   Konerak    15 年前

    使用JavaScript REGEXP对象。阅读 W3schools page their tutorial 举几个例子。如果您对其中任何一个都有问题,只需在此处发布,我们将详细介绍:)

    var Email = 'testmail@yahoo.com';
    
    var mail_pattern=new RegExp("/aol.com|hotmail|gmail|yahoo/");
    if (mail_pattern.test(Email)) {
     alert("No Hotmail, Gmail, Yahoo or AOL emails are allowed!");  
    }
    else {
    alert("mail ok")
    }
    
        3
  •  0
  •   Bashar Abdullah    15 年前

    最好的方法是REGEXP。快速简单的东西也许是方法的索引,但它是如此的有限。

    http://www.quirksmode.org/js/strings.html#indexof

        4
  •  0
  •   Sune Rasmussen    15 年前

    (我有点暗示你对JavaScript不太了解)

    function validateEmail (email) {
        // Checks if whatever variable submitted is a string.
        // This line is NOT useful if you're sure of whatever being sent to the function,
        // but otherwise it'll save your ass for a runtime error.
        if (email.constructor != String) return false;
    
        // I'm not sure the search patterns are 100 % (they probably ain't),
        // but the @'s should make the search less generic.
        var emailsNotAllowed = ["@aol.com", "@hotmail.", "@live.", "@gmail.", "@google.", "@yahoo."];
        // Convert the email address to lower case, so you're sure nothing is going wrong with the patterns.
        email = email.toLowerCase();
        // Loop through all the patterns, and check if one of them matches the email address provided.
        for (var i = 0; i < emailNotAllowed.length; i++) {
            // The indexOf method will return zero if the string could not be found.
            // Otherwise, it will return a positive number, which in JavaScript validates to a true condition.
            if (email.indexOf(emailsNotAllowed[i])) {
                alert("No Hotmail, Gmail, Yahoo or AOL emails are allowed!");
                return false;
            }
        }
    }