jQuery 验证插件:如果不需要的字段中存在错误,则阻止提交
在我的表单中,我有多个电子邮件地址文本输入。
仅需要第一个。
我的问题是,当用户在不需要的字段中输入无效的电子邮件地址时,就会提交表单。
如果不需要的字段中存在错误,我需要阻止表单提交。
我怎样才能做到这一点? 我应该添加自定义规则吗?
这是我的表格中的内容。
<fieldset>
<label for="To"><span>To</span> <input id="To" name="To" type="email" autocomplete="off" maxlength="30" class="required"></label>
<label for="Cc1"><span>Cc</span> <input id="Cc1" name="CC[]" type="email" autocomplete="off" maxlength="30" ></label>
<label for="Cc2"><span>Cc</span> <input id="Cc2" name="CC[]" type="email" autocomplete="off" maxlength="30" ></label>
</fieldset>
以及 jQuery 插件的初始化
$.metadata.setType("attr", "validate");
$().ready(function() {
// validate signup form on keyup and submit
$("#myform").validate();
});
In my form, I have multiples text inputs for email addresses.
Only the first one is required.
My problem is that the form is submitted when the user has typed an invalid email address in a field that's not required.
I need to prevent the form submit if there's an error in a field that's not required.
How can I do that ?
Should I add a custom rule ?
Here's what I have in my form.
<fieldset>
<label for="To"><span>To</span> <input id="To" name="To" type="email" autocomplete="off" maxlength="30" class="required"></label>
<label for="Cc1"><span>Cc</span> <input id="Cc1" name="CC[]" type="email" autocomplete="off" maxlength="30" ></label>
<label for="Cc2"><span>Cc</span> <input id="Cc2" name="CC[]" type="email" autocomplete="off" maxlength="30" ></label>
</fieldset>
And the initialization for the jQuery plugin
$.metadata.setType("attr", "validate");
$().ready(function() {
// validate signup form on keyup and submit
$("#myform").validate();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
email
验证规则添加到您的电子邮件字段。它不是必需的,但如果有值,将检查电子邮件地址是否有效。在 jsfiddle 上查看此工作示例。
Add the
email
validation rule to your email field. It won't be required but will be checked for a valid email address if there is a value.Check this working example on jsfiddle.