Answer the question
In order to leave comments, you need to log in
A global variable changes in a nested function, but outside remains the same?
The global variable does not change outside the nested function, you need to use the checkName variable in another function, but console.log returns false. Although the validation is successful. Please tell me what could be the reason?
var checkName = false;
$('.userName').blur(function() {
if($(this).val() != '') {
var pattern = /^[a-zA-Z]+$/;
if(pattern.test($(this).val())){
checkName = true;
$('.nameValid').css({'display' : 'none'});
} else {
$('.nameValid').css({'display' : 'block'});
$('.nameValid').text('Invalid name!');
}
} else {
$('.nameValid').css({'display' : 'block'});
$('.nameValid').text('Name must be filled!');
}
});
console.log(checkName);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question