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 :

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)));

Leave a Reply Cancel reply