Require at least one alpha character

We have a login form that allows you to enter your user_id or your player_tag . We have a model with the following rules:

 protected $rules = ['player_tag' => 'required|unique|min:3|max:15|regex:/^[a-zA-Z0-9_]+$/']; 

Is there any way to add a rule requiring the player_tag field player_tag contain at least 1 alpha character ( a-zA-Z )?

+5
source share
3 answers

This may help you:

 ^(?=.*[a-zA-Z]).+$ 

Here is a working example: https://regex101.com/r/gD3gR6/2

+4
source

If there should be one alpha character at the beginning of the field, you can simply expand your regex to check it at the beginning of char:

 /^[a-zA-Z][a-zA-Z0-9_]*$/ 

To require at least one alpha character without a specific position, simply use the following regular expression:

 /^[a-zA-Z0-9_]*[a-zA-Z][a-zA-Z0-9_]*$/ 
+1
source

Try this way

 /^\d*[a-zA-Z][a-zA-Z0-9]*$/ 

Explanation:

1. 0 or more digits;

2. At least 1 character; // this will provide your at least one alpha condition

3. 0 or 1 alphanumeric characters;

0
source

Source: https://habr.com/ru/post/1243594/


All Articles