jQuery 验证不适用于 Razor
jQuery validate not works with Razor
由于 html 中生成的名称与 jQuery 验证语法不匹配,我无法使我的 Viewmodel 的联系人数据属性正常工作 jQuery 验证。如果我使用 TextBoxFor
,Telephone
的 html 结果是 name="Contact.Telephone"
,但我无法将规则设置为 Contact.Telephone
。
我是不是漏掉了什么?
<input id="Contact_Telephone" class="form-control" type="text" value="" name="Contact.Telephone">
jQuery("#form").validate({
rules: {
Name: "required",
LastName: "required",
SecondLastName: "required",
BirthDate: "required",
Email: {
email:true
},
Telephone: {
digits: true,
maxlength: 10
},
Mobile: {
digits: true,
maxlength: 10
}
},
messages: {
Name: "Dato requerido.",
LastName: "Dato requerido.",
SecondLastName: "Dato requerido.",
BirthDate: "Dato requerido.",
Email: {
email: "Capture un correo válido."
},
Telephone: {
digits: "Capture un dato numérico.",
maxlength:"Longitud máxima permitida de 10 caractéres"
},
Mobile: {
digits: "Capture un dato numérico.",
maxlength: "Longitud máxima permitida de 10 caractéres"
}
}
public class PersonViewModel
{
public int PersonID {get;set;}
public string Name {get;set;}
public string LastName {get;set}
public Datetime BirthDate {get;set;}
public Contact Contact {get;set;}
}
public class Contact
{
public int ContactID {get;set;}
public string Email {get;set;}
public string Mobile {get;set;}
public string Telephone {get;set;}
}
I can't get jQuery Validate working properly for Contact data properties of my Viewmodel due the generated names in the html do not match with the jQuery validate syntax.
它们必须匹配。你必须让它们匹配,否则它永远不会工作。
...the html result for Telephone is name="Contact.Telephone"
, but I can't set the rule as Contact.Telephone
When the name
contains special characters like dots, you must use quotes around the name
...
jQuery("#form").validate({
rules: {
"Contact.Telephone": {
digits: true,
maxlength: 10
}
.....
由于 html 中生成的名称与 jQuery 验证语法不匹配,我无法使我的 Viewmodel 的联系人数据属性正常工作 jQuery 验证。如果我使用 TextBoxFor
,Telephone
的 html 结果是 name="Contact.Telephone"
,但我无法将规则设置为 Contact.Telephone
。
我是不是漏掉了什么?
<input id="Contact_Telephone" class="form-control" type="text" value="" name="Contact.Telephone">
jQuery("#form").validate({
rules: {
Name: "required",
LastName: "required",
SecondLastName: "required",
BirthDate: "required",
Email: {
email:true
},
Telephone: {
digits: true,
maxlength: 10
},
Mobile: {
digits: true,
maxlength: 10
}
},
messages: {
Name: "Dato requerido.",
LastName: "Dato requerido.",
SecondLastName: "Dato requerido.",
BirthDate: "Dato requerido.",
Email: {
email: "Capture un correo válido."
},
Telephone: {
digits: "Capture un dato numérico.",
maxlength:"Longitud máxima permitida de 10 caractéres"
},
Mobile: {
digits: "Capture un dato numérico.",
maxlength: "Longitud máxima permitida de 10 caractéres"
}
}
public class PersonViewModel
{
public int PersonID {get;set;}
public string Name {get;set;}
public string LastName {get;set}
public Datetime BirthDate {get;set;}
public Contact Contact {get;set;}
}
public class Contact
{
public int ContactID {get;set;}
public string Email {get;set;}
public string Mobile {get;set;}
public string Telephone {get;set;}
}
I can't get jQuery Validate working properly for Contact data properties of my Viewmodel due the generated names in the html do not match with the jQuery validate syntax.
它们必须匹配。你必须让它们匹配,否则它永远不会工作。
...the html result for Telephone is
name="Contact.Telephone"
, but I can't set the rule asContact.Telephone
When the name
contains special characters like dots, you must use quotes around the name
...
jQuery("#form").validate({
rules: {
"Contact.Telephone": {
digits: true,
maxlength: 10
}
.....