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!!!!