RegEx for matching "A-Z, a-z, 0-9, _" and "."

Viewed 248433

I need a regex which will allow only A-Z, a-z, 0-9, the _ character, and dot (.) in the input.

I tried:

[A-Za-z0-9_.] 

But, it did not work. How can I fix it?

6 Answers

You could simply use ^[\w.]+ to match A-Z, a-z, 0-9 and _

regex: /^[a-zA-Z0-9_.]$/i

This works

For me, i was looking for a regex like Gmail has, should allow a-z, A-z, 0-9, _, . only here is the Yup format for it i used:

Email: Yup.string()
      .email("Please Provide Valid Email")
      .matches(
        /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(.\w{2,3})+$/,
        "Only a-z A-Z _ . 0-9 are allowed in this Field"
       )
Related