For the input type text, if i add required attribute, my form won't submit and browser will focus on required field and alert will say please fill this field. For the input type radio, if i add required attibute, my form won't submit but also it does not provide me any alert or focus on the radio which is unchecked. If this is not an in-built functionality for HTML5, can i in some way create it and make it look like the same as it looks for text inputs so that style integrity is also preserved?
-
I think you're looking for this: http://stackoverflow.com/q/8287779/2486583 – James Irwin Mar 07 '17 at 18:24
-
@JamesIrwin i'm looking for more than that. It surely does validate the form but does not give an alert. – Sumit Nalawade Mar 07 '17 at 18:26
-
What validation library are you using? Are you using mvc? Do you have a validation summary? – Harry Mar 07 '17 at 18:27
-
@Harry no library. Just plain HTML/JS. – Sumit Nalawade Mar 07 '17 at 18:32
-
Are you using Safari? If so, there's a bug: https://bugs.webkit.org/show_bug.cgi?id=28649 – Don Rhummy Mar 07 '17 at 18:34
2 Answers
This code works well, if you not select radio, form will not submit. If you select one and enter text in textbox, form will submit.
<form>
<input type="radio" name="one" value="1" required>
<input type="radio" name="one" value="2" required>
<input type="radio" name="one" value="3" required>
<input type="text" name="two" required>
<button>Submit</button>
<form>
Checked on latest version of Google Chrome. May be you found a bug in your browser, try to update it.

- 1,181
- 1
- 13
- 20
-
if you read my question carefully, i'm not denying that the form validation fails, i'm saying it does not give an alert for radio – Sumit Nalawade Mar 07 '17 at 18:38
Beside required
radio button alerts work "perfectly fine" in Chrome...
it makes no sense at all to have an alert for a radio button, that's silly.
If you have a radio button:
- there's absolutely no need to have only one radio button. → Use checkboxes.
- there's absolutely no reason to have all radio buttons unchecked initially.
- one must be checked by default - and it's your job to do so
- logically there's no need to popup alerts like "This radio button is required" - therefore neither to set a
required
attribute to a radio button.
if you still don't understand why... well simple because radios are used as UI switch states. Only one can and must be checked
. If you make them all initially unchecked - and a client unintentionally hits a radio - he's dead in the devil's loop, because once you enter the circle there's no way out. Therefore makes no sense to have all blanks in the first place. You cannot undo... (well, unless you have another silly checkbox or something that says "uncheck all radio buttons!" nonsense).

- 196,159
- 39
- 305
- 313
-
2
-
@MikeMcCaughan sorry I don't believe this *client* stories. in 8Y of front-end web development if I said to my client that his idea is stupid - they always believed me. That's also your job. – Roko C. Buljan Mar 07 '17 at 18:50
-
1Totally agree. I always suggest, that if they must "require" a radio button, that they add a "None" option that is initially checked. Then the validation is that the "None" option is not checked. – Heretic Monkey Mar 07 '17 at 18:56
-
@RokoC.Buljan this is indeed a client story :P otherwise i wouldn't have bothered to think about radio button validation as it's best practice to have one value checked always. – Sumit Nalawade Mar 07 '17 at 19:01
-
@SumitNalawade :) well in that case hope to have unveiled the best practice and helped you to get to (hopefully/eventually) a better UI solution :) – Roko C. Buljan Mar 07 '17 at 21:37
-