validationTelephone number

How to validate telephone number(must be 10 digit and numeric number)? Here is my model file

public function rules()

{


	return array(


		array('telephone','length','max'=>10),


		array('telephone','required'),


	);


}

You could add the ‘numerical’ validator and be done with it:




public function rules()

{

 return array(

  array('telephone','length','max'=>10),

  array('telephone', 'numerical', 'integerOnly'=>true),

  array('telephone','required'),

 );

}



However, note that when you treat a telephone number like an actual number you may lose any leading zeroes in the number. Since you’re probably never going to do calculations with a telephone number, it may be better to treat it like a string and perform a regex check on it using the match validator:




public function rules()

{

 return array(

  array('telephone','required'),

  array('telephone', 'match', 'pattern'=>'^[\d]{10}$'),

 );

}



You should doublecheck the above regex by the way, I typed it up without verifying :)

Hello,

array(‘telephone’, ‘match’, ‘pattern’=>’/^(\D*)?(\d{3})(\D*)?(\d{3})(\D*)?(\d{4})$/’),

This is working.

Thanks

Is it? Wouldn’t that still validate a string like ‘xxxxxx000%%%%%%000xxxxxxxx0000’? I hardly believe thats a valid phone number.

If you want to allow separation by dashes or spaces I’d go with something like this:




/^(\d{3}[ -]?){2}\d{4}$/



Here’s a nice site where you can test your regular expressions on the fly: http://www.gskinner.com/RegExr/

How do you expect your phone numbers to be formatted?

Thanks Buddy. Working Fine. I have posted another problem, ( checkbox array value). Can you plz help me how to get checkbox array value.