Validator For Positive Numbers Only

Hi

I need to use in my form just positive numbers so I used numberPattern’=>’^[+]?\d+([.]\d+)?$’ but not worked .

this is my Model :


	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('count,user_id', 'required','message'=>'يرجى ملئ حقل {attribute}  '),

			array('count, user_id', 'numerical','numberPattern'=>'^[+]?\d+([.]\d+)?$', 'integerOnly'=>true,'message'=>'Error '),

			array('count', 'length','max'=>5 ,'min'=>1,'tooShort'=>'عدد الأحرف أقل من {min} '),

       	


        	// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('st_id, count, price, b_date, user_id', 'safe', 'on'=>'search'),

		);

	}

How to fix it ?

Thanks in advance

You can use this website to check if your regext is correct: http://www.regex101.com/

Why not just this:




array('count, user_id', 'numerical', 'integerOnly'=>true, 'min'=>0),



Alternatively, min could be 1.

I’m not sure why you’re using a regex which matches decimals when you’re specifying that only integers should be accepted though. If you’re intending to match decimal values, you should remove ‘integerOnly’=>true .

Positive = those float numbers which >0

‘min’=>0 property would allow 0, that is incorrect so we need additional custom validator for != 0.

Thanks all , it’s work now with Keith soultion .

[b]

[/b]

[b]

[/b]

We can also use like this -


array('count', 'length', 'min' => 1, 'max'=>5, 

'tooShort'=>Yii::t("translation", "{attribute} is too short."),

'tooLong'=>Yii::t("translation", "{attribute} is to long.")),

			

array('count', 'match', 'pattern'=>'/^[0-9]+$/'),