Javascript regular expression to validate user name size

Asked

Viewed 435 times

3

I need a regular Javascript expression that meets the following rules:

  • Between 2 and 255 characters
  • In low box (Lower case)
  • Contain only alphanumerics, underline (_), dot (.) or hyphen (-).
  • tip, this site is very good for testing your regex and tbm lets you save and share them. http://www.regexpal.com/

  • 2

    Need to allow accented characters?

2 answers

3

Try this to capture exact line:

/^[a-z0-9_\.-]{2,255}$/

Play by the rules:

  • Between 2 and 255 characters
  • In low box (Lower case)
  • Contains only alphanumerics, unerline (_), dot (.) or hyphen (-).

1

The regex looks like this:

/^[a-z0-9_\.-]{2,255}$/
  • 2

    William, two small corrections: the character -(dash) should be at the end of the list, otherwise it will be interpreted as an interval, similar to a-z. And if I’m not mistaken there’s no need to escape .(dot) inside the brackets[].

  • Good! I did it in my head. You’re right, corrected.

  • Must escape the point.

  • Special characters (such as the dot (.) and the asterisk(*)) it has no meaning when it is within a character set. It does not need to use escape in them. But using exhaust will also work. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-Character-set

Browser other questions tagged

You are not signed in. Login or sign up in order to post.