Rachit Agarwal
Rachit Agarwal

Reputation: 57

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

"base.js:35 Uncaught SyntaxError: Unexpected identifier".

My javascript is as follows:

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

Upvotes: 1

Views: 116

Answers (2)

RNC
RNC

Reputation: 24

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

};

Upvotes: -1

pwolaq
pwolaq

Reputation: 6381

it seems that your code is missing , before validateEmail you should remove , at the end

as @nnnnnn mentioned in the comment below, using validateEmail() won't work - replace it with this.validateEmail()

be advised that your method of validating email is not very good, passing also invalid email addresses

Upvotes: 3

Related Questions