yii常見字段驗證示例總結。
<?php class ContactForm extends CFormModel{ public $id; public $contact;//聯系人 public $tel;//電話 public $fax;//傳真 public $zipcode;//郵編 public $addr;//地址 public $mobile;//手機 public $email;//郵箱 public $website;//網址 public $qq;//QQ public $msn;//MSN public $add_time;//添加時間 public function rules(){ return array( array('id,add_time','numerical','integerOnly'=>true),//這些字段必須為數字 //關於safe的理解可參考:http://www.phpernote.com/php-template-framework/1163.html array('contact,tel,fax,zipcode,addr,mobile,email,website,qq,msn,add_time','safe') array('contact','required','on'=>'edit','message'=>'聯系人必須填寫.'), array('contact','length','on'=>'edit','min'=>2,'max'=>10,'tooShort'=>'聯系人長度請控制在2-10個字符.','tooLong'=>'聯系人長度請控制在2-10個字符.'), array('tel','match','pattern'=>'/^(\d{3}-|\d{4}-)(\d{8}|\d{7})?$/','message'=>'請輸入正確的電話號碼.'), array('fax','match','pattern'=>'/^(\d{3}-|\d{4}-)(\d{8}|\d{7})?$/','message'=>'請輸入正確的傳真號碼.'), array('mobile','match','pattern'=>'/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/','message'=>'請輸入正確的手機號碼.'), array('email','email','on'=>'edit','message'=>'郵箱輸入有誤.'), array('zipcode','required','on'=>'edit','message'=>'郵編必須填寫.'), array('zipcode','numerical','on'=>'edit','message'=>'郵編是6位數字.'), array('zipcode','length','on'=>'edit','min'=>6,'max'=>6,'tooShort'=>'郵編長度為6位數.','tooLong'=>'郵編長度為6位數.'), array('website','url','on'=>'edit','message'=>'網址輸入有誤.'), array('qq','match','pattern'=>'/^[1-9]{1}[0-9]{4,11}$/','message'=>'請輸入正確的QQ號碼.'), array('msn','email','on'=>'edit','message'=>'MSN輸入有誤.'), ); } }