'How to check if a input value does / doesnt contain REGEX characters
trying to write a password validator using jquery:
my password element is:
<input type="password" id="passwordTxtBox" class="form-control form-control-lg" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" aria-describedby="passwordHelpBlock" name="Password" required>
my jquery code is:
$("#passwordTxtBox").keyup(function () {
var upperCase = new RegExp('[A-Z]');
var lowerCase = new RegExp('[a-z]');
var numbers = new RegExp('[0-9]');
var special = new RegExp('/[^a-zA-Z0-9- ]*$/');
if ($(this).val().length < 9 && $(this).val().length != 0) {
console.log("not long enough");
}
else if (!$(this).val().match(upperCase)) {
console.log("not uppercase");
}
else if (!$(this).val().match(lowerCase)) {
console.log("not lowercase");
}
else if (!$(this).val().match(numbers)) {
console.log("no numbers");
}
else if (!$(this).val().match(special)) {
console.log("no specials");
}
else if ($(this).val().length >= 8 ) {
console.log("length is fine");
}
else if ($(this).val().match(upperCase)) {
console.log("contains uppercase");
}
else if ($(this).val().match(lowerCase)) {
console.log("contains lowercase");
}
else if ($(this).val().match(numbers)) {
console.log("contains numbers");
}
else if ($(this).val().match(special)) {
console.log("contains specials");
}
});
its important that each console log is fired off separately, so i have to read each regex rule separately
however when I fire this off, only the console.logs for the negative cases fire
i.e console.log("not long enough"); console.log("not uppercase");console.log("no numbers");
etc
the instances where the input does contain those characters, the console logs are never fired.
what am i doing wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|