RegEx Registration

lazarote

web enthusiastic
Collaborate
Registered
Joined
Jun 24, 2019
Messages
142
Points
53

Reputation:

Hello guys, I have a question. How could I use this function:

1643064436332.png


so that users when registering, their usernames can only have letters of the Latin alphabet, and numbers (do not allow emojis, spaces or weird characters)
 

Soft4Win

Developer
Staff member
Moderator
Collaborate
Registered
Joined
Apr 27, 2019
Messages
370
Points
103

Reputation:

You can use it in various ways.

Let's suppose, you want to ensure that any user who is registering on your website, should only use alphabets in their username, then you can use this regular expression:
Code:
/^[a-zA-Z]+/g

Now, if you want to make sure, that it should only contain, Alphabets, Numbers and Underscore. Then this is the expression you can use.
Code:
^[a-zA-Z0-9_]*$

Explanation:
^ : start of string
[ : beginning of character group
a-z : any lowercase letter
A-Z : any uppercase letter
0-9 : any digit
_ : underscore
] : end of character group
* : zero or more of the given characters
$ : end of string

So, similarly you can make various combinations, Regex is very powerful, but a bit trick to understand at first. You can use some websites to generate the perfect regex for yourself.

https://regexr.com/
https://regex101.com/

Make sure you test by registering yourself, to ensure the regex is working properly, because a mis-configured regex configuration, can prevent your users from registering.

Hope this helps you.

Have a Great Day
 
Top