Password Reconfirmation

Secure password validation is very important when creating a user system. Imagine if users could have insecure passwords without a character limit. Many user’s would have extremely insecure password and this is why we need password validation.

 

The code bellow is quite easy to follow. Make sure you read the comments if you get lost.

function validate_pwd(){
    password = document.getElementById(“pass_new”).value;
    confirmPassword = document.getElementById(“repass_new”).value;
    errors.innerHTML = “<ul>”;
   var error = 0
    if (password.length < ‘8’ ) {
        errors.innerHTML = errors.innerHTML + “<li> Passwords must be at least 8 characters long”;
        error = 1;
    }
    if(!(password == confirmPassword)){
        errors.innerHTML = errors.innerHTML + “<li> Passwords is not match</li>”;
 error = 1;
 }
    if (!password.match(/[a-z]/)) {
        errors.innerHTML = errors.innerHTML + “<li> Passwords must contain one lowercase letter</li>”;
        error = 1;
    }
    if (password.match(/[A-Z]/)) {
        errors.innerHTML = errors.innerHTML + “<li> Passwords must not contain one uppercase letter</li>”;
        error = 1;
    }
    if (!password.match(/[1-9]/)) {
        errors.innerHTML = errors.innerHTML + “<li> Passwords must contain one number </li>”;
 error = 1;
    }
    // If error is equal to one there must be an error
    if(error == 1){
        // Finish of the list for proper html
  document.getElementById(‘errors’).style.visibility=”visible”;
  document.getElementById(‘errors’).style.display=”block”;
        errors.innerHTML = errors.innerHTML + “</ul>”;
        // Do not allow submit the form as we have had an error
 return false;
    // IF error is not equal to 1 then there was no errors
     } else {
  errors.innerHTML =”;
 // Allow the submit to take place
 return true;
    }
}
<div id=”errors”> </div>

To get this validation to work all you need to do is add onSubmit="return validate()" into your form tag. Add this code to where you want your errors to be displayed

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: