tp5 php正则邮箱,TP5验证器使用实例
TP5驗(yàn)證器建立模塊\validate\驗(yàn)證器文件名
例如我們定義了一個(gè)驗(yàn)證USER的驗(yàn)證器類<?php
namespace?app\admin\validate;
use?think\Validate;
class?Admin?extends?Validate{
protected?$rule?=?[
'username'?=>?'require|min:3|max:16|unique:admin',
'password'?=>?'require|min:6|max:32',
];
protected?$message?=?[
'username.require'?=>?'管理員名稱不能為空',
'password.require'?=>?'管理員密碼不能為空',
'username.min'?=>?'管理員名稱最少應(yīng)為3位',
'username.unique'?=>?'管理員名稱已經(jīng)存在',
'password.min'?=>?'管理員密碼最少應(yīng)為6位',
'username.max'?=>?'管理員名稱不能超過16位',
'password.max'?=>?'管理員密碼不能超過32位',
];
protected?$scene=[
'edit'?=>?['username'],
'add'?=>?['username','password'],
];
}
驗(yàn)證器的使用在需要驗(yàn)證的地方可以使用//驗(yàn)證數(shù)據(jù)
$validate?=?Loader::validate('Admin');
if(!$validate->scene('edit')->check($data)){
$this->error($validate->getError());die;
}
擴(kuò)展正則在驗(yàn)證器類里的使用方法<?php
namespace?app\index\validate;
use?think\Validate;
class?User?extends?Validate{
//?正則自定義驗(yàn)證手機(jī)方法
protected?$regex?=?[?'mobile'?=>?'/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/'];
protected?$rule?=?[
'username'?=>?'require|min:6|max:16|unique:user',
'password'?=>?'require|min:6',
//數(shù)組的寫法'mobile'?=>?'require|regex:mobile',
'mobile'?=>?['require','regex'=>'mobile'],
'mail'?=>?'require|email',
'captcha'?=>?'require|captcha',
];
protected?$message?=?[
'username.require'?=>?'用戶名不能為空',
'username.min'?=>?'用戶名至少為6個(gè)字符',
'username.max'?=>?'用戶名最多為16個(gè)字符',
'username.unique'?=>?'用戶名已存在',
'password.require'?=>?'密碼不能為空',
'password.min'?=>?'密碼至少為6個(gè)字符',
'mobile.require'?=>?'手機(jī)號(hào)不能為空',
'mobile.regex'?=>?'手機(jī)號(hào)填寫不正確',
'mial.require'?=>?'E-mail不能為空',
'mial.email'?=>?'E-mail格式不正確',
'captcha.require'?=>?'驗(yàn)證碼不能為空',
'captcha.captcha'?=>?'驗(yàn)證碼不正確',
];
protected?$scene=[
'add'?=>?['username','password','mobile','mail','captcha'],
];
}//?驗(yàn)證規(guī)則
protected?$rule?=?[
'goods_id'?=>?'checkGoodsId',
'goods_name'?=>?'require|min:3|max:150|unique:goods',
'cat_id'?=>?'number|gt:0',
'goods_sn'?=>?'unique:goods|max:20',
'shop_price'?=>?['require',?'regex'?=>?'([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])'],
'market_price'?=>?'require|regex:\d{1,10}(\.\d{1,2})?$|checkMarketPrice',
'weight'?=>?'regex:\d{1,10}(\.\d{1,2})?$',
'give_integral'?=>?'regex:^\d+$',
'is_virtual'?=>?'checkVirtualIndate',
'exchange_integral'?=>?'checkExchangeIntegral',
'is_free_shipping'?=>?'require|checkShipping',
'commission'?=>?'checkCommission'
];
//錯(cuò)誤信息
protected?$message?=?[
'goods_name.require'?=>?'商品名稱必填',
'goods_name.min'?=>?'名稱長度至少3個(gè)字符',
'goods_name.max'?=>?'名稱長度至多50個(gè)漢字',
'goods_name.unique'?=>?'商品名稱重復(fù)',
'cat_id.number'?=>?'商品分類必須填寫',
'cat_id.gt'?=>?'商品分類必須選擇',
'goods_sn.unique'?=>?'商品貨號(hào)重復(fù)',
'goods_sn.max'?=>?'商品貨號(hào)超過長度限制',
'goods_num.checkGoodsNum'?=>?'搶購數(shù)量不能大于庫存數(shù)量',
'shop_price.require'?=>?'本店售價(jià)必填',
'shop_price.regex'?=>?'本店售價(jià)格式不對(duì)',
'market_price.require'?=>?'市場價(jià)格必填',
'market_price.regex'?=>?'市場價(jià)格式不對(duì)',
'market_price.checkMarketPrice'?=>?'市場價(jià)不得小于本店價(jià)',
'weight.regex'?=>?'重量格式不對(duì)',
'give_integral.regex'?=>?'贈(zèng)送積分必須是正整數(shù)',
'exchange_integral.checkExchangeIntegral'?=>?'積分抵扣金額不能超過商品總額',
'is_virtual.checkVirtualIndate'?=>?'虛擬商品有效期不得小于當(dāng)前時(shí)間',
'is_free_shipping.require'?=>?'請(qǐng)選擇商品是否包郵',
];
繼續(xù)擴(kuò)展自定義驗(yàn)證器類namespace?app\index\validate;
use?think\Validate;
class?User?extends?Validate
{
protected?$rule?=?[
'name'??=>??'checkName:thinkphp',
'email'?=>??'email',
];
protected?$message?=?[
'name'??=>??'用戶名必須',
'email'?=>??'郵箱格式錯(cuò)誤',
];
//?自定義驗(yàn)證規(guī)則
protected?function?checkName($value,$rule,$data)
{????????return?$rule?==?$value???true?:?'名稱錯(cuò)誤';
}
}
自定義驗(yàn)證方法可以傳入5個(gè)參數(shù),后面的三個(gè)可以選用順序?yàn)?/p>
驗(yàn)證數(shù)據(jù),驗(yàn)證規(guī)則,全部數(shù)據(jù),字段名,字段描述use?think\Validate;
class?User?extends?Validate{
protected?$rule?=?[
'username'?=>?'require|min:6|max:16|unique:user',
'password'?=>?'require|min:6',
'mobile'?=>?['require','checkMobile'],
'mail'?=>?'require|email',
'captcha'=>'require|captcha',
];
protected?$message?=?[
'username.require'?=>?'用戶名不能為空',
'username.min'?=>?'用戶名至少為6個(gè)字符',
'username.max'?=>?'用戶名最多為16個(gè)字符',
'username.unique'?=>?'用戶名已存在',
'password.require'?=>?'密碼不能為空',
'password.min'?=>?'密碼至少為6個(gè)字符',
'mobile.require'?=>?'手機(jī)號(hào)不能為空',
'mail.require'?=>?'E-mail不能為空',
'mail.email'?=>?'E-mail格式不正確',
'captcha.ruquire'?=>?'驗(yàn)證碼不能為空',
'captcha.captcha'?=>?'驗(yàn)證碼不正確',
];
protected?$scene=[
'add'?=>?['username','password','mobile','mail','captcha',],
];
//自定義驗(yàn)證手機(jī)號(hào)格式
protected?function?checkMobile($value)
{
//檢測手機(jī)號(hào)碼是否合法
if(!preg_match("/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/",$value)){
return?'手機(jī)號(hào)格式不正確';
}else{
return?true;
}
}
}
總結(jié)
以上是生活随笔為你收集整理的tp5 php正则邮箱,TP5验证器使用实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 民生车车信用卡金卡提额快吗
- 下一篇: php值对象模式场景,php设计模式介绍