电子邮件格式验证问题

时间:2017-05-23 12:03:16

标签: jquery asp.net-mvc validation data-annotations

型号代码

var array = [{ id: '124', name: 'qqq', type: 3 }, { id: '589', name: 'www', type: 1 }, { id: '45', name: 'eee', type: 1 }, { id: '567', name: 'rrr', type: 1 }, { id: '124', name: 'qqq', type: 2 }, { id: '589', name: 'ddd', type: 2 }, { id: '45', name: 'qqq', type: 1 }, { id: '567', name: 'vvv', type: 3 }],
    count = array.reduce(function (r, a) {
        return r + +(a.type === 1 || a.type === 3);
    }, 0);
    
console.log(count);

查看代码

[Required(ErrorMessage = "Required.")]
[EmailAddress(ErrorMessage = "Invalid email address.")]
[Remote("checkEmailAvailability", "Users", "Users", AdditionalFields = "RoleId,UserID", ErrorMessage = "Email address already in use")]
public string stEmail { get; set; }

Js Code [AddPatients.js]

@Html.TextBoxFor(model => model.stEmail, null, new { @class = "form-control" })

<script src="@Url.Content("~/assets/global/plugins/jquery.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/assets/global/plugins/jquery-validation/js/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Areas/Users/Scripts/AddPatients.js")" type="text/javascript"></script>

控制器代码

$("#btnAddPatient").click(function (event) {
    event.preventDefault();
    var $form = $('#frmAddPatient');
    if ($form.valid()) {//some code
    }
    }
});

我面临的问题是,当用户在电子邮件地址文本框中输入“abc @ gmail”时,它会从“ $ form.valid()”传递,但“ ModelState.IsValid '返回错误的 [“无效的电子邮件地址。”] 以获取相同的值,因此用户无法发布表单。

所以有人能告诉我我的代码或解决方案有什么不妥吗? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试使用Html.EditorFor辅助方法而不是Html.TextBoxFor。希望能帮忙,我的朋友:))