Price validation

Hello everyone,

I couldn’t find any price validation for Yii yet so I’ve written a regular expression for validating a price field.

Component:




<?php

class PriceValidator extends CRegularExpressionValidator

{

        // 12,10 OR 12.10

	public $pattern = '/^[¥£$€]?[ ]?[-]?[ ]?[0-9]*[.,]{0,1}[0-9]{0,2}[ ]?[¥£$€]?$/';

}

?>



Rules:




array('price', 'PriceValidator'),



This expression checks if the value is in the format of 12,10 OR 12.10

Feel free to use and comment if you have any improvements/suggestions.

Well

Price still can be a negative value… and it can have a currency symbol at the beginning or at the end… and for some needs (bookkeeping application) can be on 3 or even 4 decimal places

Thanks for the input, I edited the negative value and currency symbols in, I hadn’t included them because my site is only for use in Holland.

What do you mean exactly? Prices going into the millions? like for example $ 1.000.000 ?

I meant on decimal places… but this is used only for calculations… for display it’s always only two decimal places… now the pattern looks better :)