简体   繁体   中英

Regular expression to match letters, numbers, and certain symbols

I need to validate a username in PHP. It can be:

  • Letters (upper and lower case)
  • Numbers
  • Any of these symbols :.,?!@
  • Up to 15 characters OR 16 if the last character is one of the following #$^ (it can also be 15 or less with one of these 3 characters at the end only)

How do I do this?

Start with this:

/^[a-zA-Z0-9:.,?!@]{3,15}[#$^]?$/

then refine it to your needs. Try to see if you need escaping of the special char, but you should get the idea.

This means: from a to z, from A to Z, from 0 to 9 and :.,?!@ repeated from 3 to 15 times, optionally followed by one among #$^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM