1

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;
    },
}
Tharif
  • 13,794
  • 9
  • 55
  • 77
  • The code shown has invalid object literal syntax. In the console did it show a line number as a hyperlink? If so, click on it and it should highlight the specific identifier in question... – nnnnnn Sep 06 '16 at 06:02
  • There are many topics on internet to valid email. You can check this answer for example where is nice regex which you can just easy copy paste http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – Andurit Sep 06 '16 at 06:02

2 Answers2

3

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

pwolaq
  • 6,343
  • 19
  • 45
-1

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

};
RNC
  • 24
  • 5