How To Validate Contact Number With Regex

Here i am showing how to validate contact number with regex.

==>IN your model do this------

public function rules()

{

return array(array(‘emergency_contact’,‘contactMatch’,‘contact’=>self::MATCH),

);

}

==>Now i m write a function called ‘contactMatch’

const MATCH= 1;

public function contactMatch($emergency_contact,$params) 


{


 if(!empty($this->emergency_contact))


 {


 if ($params['contact'] === self::MATCH)


    $pattern = '/^([+]{0,1}?[0-9.-]+)$/';  


	


if(!preg_match($pattern, $this->$emergency_contact))


  $this->addError($emergency_contact, 'Number not valid,You can use only combination of [0-9.-] & at the start point can use +');	


}


}

==>By this program you don’t need to get error message after press save button. when you write false format, it will

show message instantly.

==> Remember uncomment Ajex validation in ur controller.

==> Now run ur programe & enjoy it!!!!

It’s not clear what’re you trying to do.

Is it some kind of ‘tutorial’? then it’s a bad one.

Regexp check can be implemented using built-in ‘match’ validator.


array('emergency_contact', 'match', 'pattern' => '/...some regexp here.../')

but i do it & work correctly

i used it on my project. plz read post properly

i m not used build match…it’s my custom validation