I am trying to validate a username. I want to have a regex that,
- At least 4 characters long.
- Must contain only letters, numbers and an optional underscore
- It must not end with an underscore
i have tried regex,
/^(?!_*\_*\_*)[A-Za-z]([A-Za-z0-9_]*[A-Za-z0-9])?$/
function validate(username) {
if ((/^(?!_*\_*\_*)[A-Za-z]([A-Za-z0-9_]*[A-Za-z0-9])?$/).test(username)) {
return true;
}
return false;
}
console.log(validate('hej_gd'));