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?
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
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.
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.
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" .
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
formnovalidateattribute 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> 
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With