//===============jsp======state==== //開啟驗證 <script type="text/javascript"> yZ();//自定義驗證方法 state $(function () { //設置text需要驗證 $('.easyui-validatebox').validatebox(); }) </script> //自定義驗證規則,需要重寫$.fn.validatebox.defaults.rules中定義的驗證器函數和無效消息。 //例如,intOrFloat為自定義的驗證規則 <input id="addoriginalPrice" class="easyui-textbox" placeholder="輸入價格1" data-options="validType:'intOrFloat'"/> //===============jsp======end==== //====================================js================state=== //自定義驗證方法封裝 /* * @Description 自定義驗證 * * @Author wzf * @Date 2018/9/22 13:03 * @Param * @return **/ function yZ() { $.extend($.fn.validatebox.defaults.rules, { idcard: {// 驗證身份證 validator: function (value) { return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value); }, message: '身份證號碼格式不正確' }, minLength: { validator: function (value, param) { return value.length >= param[0]; }, message: '請輸入至少(2)個字符.' }, length: { validator: function (value, param) { var len = $.trim(value).length; return len >= param[0] && len <= param[1]; }, message: "輸入內容長度必須介於{0}和{1}之間." }, phone: {// 驗證電話號碼 validator: function (value) { return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value); }, message: '格式不正確,請使用下面格式:020-88888888' }, mobile: {// 驗證手機號碼 validator: function (value) { return /^(13|15|18)\d{9}$/i.test(value); }, message: '手機號碼格式不正確' }, intOrFloat: {// 驗證整數或小數 validator: function (value) { return /^\d+(\.\d+)?$/i.test(value); }, message: '請輸入數字,並確保格式正確' }, currency: {// 驗證貨幣 validator: function (value) { return /^\d+(\.\d+)?$/i.test(value); }, message: '貨幣格式不正確' }, qq: {// 驗證QQ,從10000開始 validator: function (value) { return /^[1-9]\d{4,9}$/i.test(value); }, message: 'QQ號碼格式不正確' }, integer: {// 驗證整數 可正負數 validator: function (value) { //return /^[+]?[1-9]+\d*$/i.test(value); return /^([+]?[0-9])|([-]?[0-9])+\d*$/i.test(value); }, message: '請輸入整數' }, age: {// 驗證年齡 validator: function (value) { return /^(?:[1-9][0-9]?|1[01][0-9]|120)$/i.test(value); }, message: '年齡必須是0到120之間的整數' }, chinese: {// 驗證中文 validator: function (value) { return /^[\Α-\¥]+$/i.test(value); }, message: '請輸入中文' }, english: {// 驗證英語 validator: function (value) { return /^[A-Za-z]+$/i.test(value); }, message: '請輸入英文' }, unnormal: {// 驗證是否包含空格和非法字符 validator: function (value) { return /.+/i.test(value); }, message: '輸入值不能為空和包含其他非法字符' }, username: {// 驗證用戶名 validator: function (value) { return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value); }, message: '用戶名不合法(字母開頭,允許6-16字節,允許字母數字下划線)' }, faxno: {// 驗證傳真 validator: function (value) { // return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/i.test(value); return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value); }, message: '傳真號碼不正確' }, zip: {// 驗證郵政編碼 validator: function (value) { return /^[1-9]\d{5}$/i.test(value); }, message: '郵政編碼格式不正確' }, ip: {// 驗證IP地址 validator: function (value) { return /d+.d+.d+.d+/i.test(value); }, message: 'IP地址格式不正確' }, name: {// 驗證姓名,可以是中文或英文 validator: function (value) { return /^[\Α-\¥]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value); }, message: '請輸入姓名' }, date: {// 驗證姓名,可以是中文或英文 validator: function (value) { //格式yyyy-MM-dd或yyyy-M-d return /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29))$/i.test(value); }, message: '清輸入合適的日期格式' }, msn: { validator: function (value) { return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value); }, message: '請輸入有效的msn賬號(例:abc@hotnail(msn/live).com)' }, same: { validator: function (value, param) { if ($("#" + param[0]).val() != "" && value != "") { return $("#" + param[0]).val() == value; } else { return true; } }, message: '兩次輸入的密碼不一致!' } }); } //表單提交根據驗證框的驗證結果決定是否提交 $("#addForm").form("submit", { url: base + "/xxx/xxx", onSubmit: function(){ /* 單獨驗證一個字段: var isValid=$('#passwd').textbox("isValid");//true為驗證通過 驗證表單里所有字段: var isValid = $('#form').form('validate');//所有字段有效返回true*/ return $('#addoriginalPrice').validatebox("isValid");//驗證結果 }, success: function (data) { //如果添加成功 刷新datagrid 關閉對話框 並且清除$("#voteForm").html(""); if (JSON.parse(data).code == "200") $('#sysLogDataGrid').datagrid("reload"); $("#addDiv").dialog("close"); //添加之前先把form表單中的內容清空 $("#addForm").form("clear"); $.messager.alert("提示框",JSON.parse(data).msg , "info"); } else { $.messager.alert("提示框",JSON.parse(data).msg , "info"); } } }); } //====================================js================end===