github:https://github.com/52fhy/verify.js
首先引入js,最好拷貝verify整個目錄,因為里面有圖標。
<script src="verify/verify.js"></script>
d:默認提示信息,v:正則,c:正確提示信息,e:錯誤提示信息
使用:
在需要驗證的的輸入框里輸入下面類似內容:
<input type="text" verifys="{d:'請輸入賬戶信息(郵箱或用戶名)',v:{r:/^[0-9A-Za-z-\._]+@\w+(\.(\w){1,3}){1,3}$|^[0-9A-Za-z-_]{3,16}$/,c:'賬戶信息格式正確',e:'賬戶信息格式不正確'}}" name="email" class=""><span id="emailTip" class="error"></span>
注意:name="email"和id="emailTip"對應關系
在公共CSS里加上一句,注意路徑
.error{
height: 25px;
line-height: 25px;
margin: 0px 0px 0px 3px;
padding: 0px 0px 0px 25px;
color: rgb(153, 153, 153);
background: url(../verify/error.gif) no-repeat scroll 0px 6px transparent;
}
完成。
常見驗證規則:
郵箱:
<input type="text" class="field" name="user_email" verifys="{d:'請填寫Email帳號!',v:{r:/^[0-9A-Za-z-\._]+@\w+(\.(\w){1,3}){1,3}$/,c:'恭喜,您的Email帳號可用!',e:'填寫的Email帳號格式不正確!'},a:{u:'do.php?act=user_check_useremail',a:'',c:'恭喜,您的Email帳號可用!',e:'很抱歉,您的Email帳號已被使用!'}}" /><span id="user_emailTip"></span>
用戶名:
<input type="text" class="field" name="user_name"
verifys="{d:'用戶名由3-16位字母、數字與下划線組成',
v:{r:/^[0-9A-Za-z-_]{3,16}$/,c:'恭喜,您的用戶名可用!',e:'填寫的會員用戶名格式不正確!'},
a:{u:'do.php?act=user_check_username',a:'',c:'恭喜,您的用戶名可用!',e:'很抱歉,您的用戶名已被使用!'}}"
/>
<span id="user_nameTip"></span>
密碼:
<input type="password" class="field" name="user_password" verifys="{d:'請填寫6-20位密碼!',v:{r:/^.{6,20}$/,c:'密碼格式正確!',e:'填寫的密碼格式不正確!'}}" /><span id="user_passwordTip"></span>
確認密碼:
<input type="password" class="field" name="user_repassword" verifys="{d:'請填寫確認密碼!',v:{r:/^.{6,20}$/,c:'確認密碼格式正確!',e:'確認密碼格式不正確!'},c:{m:'=',w:'user_password',c:'確認密碼格式正確!',e:'兩次填寫的密碼不一致!'}}" /><span id="user_repasswordTip"></span>
驗證碼:
<input type="text" class="field" name="vericode" verifys="{d:'請填寫驗證碼!',v:{r:/^[0-9a-zA-Z]{5}$/,c:'驗證碼格式正確!',e:'驗證碼格式錯誤!'}}" /><span id="vericodeTip"></span>
單選框:
<input type="checkbox" checked="checked" name="servitems" verifys="{d:'注冊協議',s:{r:'1'}}"/>我已閱讀並同意注冊協議
<span id="servitemsTip" class="error"/>注冊協議</span>
* v:base verify function,c:compare verify function,f:function compare verify function,a:Ajax verify function,s:radio and checkbox input verify function
v:基本驗證方法
c:比較驗證方法
f:比較驗證回調
a:ajax驗證方法
s:單選和多選驗證方法
*
* d:default notice information
* d:默認提示信息
*
* c:success notice information,e:failure notice information
* c:驗證成功提示消息
* e:驗證失敗提示消息
*
* r:regular expression(can not add quotation marks)
* r:正則(不能加引號)
*
* m:compare model,w:the name which compare with the object
* m:比較模型
* w:將要進行比較的對象
*
* u:URL,a:arguments,w:processing notice information
* u:url
* a:參數
* w:正在處理提示消息
