Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bypass HTML "required" attribute when submitting [duplicate]

I use required for a 1st check before submitting a form.

<form action="myform.php">   Foo: <input type="text" name="someField" required="required">   <input type="submit" value="submit">   <input type="submit" value="ignore"> </form> 

Problem is, that I have an "ignore" button, which submits the form as well and the backend logic then sets a correct state in a DB. In this case (ignore) the validation is not desired (because there is no need to fill in field "Foo").

What's the best way to handle the 2 scenarios?

  1. I am aware that I could give up "required" and do the validation somewhere else (backend / JavaScript). I am looking for a way to keep the "required".
  2. I could use some Js onclick for the ignore button scenario and remove the attribute just before sending the form ( https://stackoverflow.com/a/13951500/356726 ).

But actually I am looking for something smarter ....

--- Edit ---

Yes, duplicate of Required attribute HTML5

like image 475
Horst Walter Avatar asked Sep 10 '13 17:09

Horst Walter


People also ask

How do I remove required attributes?

To remove the required attribute, select the element and call the removeAttribute() method on it, passing it required as a parameter, e.g. input. removeAttribute('required') . The removeAttribute method will remove the required attribute from the element.

How do you bypass a field in Google forms?

g_form. checkMandatory = false; this will bypass the mandatory check and the form will be submitted. You can use in a client script (onsubmit) or in a client side UI action.

What is Formnovalidate?

The formnovalidate attribute is a boolean attribute. When present, it specifies that the <input> element should not be validated when submitted. The formnovalidate attribute overrides the novalidate attribute of the <form> element. Note: The formnovalidate attribute can be used with type="submit" .


Video Answer


2 Answers

No JavaScript, no second form needed, and the validation can stay:

For exactly this use case the HTML5 spec has designed the formnovalidate attribute for submit elements (like <input type=submit>), as one of the attributes for form submission:

The formnovalidate attribute can be used to make submit buttons that do not trigger the constraint validation.

So simply put

<form action="myform.php">   Foo: <input type="text" name="someField" required>   <input type="submit" value="submit">   <input type="submit" value="ignore" formnovalidate> </form> 
like image 148
Bergi Avatar answered Sep 17 '22 16:09

Bergi


Your #2 is a common approach. Something like:

$('input[value="ignore"]').click(function() {     $(this).siblings('input[type="text"]').removeAttr('required'); }); 

Might have to use mousedown to beat submit.

like image 36
isherwood Avatar answered Sep 16 '22 16:09

isherwood