这是一个工作演示,用于在以分号分隔的单个输入字段中验证多封电子邮件。
为此,我创建了一个自定义 Kendo 验证器规则。DEMO 中的代码片段如下所示:
HTML:
<input type="text" class="k-textbox" name="Email" id="Email" required data-required-msg="Please enter atleast one email" data-multipleemails-msg="Some of the Entered Email ids are invalid"/>
JS:
function validateEmail(email) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
$(function () {
var container = $("#employeeForm");
kendo.init(container);
container.kendoValidator({
rules: {
multipleemails: function (input) {
if (input.is("[data-multipleemails-msg]") && input.val() != "")
{
var emailsArray = input.val().split(";");
for (var i=0; i < emailsArray.length; i++)
{
//alert(emailsArray[i]);
//return validateEmail(emailsArray[i].trim());
if ((emailsArray[i].trim() != "") && (validateEmail(emailsArray[i].trim()) == false))
{
return false;
}
}
}
return true;
}
}
});
});
$('#save').click(function() {
//alert('save');
var validator = $("#employeeForm").data("kendoValidator");
if (validator.validate()) {
alert("Form is successfully validated");
}
});