I have only had a week to learn JQuery, I still suck at it but getting there.
I created the below code as I do not want to use JQuery Validator Plugging. I needed to create a validation to work with ASP.NET (Web Forms).
This works on Blur (when users leave a field), now I want to disable the button when the form loads and enabled it when all fields have validated.
I tried several things but they never work:
- Check to see if any form element has an error class
- Created a Boolean Variable that evaluates to false if input not valid and use this variable to trigger button
- Add the code to enabled/Disable button directly to each conditional statement
I need to disable the submit button until all fields pass validation, if the fields pass validation and the user goes back and make changes that do not pass validation then the button becomes disable again.
Here is the complete validation code minus all my experiment:
$(document).ready(function () {
$('#txtUserName').blur(function () {
if ($('#txtUserName').val().match(isNumberLetter) && ($('#txtUserName').val().length >= 8)) {
$('#userNameError').removeClass("error").addClass("default");
$('#txtUserName').removeClass("alert");
$('#txtUserName + label').removeAttr("id", "lblUserName");
}
else {
$('#userNameError').removeClass("default").addClass("error");
$('#txtUserName').addClass("alert");
$('#txtUserName + label').attr("id", "lblUserName");
}
});
$('#txtPassword').blur(function () {
if ($('#txtPassword').val().match(isPassword) && ($('#txtPassword').val().length >= 8)) {
$('#passwordError').removeClass("error").addClass("default");
$('#txtPassword').removeClass("alert");
$('#txtPassword + label').removeAttr("id", "lblPassword");
//FLAG USER TO CONFIRM PASSWORD IF INITIAL PASSWORD IS VALID
//----------------------------------------------------------
$('#passwordConfirmError').removeClass("default").addClass("error");
$('#txtConfirmPassword').addClass("alert");
$('#txtConfirmPassword + label').attr("id", "lblConfirmPassword");
}
else {
$('#passwordError').removeClass("default").addClass("error");
$('#txtPassword').addClass("alert");
$('#txtPassword + label').attr("id", "lblPassword");
//TURN OF PASSWORD REENTRY FLAG IS INITIAL PASSWORD REENTRY IS INVALID
//ONLY REQUEST PASSWORD REENTRY IF INITIAL VALUE IS VALID
//--------------------------------------------------------------------
$('#passwordConfirmError').removeClass("error").addClass("default");
$('#txtConfirmPassword').removeClass("alert");
$('#txtConfirmPassword + label').removeAttr("id", "lblConfirmPassword");
}
});
$('#txtConfirmPassword').blur(function () {
if ((IsValidPassWord == true) && ($('#txtConfirmPassword').val()) === ($('#txtPassword').val())) {
$('#passwordConfirmError').removeClass("error").addClass("default");
$('#txtConfirmPassword').removeClass("alert");
$('#txtConfirmPassword + label').removeAttr("id", "lblConfirmPassword");
}
});
$('#txtEmail').blur(function () {
if ($('#txtEmail').val().match(emailPattern) && ($('#txtEmail').val() != '')) {
$('#emailError').removeClass("error").addClass("default");
$('#txtEmail').removeClass("alert");
$('#txtEmail + label').removeAttr("id", "lblEmail");
$('#emailConfirmError').removeClass("default").addClass("error");
$('#txtConfirmEmail').addClass("alert");
$('#txtConfirmEmail + label').attr("id", "lblConfirmEmail");
}
else {
$('#emailError').removeClass("default").addClass("error");
$('#txtEmail').addClass("alert");
$('#txtEmail + label').attr("id", "lblEmail");
$('#emailConfirmError').removeClass("error").addClass("default");
$('#txtConfirmEmail').removeClass("alert");
$('#txtConfirmEmail + label').removeAttr("id", "lblConfirmEmail");
}
});
$('#txtConfirmEmail').blur(function () {
if ((IsValidEmail == true) && ($('#txtConfirmEmail').val()) === ($('#txtEmail').val())) {
$('#emailConfirmError').removeClass("error").addClass("default");
$('#txtConfirmEmail').removeClass("alert");
$('#txtConfirmEmail + label').removeAttr("id", "lblConfirmEmail");
}
});
$('#ddlSecurityQuestion').blur(function () {
if ($("select option:selected").index() <= 0) {
$('#securityQuestionError').removeClass("default").addClass("error");
$('#ddlSecurityQuestion').addClass("alert");
$('#ddlSecurityQuestion + label').attr("id", "lblSecurityQuestion");
}
else {
$('#securityQuestionError').removeClass("error").addClass("default");
$('#ddlSecurityQuestion').removeClass("alert");
$('#ddlSecurityQuestion + label').removeAttr("id", "lblSecurityQuestion");
}
});
$('#txtSecurityAnswer').blur(function () {
if ($('#txtSecurityAnswer').val().match(isNumberLetterTab)) {
$('#securityAnswerError').removeClass("error").addClass("default");
$('#txtSecurityAnswer').removeClass("alert");
$('#txtSecurityAnswer + label').removeAttr("id", "lblSecurityAnswer");
}
else {
$('#securityAnswerError').removeClass("default").addClass("error");
$('#txtSecurityAnswer').addClass("alert");
$('#txtSecurityAnswer + label').attr("id", "lblSecurityAnswer");
}
});
});
Please, please take a look:
Thank you for helping.