XJ项目新增的时候字段验证两种方式
(1)如果新增是弹出框的形式
例如对电话加格式验证时在前端js中新增的的代码中该属性下方加入验证语句。如下图
{
id : "project_contactsTel",
name : 'contactsTel',
label : '联系电话',
type : "textbox",
rules : {
required : true,
pattern : {
value:Regex.ContactPhone,
message:"请填写正确的联系电话!",
},
}
}
(2)如果新增是页面的形式
例如对联系方式加格式验证时在前端js中新增的的代码下方加入method方法区,在方法区里写上验证方法和语句,并且在保存的方法中调用验证方法。如下图
/*方法区代码*/
methods : {
//验证字段格式方法
verify : function(keyId){
var regPhone = /^1[3|4|5|6|7|8|9]\d{9}$/;
var contactsTel = $("#carrier_add_contactsTel").textbox("getValue");
if (!regPhone.test(contactsTel)) {
Message.error("联系方式不正确!");
return false;
}
var bankCount = /\d{16}|\d{19}/;
var paymentAccount = $("#carrier_add_paymentAccount").textbox("getValue");
if (!bankCount.test(paymentAccount)) {
Message.error("账户格式不正确!");
return false;
}
var isNum = /^[0-9]+\.?[0-9]*$/;
var cooperationYears = $("#carrier_add_cooperationYears").textbox("getValue");
if(cooperationYears != ""){
if (!isNum.test(cooperationYears)) {
Message.error("合作年限为数字!");
return false;
}
}
var isNum = /^[0-9]+\.?[0-9]*$/;
var handlingCost = $("#carrier_add_handlingCost").textbox("getValue");
if(handlingCost != ""){
if (!isNum.test(handlingCost)) {
Message.error("装卸费为数字!");
return false;
}
}
var isNum = /^[0-9]+\.?[0-9]*$/;
var otherCost = $("#carrier_add_otherCost").textbox("getValue");
if(otherCost != ""){
if (!isNum.test(otherCost)) {
Message.error("其他费用为数字!");
return false;
}
}
var isNum = /^[0-9]+\.?[0-9]*$/;
var netProfit = $("#carrier_add_netProfit").textbox("getValue");
if(netProfit != ""){
if (!isNum.test(netProfit)) {
Message.error("毛利率为数字!");
return false;
}
}
return true;
},
//保存时调用代码
carrierSave : function() {
$("body").loading();
var vm = this;
var keyID = vm.parentParam != undefined ? vm.parentParam.keyID: "";
var data = OperateData.getValue(this.item);
data["keyID"] = keyID;
//调用验证方法
if(!vm.verify(keyID)){
$("body").removeLoading();
return;
};
总结:东华新增时验证字段的两种写法