验证多个Name值相同的元素:
$(".send").click(function () {
var a = 0;
var b = 0;
var c = 0;
var d = 0;
//重置name值
$("input[name^='ContactName']").each(
function () {
$(this).attr('name', 'ContactName[' + a + ']');
a++;
})
$("input[name^='ContactTel']").each(
function () {
$(this).attr('name', 'ContactTel[' + b + ']');
b++;
})
$("input[name^='Position']").each(
function () {
$(this).attr('name', 'Position[' + c + ']');
c++;
})
$("input[name^='Email']").each(
function () {
$(this).attr('name', 'Email[' + d + ']');
d++;
})
$("input[name^='ContactName']").each(
function () {
$(this).rules("add", { required: true, messages: { required: "请填写联系人" },});
$(this).rules("add", { maxlength: 50, messages: { maxlength: "联系人最大长度不能大于50" }, });
})
$("input[name^='ContactTel']").each(
function () {
$(this).rules("add", { required: true, messages: { required: "请填写联系方式" }, });
$(this).rules("add", { maxlength: 50, messages: { maxlength: "联系人最大长度不能大于50" } });
$(this).rules("add", { PhoneCheck: true, messages: { PhoneCheck: "请正确填写您的联系方式" } });
})
$("input[name^='Position']").each(
function () {
$(this).rules("add", { required: true, messages: { required: "请填写职务" }, });
})
$("input[name^='Email']").each(
function () {
$(this).rules("add", { MailCheck: true, messages: { MailCheck: "请填写正确的邮件格式" }, });
//$(this).rules("add", { required: true, messages: { required: "请填写联系邮箱" }, });
})
$("#mainform").submit();
$(this).attr('disabled', true);
if (!$("#mainform").valid()) {
$(this).attr('disabled', false);
}
})
})
效果图:
显示元素位置:
1 $("#mainform").validate({
errorPlacement: function (error, element) {
if (element.parent().hasClass("input-group")) {
element.parent().after(error);
}
else
element.after(error)
},
errorPlacement: function (error, element) {
error.appendTo(element.parent()); //直接把错误信息加在验证元素后··
},
rules: {
CustomerName: {
required: true,
maxlength: 50,
remote: {
url: '/crm/Customer/CheckCustomName',
type: 'Post',
data: {
CustomerName: function () { return $("#CustomerName").val() },
id: function ()
{
if ($("#Id").val() == undefined) {
return 0;
} else {
return $("#Id").val();
}
}
}
}
},
OwnerId: {
required:true
},
CustomerType:{
required:true
},
Address:{
required: true,
maxlength: 100
},
IndustryId: {
required: true
},
Capital: {
number: true,
min: 0.001,
maxlength: 8
}
},
messages: {
CustomerName: {
required: '请输入客户名称',
maxlength: '客户名称最大长度为50',
remote:'客户名称不能重复'
},
CustomerType: {
required: '请选择客户类型'
},
Address: {
required: '请输入地址',
maxlength: '客户名称最大长度为50'
},
OwnerId: {
required: '请选择对接人'
},
IndustryId: {
required: '请选择行业'
},
Capital: {
number: '注册资金必须为数字',
min: '注册资金最小为0.001万元',
maxlength:'注册资金最长单位为8位'
}
}
});