Experts I need little help here. I have a jsp page where three validation will done:
- Password Strength (using Live Checking, Not validate() function)
- Confirm Password (using validate() function, Not Live Checking)
- Email Validation (using Live Checking, Not validate() function)
Live checking is done by keyUp() function and corresponding jQuery here:
$(document).ready(function(){
$("#register-form").validate({
rules: {
password_again: {
equalTo: "#password"
}
}
});
$("#validate").keyup(function(){
var email = $("#validate").val();
if(email != 0)
{
if(isValidEmailAddress(email))
{
$("#validEmail").css({
"background-image": "url('validate/validYes.png')"
});
} else {
$("#validEmail").css({
"background-image": "url('validate/validNo.png')"
});
}
} else {
$("#validEmail").css({
"background-image": "none"
});
}
});
$('#password').keyup(function(){
$('#result').html(checkStrength($('#password').val()));
});
function checkStrength(password){
//initial strength
var strength = 0;
//if the password length is less than 6, return message.
if (password.length < 6) {
$('#result').removeClass();
$('#result').addClass('short');
return 'SHORT';
}
//length is ok, lets continue.
//if length is 8 characters or more, increase strength value
if (password.length > 7) strength += 1;
//if password contains both lower and uppercase characters, increase strength value
if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) strength += 1;
//if it has numbers and characters, increase strength value
if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) strength += 1;
//if it has one special character, increase strength value
if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1;
//if it has two special characters, increase strength value
if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1;
//now we have calculated strength value, we can return messages
//if value is less than 2
if (strength < 2 ) {
$('#result').removeClass();
$('#result').addClass('weak');
return 'WEAK';
} else if (strength == 2 ) {
$('#result').removeClass();
$('#result').addClass('good');
return 'GOOD';
} else {
$('#result').removeClass();
$('#result').addClass('strong');
return 'STRONG';
}
}
});
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}
All is working fine but I cannot prevent the page forwarding when any of those three validation(Confirm_Password, Password_Strength, Email) is failed after hit the Submit Button except the Confirm_Password validation. I want to restrict the action for the Email & Password_Strength too. Means whenever a validation is failed the set Focus to that field.
Notice that validate() function used only for Confirm_Password.
keyuphandler, and then since you're mostly not using it, remove the jQuery Validate plugin entirely. - Sparky