Alphanumeric Field

Hi all!

I need a textField that only accepts letter or space-type field. I see Alphanumeric plugin. I dont se any extension. Have you ever need a field like this? Can Yii do this?

Hi, sensorario

The question is, HOW you want to achieve this?

If you don’t want to allow user to even enter invalid characters to textbox, then your choice (this or other jQuery plugin) is best, because Yii has nothing to do about this.

If you want to filter such characters when processing form (in either AJAX or normal form process request), simply add a special filter to your form model (look for rules method - take any auto-generated Yii application as example and look for that method in your models/LoginForm.php or ContactForm.php) just as you would do any other (for example validating e-mail, hash, numbers etc.).



There is the reghexp validator:

array('hotel_phone', 'CRegularExpressionValidator', 'pattern'=>'/^([+]?[0-9 ]+)$/'),

for example this one is for validate a telephone number: it allows a + at the beging and then only numbers.

You can use something like:

array('only_text_field', 'CRegularExpressionValidator', 'pattern'=>'/^([0-9a-z  ]+)$/'),

Only letters, number and spaces.

In such a situation is better to write your own error message in order to be clear about what you want, something like:

errorMessage=>‘Only letters, number and spaces!’