Following is the regex that I tried to validate against the below mentioned criteria, but in some cases its failing. Let me know what I am doing wrong here.
Regex-
/[a-z]|\d|\_{4, 16}$/.test(username)
Criteria -
Allowed characters are:
- lowercase letters
- Numbers
- Underscore
- Length should be between 4 and 16 characters (both included).
Code
function validateUsr(username) {
res = /[a-z]|\d|\_{4, 16}$/.test(username)
return res
}
console.log(validateUsr('asddsa')); // Correct Output - true
console.log(validateUsr('a')); // Correct Output - false
console.log(validateUsr('Hass')); // Correct Output - false
console.log(validateUsr('Hasd_12assssssasasasasasaasasasasas')); // Correct Output - false
console.log(validateUsr('')); // Correct Output - false
console.log(validateUsr('____')); // Correct Output - true
console.log(validateUsr('012')); // Correct Output - false
console.log(validateUsr('p1pp1')); // Correct Output - true
console.log(validateUsr('asd43 34')); // Correct Output - false
console.log(validateUsr('asd43_34')); // Correct Output - true