I am trying to validate the password using regex with function but its not working for me. can anyone take a look at what is missing or what mistake i have made.?
Here is my code:
function passCheck() {
var passexp = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/;
var password = document.getElementById("password");
if (password != "") {
if (!password.match(passexp)) {
document.registration.password.focus();
document.getElementById('error2').innerHTML = "Password should be atleast 8 character long and must contain at least one digit,at least one lower case,at least one upper case.";
return false;
} else {
document.getElementById('error2').innerHTML = "";
return true;
}
} else {
document.registration.password.focus();
document.getElementById('error2').innerHTML = "Password can't be blank";
return true;
}
}
<label for="password">* Password </label>
<input type="password" name="password" placeholder="Password should be atleast 8 characters" id="password" size="40" required="required" onblur="return passCheck();"/>
<span id="error2"></span>
EDIT:
Hello,
In same form I have few other fields(viz. name,email,password,confirm password,username,mobile no, street, city, country(Select tag), gender(radio button). and now my questions are or rather confusions are:
do I need to check if the field is empty(on both client side and server side) in spite of using 'required' attribute?
I have created new function for each of the field i am trying to validate(just like passCheck() which makes code unnecessarily lengthy. So here re-usability of code is not done. In what other way I can implement validation so as to achieve it. Its bit boring too to implement similar kind of logic or methods again and again with minor changes.
Javascript variable can not have same name as that of function name which contains that variable.? Why? Because when I was trying to use same name,code didn't worked. But when I checked same code on jsfiddle.net it showed me an error for that variable.
Whats the purpose of using PHP functions like mysql_real_escape_string() for non text fields(select box and radio button). Here we are not allowing user to enter anything else but the already defined options only.
Is it good practice to keep one radio button checked for gender.?
I am storing gender in database as CHAR(1) which stores first character of Male/Female. How to store same using ENUM.?
<input>elements such that the checks are carried out by the browser instead of writing a procedure oneself?