简体   繁体   English

HTML5浏览器验证-1个表单,2个按钮,需要更改验证

[英]HTML5 Browser Validation - 1 form, 2 buttons, need to change validation

I have a form with two buttons - one is a "submit" button at the end of the form, and in the middle of the form I have an "Add" button that uses Javascript to add hidden input elements within the form whenever it's clicked. 我有一个带有两个按钮的表单-一个是表单末尾的“提交”按钮,在表单中间,我有一个“添加”按钮,该按钮使用Javascript在每次单击时在表单内添加隐藏的输入元素。

Here are the two input fields/add button: 这是两个输入字段/添加按钮:

<input name="name" required>
<input name="email" required type="email">
<button type="submit">Add</button>

And then another set of input fields: 然后是另一组输入字段:

<input name="title" required>
<button type="submit">Submit</button>

And these are all within one form. 这些都是一种形式。

I want HTML5 browser validation to fire on the "name" and "email" fields when I click "Add" (but not the "title" field) and the browser validation to fire on the "title" field (but not the "name" and "input" fields) when I click "Submit." 我希望在单击“添加”(而不是“标题”字段)时在“名称”和“电子邮件”字段上触发HTML5浏览器验证,并在“标题”字段(而不是“名称”上)触发浏览器验证和“输入”字段),然后点击“提交”。 Is there any way to accomplish this? 有什么办法可以做到这一点?

You can add or remove attribute "required" to the fields to which you required by 您可以将属性“必需”添加或删除到您需要的字段

                $('#field_id').attr('required','true');
                $('#field_id').removeAttr('required');

The only way I see is to set the required attributes (or: properties) dynamically on-click. 我看到的唯一方法是在点击时动态设置required属性(或属性)。

Or you can add and remove event listeners for invalid , which seem to suppress the native "missing"/"wrong format" notice - even if they do nothing (like preventDefaultAction or so). 或者,您可以添加和删除invalid事件的事件侦听器,这些事件侦听器似乎抑制了本机的“丢失” /“格式错误”通知-即使它们不执行任何操作(例如preventDefaultAction左右)。

I also tried buttons with the formnovalidate attribute and manually checkValidity() on the elected elements, but even though that fires "invalid"-events no native dialogue is shown and the submit is not cancelled. 我还尝试了带有formnovalidate属性的按钮,并在当选元素上手动执行checkValidity() ,但是即使触发“无效”事件,也不会显示任何本机对话,并且提交也不会被取消。 (tested everything with opera) (用歌剧测试了一切)

Is there any particular reason that you want to use HTML5 to validate your form in the first place? 首先,您是否有任何特定原因想要使用HTML5来验证表单? I feel like what you need would be easily accomplished using Javascript, and you noted that your add button would be using javascript anyway. 我觉得使用Java脚本可以轻松实现所需的功能,并且您注意到添加按钮无论如何都将使用javascript。 Also, why would your form validation to be partitioned in such an odd way? 此外,为什么您的表单验证会以这种奇怪的方式进行分区?

I don't even like the HTML5 validation in the first place. 我什至不喜欢HTML5验证。 For example, if you type in "asdf@1" into an HTML5 email input, it will accept it. 例如,如果您在HTML5电子邮件输入中键入“ asdf @ 1”,它将接受它。 Now, you can make the argument that that's technically a valid email address, but I think in practice most people would agree that they wouldn't accept that as a valid email address. 现在,您可以提出一个论点,即从技术上讲这是一个有效的电子邮件地址,但我认为实际上大多数人都同意他们不会接受该地址为有效的电子邮件地址。 You could use an IP address in place of the domain but I highly doubt that you could use that as an email to log into any modern web page. 您可以使用IP地址代替域,但是我非常怀疑您可以将其用作电子邮件登录任何现代网页。

But I digress. 但是我离题了。 To answer your question, you could write a quick function with JQuery that would override the form validation based on which button was clicked. 要回答您的问题,您可以使用JQuery编写一个快速函数,该函数将基于单击哪个按钮来覆盖表单验证。 You would do this by catching the "invalid" error thrown by the HTML5 validation for that particular input and returning false to get around it. 您可以通过捕获HTML5验证针对该特定输入抛出的“无效”错误并返回false来解决该问题,来实现此目的。 Therefore, when you clicked submit you could override the name and email form validation, and vice versa for when you click the add button. 因此,当您单击“提交”时,您可以覆盖名称和电子邮件表单验证,对于单击“添加”按钮而言,反之亦然。 Again, I have no idea why you would want to do this but it is a solution. 同样,我不知道您为什么要这样做,但这是一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM