问题描述
我尝试验证表单,其中输入具有带点的名称属性。根据他们的文档,我可以转义点字符。它似乎有效,但仅当我验证特定输入时才有效。如果我验证整个表单,则不会。插件示例在这里:https://github.com/ansman/validate.js/blob/master/examples.html
我做错了什么?
var constraints = {
"email\\.address": {
presence: true,email: true
}
};
var form = document.querySelector('form'),btnForm = document.querySelector('.validate-form'),btnEmail = document.querySelector('.validate-email');
btnForm.addEventListener('click',function(e) {
e.preventDefault();
handleFormSubmit(form);
});
btnEmail.addEventListener('click',function(e) {
e.preventDefault();
console.log(validate({"email.address": document.getElementById("email").value},constraints));
});
function handleFormSubmit(form,input) {
var errors = validate(form,constraints);
showErrors(form,errors || {});
}
function showErrors(form,errors) {
[].forEach.call(form.querySelectorAll("input[name],select[name]"),function(input) {
showErrorsForInput(input,errors && errors[input.name]);
});
}
function showErrorsForInput(input,errors) {
if (errors) {
[].forEach.call(errors,function(error) {
console.log(error);
});
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/validate.js/0.13.1/validate.min.js"></script>
<form novalidate>
<input type="email" name="email.address" placeholder="Email address" id="email" value="invalid" />
<input type="text" name="first.name" placeholder="First name" />
<input type="text" name="last.name" placeholder="Last name" />
<button class="validate-form">Validate form</button>
<button class="validate-email">Validate email</button>
</form>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)