I'm working on a form validation script. I tried some of the prebuilt validation scripts out there, but they didn't behave how I wanted, so I'm making one from scratch. For the most part it's working smooth, but I think I might have a misunderstanding of how variables are scoped and the best way to declare them.
Please keep in mind my code below is 'paraphrased' and stripped so only relevant lines are used.
In the beginning of the script, I declare error vars for each field, and each respective possible error. For instance, I have error vars for if the password entered is too short or too long. I start by declaring them equal to '0' and if later in the script, an error is determined, it is set to '1'. Finally, I have a section that makes sure all error vars are 0 before submitting form.
In this segment, when the users changes focus away from the password field, I get the value of the password form element, and first check to see if it's blank. If it is, I set the '$registerPasswordBlankError' equal to '1'. If it is not blank, I then check to see if it's less than 4 characters. If it is not blank but is less than 4 characters, I set the '$registerPasswordShortError' equal to 1.
The problem is, since the code doesn't declare the 'registerPasswordShortError' in the focusOut function, it seems that the var is unDefined, even though I defined it earlier in the script.
In other words, even though Ive declared the error vars in the beginning of the script, the focusout function doesn't know about them and sees them as unDefined if I try to reference them without again declaring them.
Simply put, even though I declared '$registerPasswordShortError' earlier in the script, if it's not declared again within the focusOut function, and I try and reference it with:
'alert($registerPasswordShortError);'
The popup will say 'undefined' even though I set it to 0 earlier.
Please keep in mind my code below is 'paraphrased' and stripped so only relevant lines are used.
In the beginning of the script, I declare error vars for each field, and each respective possible error. For instance, I have error vars for if the password entered is too short or too long. I start by declaring them equal to '0' and if later in the script, an error is determined, it is set to '1'. Finally, I have a section that makes sure all error vars are 0 before submitting form.
Code:
$(document).ready(function() {
var $registerPasswordBlankError = 0;
var $registerPasswordShortError = 0;
var $registerPasswordLongError = 0;
var $registerPasswordMatchError = 0;
In this segment, when the users changes focus away from the password field, I get the value of the password form element, and first check to see if it's blank. If it is, I set the '$registerPasswordBlankError' equal to '1'. If it is not blank, I then check to see if it's less than 4 characters. If it is not blank but is less than 4 characters, I set the '$registerPasswordShortError' equal to 1.
The problem is, since the code doesn't declare the 'registerPasswordShortError' in the focusOut function, it seems that the var is unDefined, even though I defined it earlier in the script.
In other words, even though Ive declared the error vars in the beginning of the script, the focusout function doesn't know about them and sees them as unDefined if I try to reference them without again declaring them.
Code:
$('#registerPassword').on('focusout', function() {
var checkregisterPassword=$(this).val();
if(checkregisterPassword == ''){var $registerPasswordBlankError = 1;}
else {
var $registerPasswordBlankError = 0;
if(checkregisterPassword.length < 4) {var $registerPasswordShortError = 1;}
else {var $registerPasswordShortError = 0;}
}
}
Simply put, even though I declared '$registerPasswordShortError' earlier in the script, if it's not declared again within the focusOut function, and I try and reference it with:
'alert($registerPasswordShortError);'
The popup will say 'undefined' even though I set it to 0 earlier.
Last edited: