Yii Framework Forum: Validation For Number - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Validation For Number Rate Topic: -----

#1 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 240
  • Joined: 26-September 12

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?

array('name', 'required'),

Thanks
Aneesh.
0

#2 User is offline   samilo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 231
  • Joined: 04-October 12

Posted 24 January 2013 - 05:12 AM

Hi ,

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

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

0

#3 User is offline   mbala 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 166
  • Joined: 21-July 12
  • Location:Chennai, Tamilnadu, India

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(-).'),


0

#4 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 240
  • Joined: 26-September 12

Posted 24 January 2013 - 05:27 AM

View Postsamilo, 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

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.
Thanks
Aneesh.
0

#5 User is offline   samilo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 231
  • Joined: 04-October 12

Posted 24 January 2013 - 05:31 AM

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

#6 User is offline   MadAnd 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 16-June 11
  • Location:Ukraine

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:

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.
0

#7 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 24 January 2013 - 10:04 AM

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."),
			
		
			
		);


Regards.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users