Validation For Number

For form input field ‘name’, input will allow alphabet, combination of alphabet and number (abcsd, nabhdj123) . but it not allows the numbers as input (12345). How add validation rule in Yii model for this condition?

array('name', 'required'),

Hi ,

I had no idea why not worked with you but you can try this will help

array('name', 'match', 'pattern'=>'/^([a-z0-9_])+$/'),

Try this

By using this pattern, combination(alphabet+number) will not work.

Its solved by adding a validate function in model by checking the input is number.


Why not worked ?!! you can use username like this name1212 , or i understood you in wrong way :)

Hi guys,

the samilo’s pattern is somewhat incomplete, because it allows only lower case letters. More sane solution is:

array('name',  'match', 'pattern'=>'/^[\w][\w\d_]*$/'),

So now only names which begins with an alphabet char will pass the validation. You could read more about these escape sequences, as well as about PCRE in official documentation.

Dear Friends

Here is my attempt.

I hope it would help.

public function rules()


		return array(

			array('name', 'match', 'pattern'=>'/^[0-9]*[a-zA-Z_]+[a-zA-Z0-9_]*$/','message'=>"Name should contain letters not mere numbers."),






use like this in Model
[[‘name’], ‘string’],