在項目開發中用到layUI或者easyUI進行前端頁面渲染的時候,對錶單數據會進行相關驗證。此處將easyUI項目的校驗進行總結!
在easyUI框架進行前端頁面渲染時,from表單的提交進行校驗!
//使用easyUI框架,必須將easyUI相關的js導入項目中去
//此處中class="easyui-form" ,表示該表單是easyUI表單
//method="post" ,表示提交方法爲post
//data-options="novalidate:true",表示表單加載時不進行相關校驗
<form id="taskForm" method="post" class="easyui-form" data-options="novalidate:true">
<table style="width:100%;" class="info_table" id="info_table">
<tr>
//此處填寫對應的表單內容
<td align="left"><input style="width: 70%;" type="text" id="ceshi"
name="ceshi" class="easyui-validatebox" required="true" validType="email"/>
</td>
</tr>
</table>
</form>
//from表單提交
$('#taskForm').form('submit', {
url : URL(提交後臺的路徑),
onSubmit: function(){
//提交時進行校驗
return $(this).form('enableValidation').form('validate');
// 將其註釋後,僅僅是表示提交不進行相關校驗
},
success:function(data){
if(data == 'fail'){
$.messager.alert('提示','系統異常,操作失敗!','error');
}else{
$.messager.alert('提示','保存成功!','info');
back();
}
}
});
//此處是對常用的校驗語法
$.extend($.fn.validatebox.defaults.rules, {
CHS: {
validator: function (value, param) {
return /^[\u0391-\uFFE5]+$/.test(value);
},
message: '請輸入漢字'
},
english : {// 驗證英語
validator : function(value) {
return /^[A-Za-z]+$/i.test(value);
},
message : '請輸入英文'
},
ip : {// 驗證IP地址
validator : function(value) {
return /\d+\.\d+\.\d+\.\d+/.test(value);
},
message : 'IP地址格式不正確'
},
ZIP: {
validator: function (value, param) {
return /^[0-9]\d{5}$/.test(value);
},
message: '郵政編碼不存在'
},
QQ: {
validator: function (value, param) {
return /^[1-9]\d{4,10}$/.test(value);
},
message: 'QQ號碼不正確'
},
mobile: {
validator: function (value, param) {
return /^(?:13\d|15\d|18\d)-?\d{5}(\d{3}|\*{3})$/.test(value);
},
message: '手機號碼不正確'
},
tel:{
validator:function(value,param){
return /^(\d{3}-|\d{4}-)?(\d{8}|\d{7})?(-\d{1,6})?$/.test(value);
},
message:'電話號碼不正確'
},
mobileAndTel: {
validator: function (value, param) {
return /(^([0\+]\d{2,3})\d{3,4}\-\d{3,8}$)|(^([0\+]\d{2,3})\d{3,4}\d{3,8}$)|(^([0\+]\d{2,3}){0,1}13\d{9}$)|(^\d{3,4}\d{3,8}$)|(^\d{3,4}\-\d{3,8}$)/.test(value);
},
message: '請正確輸入電話號碼'
},
number: {
validator: function (value, param) {
return /^[0-9]+.?[0-9]*$/.test(value);
},
message: '請輸入數字'
},
money:{
validator: function (value, param) {
return (/^(([1-9]\d*)|\d)(\.\d{1,2})?$/).test(value);
},
message:'請輸入正確的金額'
},
mone:{
validator: function (value, param) {
return (/^(([1-9]\d*)|\d)(\.\d{1,2})?$/).test(value);
},
message:'請輸入整數或小數'
},
integer:{
validator:function(value,param){
return /^[+]?[1-9]\d*$/.test(value);
},
message: '請輸入最小爲1的整數'
},
integ:{
validator:function(value,param){
return /^[+]?[0-9]\d*$/.test(value);
},
message: '請輸入整數'
},
range:{
validator:function(value,param){
if(/^[1-9]\d*$/.test(value)){
return value >= param[0] && value <= param[1]
}else{
return false;
}
},
message:'輸入的數字在{0}到{1}之間'
},
minLength:{
validator:function(value,param){
return value.length >=param[0]
},
message:'至少輸入{0}個字'
},
maxLength:{
validator:function(value,param){
return value.length<=param[0]
},
message:'最多{0}個字'
},
//select即選擇框的驗證
selectValid:{
validator:function(value,param){
if(value == param[0]){
return false;
}else{
return true ;
}
},
message:'請選擇'
},
idCode:{
validator:function(value,param){
return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value);
},
message: '請輸入正確的身份證號'
},
loginName: {
validator: function (value, param) {
return /^[\u0391-\uFFE5\w]+$/.test(value);
},
message: '登錄名稱只允許漢字、英文字母、數字及下劃線。'
},
equalTo: {
validator: function (value, param) {
return value == $(param[0]).val();
},
message: '兩次輸入的字符不一至'
},
englishOrNum : {// 只能輸入英文和數字
validator : function(value) {
return /^[a-zA-Z0-9_ ]{1,}$/.test(value);
},
message : '請輸入英文、數字、下劃線或者空格'
},
xiaoshu:{
validator : function(value){
return /^(([1-9]+)|([0-9]+\.[0-9]{1,2}))$/.test(value);
},
message : '最多保留兩位小數!'
},
ddPrice:{
validator:function(value,param){
if(/^[1-9]\d*$/.test(value)){
return value >= param[0] && value <= param[1];
}else{
return false;
}
},
message:'請輸入1到100之間正整數'
},
jretailUpperLimit:{
validator:function(value,param){
if(/^[0-9]+([.]{1}[0-9]{1,2})?$/.test(value)){
return parseFloat(value) > parseFloat(param[0]) && parseFloat(value) <= parseFloat(param[1]);
}else{
return false;
}
},
message:'請輸入0到100之間的最多倆位小數的數字'
},
rateCheck:{
validator:function(value,param){
if(/^[0-9]+([.]{1}[0-9]{1,2})?$/.test(value)){
return parseFloat(value) > parseFloat(param[0]) && parseFloat(value) <= parseFloat(param[1]);
}else{
return false;
}
},
message:'請輸入0到1000之間的最多倆位小數的數字'
}
});
//校驗方法是放在初始化方法中
//自定義表單校驗規則
$(function(){
$.extend($.fn.validatebox.defaults.rules, {
integ:{
validator:function(value,param){
return /^[+]?[0-9]\d*$/.test(value);
},
message: '請輸入整數'
}
});
});