javascript电子邮件验证无效

时间:2016-09-06 06:00:20

标签: javascript validation email

我正在使用javascript验证验证我的表单,但它无法正常工作,因为我在控制台屏幕上看到错误

  

“base.js:35 Uncaught SyntaxError:意外的标识符”。

我的javascript如下:

var user = {
signup : function() {
    var firstname = document.signupform.first_name.value;
    var email = document.signupform.email.value;
    var password = document.signupform.password.value;
    var confirmpassword = document.signupform.confirmpassword.value;
    if (firstname == "")
    {
        alert("Please provide first name!")
        document.signupform.first_name.focus();
    }
    else if (!validateEmail())
    {
        alert("Please provide valid email!")
        document.signupform.email.focus() ;
    }
    else if (password == "")
    {
        alert("Please enter a valid password")
        document.signupform.password.focus() ;
    }
    else if (password != confirmPassword) 
    {
        alert("Passwords do not match.");
        document.signupform.confirmpassword.focus() ;
    }
    else
    {
        return true
    }
    return false
}

validateEmail : function()
{
    var emailID = document.signupform.email.value;
    atpos = emailID.indexOf("@");
    dotpos = emailID.lastIndexOf(".");
    if (atpos < 1 || ( dotpos - atpos < 2 )) 
    {
        return false;
    }
    return true;
    },
}

2 个答案:

答案 0 :(得分:3)

,之前,您的代码似乎丢失validateEmail 你应该在最后删除,

正如@nnnnnn在下面的评论中提到的那样,使用validateEmail()将无效 - 请将其替换为this.validateEmail()

请注意,您验证电子邮件的方法不是很好,也传递了无效的电子邮件地址

答案 1 :(得分:-1)

使用以下代码

var user = {
signup : function() {
    var firstname = document.signupform.first_name.value;
    var email = document.signupform.email.value;
    var password = document.signupform.password.value;
    var confirmpassword = document.signupform.confirmpassword.value;
    if (firstname == "")
    {
        alert("Please provide first name!")
        document.signupform.first_name.focus();
    }
    else if (!validateEmail())
    {
        alert("Please provide valid email!")
        document.signupform.email.focus() ;
    }
    else if (password == "")
    {
        alert("Please enter a valid password")
        document.signupform.password.focus() ;
    }
    else if (password != confirmPassword) 
    {
        alert("Passwords do not match.");
        document.signupform.confirmpassword.focus() ;
    }
    else
    {
        return true;
    }
    return false;
},
validateEmail : function()
{
    var emailID = document.signupform.email.value;
    atpos = emailID.indexOf("@");
    dotpos = emailID.lastIndexOf(".");
    if (atpos < 1 || ( dotpos - atpos < 2 )) 
    {
        return false;
    }
    return true;
}

};