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

Regex validation numer and alphabet

So i wanna validating an student name input, that input can:

  • contain alphabets and number, but cant only number
  • and not case senstiive

Example :

  • Wawan Cakra 20
  • 14 James Smith
  • For You Page

code here :

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

name: Yup.string()
  .min(4, "Minimum 4 characters!")
  .max(30, "Maximum 30 characters!")
  .matches(/\b[A-Z][a-z]* [A-Z][a-z]*?\b/, "Only alphabets and numbers are allowed for this field!")
  .required("Name is required!")

>Solution :

I would use the following regex pattern here in case insensitive mode:

^(?=.*[a-z])[a-z0-9 ]{4,30}$

This matches any input which contains at least one character, optional numbers and spaces, and has a length of between 4 and 30 characters.

var inputs = ["Valid", "21 Jump Street", "1234", "abc", "abcdeghijkabcdeghijkabcdeghijka"];
inputs.forEach(x => console.log(x + " => " + /^(?=.*[a-z])[a-z0-9 ]{4,30}$/i.test(x)));
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