简体   繁体   English

javascript电子邮件验证不起作用

[英]javascript email validation is not working

I am validating my form using javascript validation but it is not working as I see an error on console screen 我正在使用javascript验证来验证表单,但无法正常工作,因为我在控制台屏幕上看到错误

"base.js:35 Uncaught SyntaxError: Unexpected identifier". “ base.js:35 Uncaught SyntaxError:意外标识符”。

My javascript is as follows: 我的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;
    },
}

it seems that your code is missing , before validateEmail you should remove , at the end 似乎您的代码丢失了,validateEmail之前应该删除,最后

as @nnnnnn mentioned in the comment below, using validateEmail() won't work - replace it with this.validateEmail() 就像下面评论中提到的@nnnnnn一样,使用validateEmail()无效-用this.validateEmail()替换

be advised that your method of validating email is not very good, passing also invalid email addresses 建议您验证电子邮件的方法不是很好,同时还要传递无效的电子邮件地址

Use the below Code 使用以下代码

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;
}

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM