0

This is my code. It functions perfectly up to the password validation. It completely ignores my null test and goes straight to the actual validation but then refuses to go past it and keeps giving me my invalid password alert. Any ideas as to how to fix this issue?

function validate(){

    var l = document.getElementById('lname').value;
    var f = document.getElementById('fname').value;
    var e = document.getElementById('email').value;
    var e2 = document.getElementById('cemail').value;
    var u = document.getElementById('newuser').value;
    var p = document.getElementById('newpass');
    var p2 = document.getElementById('cnewpass');

    var str = new RegExp(/[a-zA-Z]{1,30}$/);
    var em = new RegExp(/[a-z0-9._-]+@[a-z]+\.[a-z]{1,30}$/);
    var pass = new RegExp(/[a-zA-Z0-9]{1,15}$/);    

    if (l == null || l == ""){
        alert("Please enter your last name");
        return false;
    }
    var ln = str.test(l);   
    if(ln==false){
        alert("Invalid Name.");
        documents.forms['registration']['lname'].focus;
        return false;
    }

    if (f == null || f == ""){
        alert("Please enter your first name");
        return false;
    }
    var fn = str.test(f);
    if(fn==false){
        alert("Invalid Name.");
        documents.forms['registration']['fname'].focus;
        return false;
    }

    if (e == null || e == "") {
        alert("Please enter an email address");
        return false;
    }
    if (e2 == null || e2 == "") {
        alert("Please enter an email address");
        return false;
    }
    var eml = em.test(e);
    if(eml==false){
        alert("Invalid Email.");
        documents.forms['registration']['email'].focus;
        return false;
    }
    var eml2 = em.test(e2);
    if(eml2==false){
        alert("Invalid Email.");
        documents.forms['registration']['cemail'].focus;    
        return false;
    }   
    if(e2!=e){
        alert("Please ensure that the emails match.");
        return false;
    }

    if (u == null || u == "") {
        alert("Please enter a user name");
        return false;
    }
    var un = str.test(u);   
    if(un==false){
        alert("Invalid user name");
        documents.forms['registration']['newuser'].focus;
        return false;
    }   

    if (p == null || p == "") {
        alert("works");
        alert("Please enter a password");
        return false;
    }
    if (p2 == null || p2 == "") {
        alert("Please enter a password");
        return false;
    }   
    var pwrd = pass.test(p);    
    if(pwrd==false){
        alert("Invalid Password.");
        documents.forms['registration']['newpass'].focus;
        return false;
    }

    if(p2!=p){
        alert("Please ensure that the passwords match");
        documents.forms['registration']['cnewpass'].focus;
        return false;
    }

}
2
  • 1
    You are passing the input element, not the value, during regEx test. Commented May 8, 2015 at 3:19
  • wow did not see that and I've been staring at this code for two days now....thanks much much ^^ Commented May 8, 2015 at 3:20

2 Answers 2

1

You should amend js code that retriving data from from. Look,

var p = document.getElementById('newpass');
var p2 = document.getElementById('cnewpass');

Here You are trying to get NOT values of input tags, you are trying to get tag. So You should replace above code with:

var p = document.getElementById('newpass').value;
var p2 = document.getElementById('cnewpass').value;

I hope it will help you

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, i cant believe I didn't notice that on my own...I guess that's what happens when you're staring at the same piece of code for too long...the brains starts auto completing making errors difficult to find.
0

You should pass the value of password fields.

try changing the code to

    var p = document.getElementById('newpass').value;
    var p2 = document.getElementById('cnewpass').value;

1 Comment

thanks, i cant believe I didn't notice that on my own...I guess that's what happens when you're staring at the same piece of code for too long...the brains starts auto completing making errors difficult to find.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.