S
S
Sunnat Abdukhalilov2019-03-27 11:29:29
Yii
Sunnat Abdukhalilov, 2019-03-27 11:29:29

How to disable Cyrillic textInput yii2?

I want to prohibit the Cyrillic alphabet in the registration form for the login and password fields,
I googled found something but it does not work

['username', 'match', 'pattern' => '/^[^а-яА-Я] $/i'],
['password', 'match', 'pattern' => '/^[^а-яА-Я] $/i'],

even the Latin alphabet does not work with such validation
and I also want to apply the price in the field (this is a different form) tell me how to implement it give an example
number_format($adverts['price'],0,'',' ')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2019-03-27
@f_society

Good morning.
You forgot to add the "u" modifier to work with Cyrillic.
And if you use "i", then you can shorten it to this notation
Well, if you do not need Cyrillic, then why check for the presence of Cyrillic, and not immediately check the Latin? And you can try to remove the restriction on the beginning and end of the line. You can also shorten the notation pattern-a.

// \w соответствует [a-zA-Z0-9_]
['username', 'match', 'pattern' => '/\w+/i'],

It doesn't work because you don't use the repetition metacharacter (if I named it correctly)
With this pattern '/^[^а-яА-Я] $/i', you are looking for only one character, and to find from one or more you need to use "+", like this '/^[^а-яА-Я]+$/i'
PS
To display a custom error message, you can pass an additional parameter to the validation rule
PSS
Here are useful links:
1) Regular expressions for beginners .
2) yii\validators\Validator
3) YII2 Documentation

E
enchikiben, 2019-03-27
@EnChikiben

and so ['username', 'match', 'pattern' => '/^[a-zA-Z]$/i'], ?
https://stackoverflow.com/questions/46448963/valid...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question