Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Validate that the username contains letters and numbers

I want to validate that the user has letters and numbers
Example:

"pedro123" -> true
"peter" -> false

I have this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function isUserNameValid(username) {
  const res = /(?!.*[\.\-\_]{2,})^[a-zA-Z0-9]{3,24}$/.exec(username);
  const valid = !!res;
  return valid;
}

>Solution :

You can use several regexps:

function isUserNameValid(username) {
  const hasLetter = /[a-zA-Z]/.test(username);
  const hasNumber = /[0-9]/.test(username);
  const isValid = hasLetter && hasNumber;
  return isValid;
}

console.log(isUserNameValid("pedro123"))
console.log(isUserNameValid("peter"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading