array('name', 'required'),
Page 1 of 1
Validation For Number
#1
Posted 24 January 2013 - 04:43 AM
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?
#2
Posted 24 January 2013 - 05:12 AM
Hi ,
I had no idea why not worked with you but you can try this will help
I had no idea why not worked with you but you can try this will help
array('name', 'match', 'pattern'=>'/^([a-z0-9_])+$/'),
#3
Posted 24 January 2013 - 05:26 AM
Try this
Quote
/** Username validation in yii model **/
array('username', 'match' ,'pattern'=>'/^[A-Za-z0-9_]+$/u',
'message'=> 'Username can contain only alphanumeric characters and hyphens(-).'),
array('username', 'match' ,'pattern'=>'/^[A-Za-z0-9_]+$/u',
'message'=> 'Username can contain only alphanumeric characters and hyphens(-).'),
#4
Posted 24 January 2013 - 05:27 AM
samilo, on 24 January 2013 - 05:12 AM, said:
Hi ,
I had no idea why not worked with you but you can try this will help
I had no idea why not worked with you but you can try this will help
array('name', 'match', 'pattern'=>'/^([a-z0-9_])+$/'),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.
Thanks.
#5
Posted 24 January 2013 - 05:31 AM
Why not worked ?!! you can use username like this name1212 , or i understood you in wrong way
#6
Posted 24 January 2013 - 06:56 AM
Hi guys,
the samilo's pattern is somewhat incomplete, because it allows only lower case letters. More sane solution is:
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.
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.
#7
Posted 24 January 2013 - 10:04 AM
Dear Friends
Here is my attempt.
I hope it would help.
Regards.
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."),
);
Regards.
Share this topic:
Page 1 of 1

Help















