Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional rules for HTML5 form validation

Tags:

html

webforms

I am wondering if it's possible to set an element "required" based on some other element state. For instance, say I have an input element that I want to be "required" if and only if the user checks a specific checkbox. First, is this possible to do it without the use of JavaScript? Second, if we use JavaScript, of course one could set up a rule / validation if the user checks that checkbox, but what about consistency regarding the default browser validation errors? Would it be "correct" to add the "required" attribute to the element once the checkbox is clicked? I don't see an alternative here. What are your thoughts on this one?

like image 933
Jonas Avatar asked Apr 15 '12 21:04

Jonas


People also ask

Does HTML5 have form validation?

Using HTML5, we can create a form with built in validation (i.e. no javascript required). Earlier, we were using JAVASCRIPT to control form validation. These form controls are meant for both Desktop, tablets and smart phones.

How do I bypass HTML5 validation?

To ignore HTML validation, you can remove the attribute on button click using JavaScript. Uer removeAttribute() to remove an attribute from each of the matched elements.

Which are the correct input restrictions used for validation in HTML5?

The value must be greater than or equal to the value. There must be a value (if set). Unless the step is set to the any literal, the value must be min + an integral multiple of the step. The number of characters (code points) must not be less than the value of the attribute, if non-empty.

Which are two HTML5 attributes that can be used to validate form data?

There are three categories of HTML5 form validation features: HTML attributes on <input> , <select> , and <textarea> elements. (From here on out, I will just refer to them all as <input> cause brevity) CSS pseudo selectors.


1 Answers

I don't think you can do this without javascript.

With javascript, you could do this easily enough.

I would add the required attribute only after the checkbox is clicked.

In fact, I would make this a toggled piece, so that the user can uncheck and unrequire the input.

However, I would not rely on browser's implementation of the required attribute. Instead, I would create my own required notification using some CSS (highlighting, animation, displaying some words).

like image 54
Jason Gennaro Avatar answered Oct 17 '22 20:10

Jason Gennaro