Display HTML5 error message/validation on hidden radio/checkbox - css

I have CSS-customized radio buttons, which have required validation. The default radio buttons are hidden by CSS rule.
When I submit the form, the default validation message is also hidden.
Form looks like this
How can I display default error message (like: Please select one of the options) while disabling radio buttons? (Pure HTML/CSS without using Js)
input[type="radio"] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
visibility: hidden;
}
input[type="radio"]:checked +label {
border: 1px solid blue;
}
<h3>Gender</h3>
<input id="male" type="radio" name="gender" value="male" required>
<label for="male">Male</label>
<input id="female" type="radio" name="gender" value="female" required>
<label for="female">Female</label>
<input id="other" type="radio" name="gender" value="other" required>
<label for="other">Rather not to say</label>

as #joostS said, hidden radio button will not trigger native error message. for that we need to hide it using opacity. I have created sample example.
Also it will not trigger validation until we submit the form by clicking on submit button. If you need validation on "onChange" event of any form elements, then we need to use jQuery or Javascript solution to achieve that.
I hope it will be helpful.
label {
display: inline-block;
border: 2px solid #83d0f2;
padding: 10px;
border-radius: 3px;
position: relative;
}
input[type="radio"] {
opacity: 0;
position: absolute;
z-index: -1;
}
input[type="radio"]:checked +label {
border: 1px solid #4CAF50;
}
input[type="radio"]:invalid +label {
}
<h3>Gender</h3>
<form>
<input id="male" type="radio" name="gender" value="male" required>
<label for="male">Male</label>
<input id="female" type="radio" name="gender" value="female" required>
<label for="female">Female</label>
<input id="other" type="radio" name="gender" value="other" required>
<label for="other">Child</label>
<input type="submit" />
</form>

You could try using the CSS :invalid pseudo-class. Use this HTML:
<label for="male">Male</label>
<input id="male" type="radio" name="gender" value="male" required>
<span class="error-message">Please select on of the options</span>
And the following CSS:
input[name=gender] + .error-message { display: none; } // or whatever you wanna do with it
input[name=gender]:invalid + .error-message { display: block; }
MDN documentation about :invalid

Here you go...
body {font-size: 15px; font-family: serif;}
input {
background: transparent;
border-radius: 0px;
border: 1px solid black;
padding: 5px;
box-shadow: none!important;
font-size: 15px; font-family: serif;
}
input[type="submit"] {padding: 5px 10px; margin-top: 5px;}
label {display: block; padding: 0 0 5px 0;}
form > div {margin-bottom: 1em; overflow: auto;}
.hidden {
opacity: 0;
position: absolute;
pointer-events: none;
}
.checkboxes label {display: block; float: left;}
input[type="radio"] + span {
display: block;
border: 1px solid black;
border-left: 0;
padding: 5px 10px;
}
label:first-child input[type="radio"] + span {border-left: 1px solid black;}
input[type="radio"]:checked + span {background: silver;}
<form>
<div>
<label for="name">Name (optional)</label>
<input id="name" type="text" name="name">
</div>
<label>Gender</label>
<div class="checkboxes">
<label><input id="male" type="radio" name="gender" value="male" class="hidden" required><span>Male</span></label>
<label><input id="female" type="radio" name="gender" value="male" class="hidden" required><span>Female </span></label>
<label><input id="other" type="radio" name="gender" value="male" class="hidden" required><span>Other</span></label>
</div>
<input type="submit" value="Send" />
</form>
Although I like the minimalistic approach of using native HTML5 validation, you might want to replace it with Javascript validation on the long run. Javascript validation gives you far more control over the validation process and it allows you to set real classes (instead of pseudo classes) to improve the styling of the (in)valid fields. This native HTML5 validation can be your fall-back in case of broken (or lack of) Javascript. You can find an example of that here, along with some other suggestions on how to make Better forms, inspired by Andrew Cole.

Related

General Sibling Selector CSS working on test but not on website

My goal is to create some radio buttons with a nice look, and for that I need to stylize the label when the radio is checked. After some research, I found that ~ can work as a conditional with a :checked option.
I created a codepen to test my idea first, and it work like a charm, see here: https://codepen.io/SquStouf/pen/GREwwNZ
So the problem is, that it doesn't work on the website in which I try to use it; and I really struggle to understand why.
.radioPro input[type="radio"] {
display: none;
}
.optPro {
display: flex;
width: 100%;
height: 33.33%;
align-items: center;
justify-content: center;
font-size: 25px;
font-weight: bold;
transition: all 0.3s ease;
margin: 0 !important;
cursor: pointer;
}
.optPro:hover {
background-color: #e1e1e1;
}
#id1:checked:checked~.id1,
#id2:checked:checked~.id2,
#id3:checked:checked~.id3 {
background-color: red;
color: white;
}
<div class="col-2 px-0 mx-0 radioPro">
<input type="radio" id="id1" value="1" name="1-group" />
<label for="id1" class="id1 optPro"> 1</label>
<input type="radio" id="id2" value="2" name="2-group" />
<label for="id2" class="id2 optPro"> 2</label>
<input type="radio" id="id3" value="3" name="3-group" />
<label for="id3" class="id3 optPro"> 3</label>
</div>

set text inside a check box

Hi is it possible to add text inside check box replacing the tick icon.
I am could not achieve it with this code could someone suggest me how to make a size selection box with a text inside it
<ion-col>
<p>Choose the size</p>
<ion-item>
<ion-checkbox >S</ion-checkbox>
</ion-item>
</ion-col>
Help me to bring out this type of UI
You can hide the input and use the label to select the checkbox. Then style your label like in the example below, using :not(:checked) and :checked selectors. Same logic can be applied to radio buttons.
ul {
padding: 0;
margin: 0;
clear: both;
}
li{
list-style-type: none;
list-style-position: outside;
padding: 10px;
float: left;
}
input[type="checkbox"]:not(:checked),
input[type="checkbox"]:checked {
position: absolute;
left: -9999%;
}
input[type="checkbox"] + label {
display: inline-block;
padding: 10px;
cursor: pointer;
border: 1px solid black;
color: black;
background-color: white;
margin-bottom: 10px;
}
input[type="checkbox"]:checked + label {
border: 1px solid white;
color: white;
background-color: black;
}
<ul>
<li>
<input type="checkbox" id="check_1" name="check_1" value="check_1">
<label for="check_1">S</label>
</li>
<li>
<input type="checkbox" id="check_2" name="check_2" value="check_2">
<label for="check_2">M</label>
</li>
<li>
<input type="checkbox" id="check_3" name="check_3" value="check_3">
<label for="check_3">L</label>
</li>
<li>
<input type="checkbox" id="check_4" name="check_4" value="check_4">
<label for="check_4">XL</label>
</li>
</ul>
Updated
check updated demo here
Add the following Js to get the relevant radio value
JS:
$("body").on("click", "label", function(e) {
var getValue = $(this).attr("for");
var goToParent = $(this).parents(".select-size");
var getInputRadio = goToParent.find("input[id = " + getValue + "]");
console.log(getInputRadio.attr("id"));
});
I assume that the user select only one size at a time. if user can select the multiple size modify the example with checkbox.
Try this
Check Demo here
HTML:
<div class="select-size">
<input type="radio" name="s-size" id="small" checked/>
<input type="radio" name="s-size" id="medium" />
<input type="radio" name="s-size" id="large" />
<input type="radio" name="s-size" id="x-large" />
<input type="radio" name="s-size" id="xx-large" />
<label for="small">S</label>
<label for="medium">M</label>
<label for="large">L</label>
<label for="x-large">XL</label>
<label for="xx-large">XXL</label>
</div>
CSS:
.select-size input{
display: none;
}
label {
display: inline-block;
width: 50px;
height: 50px;
text-align: center;
border: 1px solid #ddd;
line-height: 50px;
cursor: pointer
}
#small:checked ~ label[for="small"],
#medium:checked ~ label[for="medium"],
#large:checked ~ label[for="large"],
#x-large:checked ~ label[for="x-large"],
#xx-large:checked ~ label[for="xx-large"] {
background: #999;
color: #ffffff;
}

Style radio inputs as button when input is within label tag

My html:
<div class="product-addon product-addon-extra-tip">
<p class="form-row form-row-wide addon-wrap-2004-extra-tip-0-0">
<label><input type="radio" class="addon-radio" name="addon-2004-extra-tip-0[]" value="2">2</label>
</p>
<p class="form-row form-row-wide addon-wrap-2004-extra-donation-to-trafficking-survivors-0-1">
<label><input type="radio" class="addon-radio" name="addon-2004-extra-tip-0[]" value="5">5</label>
</p>
</div>
I'm trying to style these radio inputs to look like buttons, and I'm almost there. The problem is that given the current construct (which I can't directly change), I can't figure out how to make the :checked option look different than the rest.
You can see in the jsfiddle where I'm falling short. Is this possible?
http://jsfiddle.net/2gdotu21/1/
Via CSS, input set in front of label and correct attribute used, you can apply a different style if input is :checked or not.
See: https://www.w3.org/wiki/HTML/Elements/label & further more https://www.w3.org/TR/WCAG20-TECHS/H44.html
label {/* button unchecked add your style*/
color:red
}
label:before {/* button checked add your style*/
content:'$';
font-size:1rem;
}
input:checked + label {
color:green;
}
[type=radio]{ /* hide it ? use any methode but display:none; */
position:absolute;
right:200%;
}
<input type="radio" class="addon-radio" name="addon-2004-extra-tip-0[]" value="2" id="addon-2004-extra-tip-0[]" />
<label for="addon-2004-extra-tip-0[]">2</label>
<input type="radio" class="addon-radio" name="addon-2004-extra-tip-0[]" value="2" id="addon-2004-extra-tip-0[1]" />
<label for="addon-2004-extra-tip-0[1]">300</label>
<input type="radio" class="addon-radio" name="addon-2004-extra-tip-0[]" value="2" id="addon-2004-extra-tip-0[2]" />
<label for="addon-2004-extra-tip-0[2]">14</label>
<!-- same name to allow only one checked in this demo -->
else with your structure, integrate the radio within the design of the button http://codepen.io/gc-nomade/pen/LketK (oldish glassy button)
example of your code to change bg color
.product-addon-extra-tip label {
float: left;
width: auto;
min-width: 60px;
margin: 3px;
border-radius: 4px;
border: 1px solid #D0D0D0;
overflow: auto;
color: black;
font-size: 1.2rem;
text-align: center;
padding: 5px 0;
display: block;
line-height: 1.3rem;
}
.product-addon-extra-tip label input {}
.product-addon-extra-tip label:before {
content: '$';
}
label {
position: relative;
}
input {
position: absolute;
top: -15px;
z-index: -1;
box-shadow: 0 0 0 200px tomato;
}
input:checked {
box-shadow: 0 0 0 200px green;
}
<div class="product-addon product-addon-extra-tip">
<p class="form-row form-row-wide addon-wrap-2004-extra-tip-0-0">
<label><input type="radio" class="addon addon-radio" name="addon-2004-extra-tip-0[]" data-raw-price="0" data-price="" value="2"> 2 </label>
</p>
<p class="form-row form-row-wide addon-wrap-2004-extra-tip-0-1">
<label><input type="radio" class="addon addon-radio" name="addon-2004-extra-tip-0[]" data-raw-price="0" data-price="" value="5"> 5 </label>
</p>
</div>

How to style required field (only when it is clicked) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need style required fields, but I need show style only when user click first time, and then it does not click, I would like to keep with the default color.
With effect of click first form, but with css of second form, result of box 3:
http://jsfiddle.net/rflfn/m0f6xuxd/
Note: the red box-shadow is default color of browser for tag required of HTML5 (I am not using any script).
<div class="test1">box 1<br>Appear box-shadow only on click
<br>(default browser color)
<br>show on 3 input at same time (all required fields).
<form action="#">
<input type="text">
<input type="text" required>
<input type="text" required>
<input type="text">
<input type="text" required>
<input type="text">
<input type="text">
<input type="submit" text="Send">
</form>
</div>
<div class="test2">box 2<br>Have CSS, but I need show color only when click (use standard formatting until it is clicked)
<form action="#">
<input type="text">
<input type="text" required>
<input type="text" required>
<input type="text">
<input type="text" required>
<input type="text">
<input type="text">
<input type="submit" text="Send">
</form>
</div>
<div class="test3">box 3<br>I need style all required fields with my own css (show on 3 input at same time, not only on focus input)
<form action="#">
<input type="text">
<input type="text" required>
<input type="text" required>
<input type="text">
<input type="text" required>
<input type="text">
<input type="text">
<input type="submit" text="Send">
</form>
</div>
.test1, .test2, .test3 {
width: 230px;
padding: 5px;
margin: 5px;
border: 1px solid red;
display: inline-block;
vertical-align: top;
}
form {
margin: 20px;
}
form * {
display: block;
clear: both;
margin: 10px;
}
.test2 :required {
border: 1px solid #FFF;
border-radius: 3px;
}
.test2 :required:valid {
background-color: #E8FFED;
box-shadow: 0 0 3px 1px #00EA33;
}
.test2 :required:invalid {
background-color: #FFEEEE;
box-shadow: 0 0 3px 1px #EA2034;
}
.test3 input[type="text"] {
border: 1px solid #D1D1D1;
border-radius: 3px;
}
.test3 :required {
box-shadow: none;
}
.test3 :required:focus {
border: 1px solid #FFF;
background-color: #FFEEEE;
box-shadow: 0 0 3px 1px #EA2034;
}
.test3 :required:valid {
background-color: #E8FFED;
box-shadow: 0 0 3px 1px #00EA33;
}
EDIT: No classes needed.
You can use general sibling combinator. The only problem is it will not work for elements above the one you click on. To go further you will need Javascript.
jsfiddle
Key part:
input:required:focus~input:required, input:required:focus {
border: 1px solid #FFF;
background-color: #FFEEEE;
box-shadow: 0 0 3px 1px #EA2034;
}

Radio Buttons Inline and Centered with Labels

I have the following form:
<form action="post.php" method="POST">
<fieldset>
<div class="ratingClass">
<input type="radio" class="radio" name="rate" value="1" id="1"/>
<label for="1">1</label>
<input type="radio" class="radio" name="rate" value="2" id="2"/>
<label for="2">2</label>
<input type="radio" class="radio" name="rate" value="3" id="3"/>
<label for="3">3</label>
<input type="radio" class="radio" name="rate" value="4" id="4"/>
<label for="4">4</label>
<input type="radio" class="radio" name="rate" value="5" id="5"/>
<label for="5">5</label>
</div>
</fieldset>
<input type="submit" value="Rate">
</form>
Styled by the following CSS:
fieldset {
overflow:hidden;
}
.ratingClass {
float:left;
clear:none;
}
label {
float:left;
clear:none;
display:block;
padding: 2px 1em 0 0;
}
input[type=radio], input.radio {
float:left;
clear:none;
margin: 2px 0 0 2px;
}
It's all inside of another div that has text-align: center; styling.
I realize that behavior is because of the floats, but if I remove them then the radio buttons no longer display inline.
How can I have them inline and centered?
You don't need to float everything nor make the labels block elements. Replacing your CSS with this causes everything to be centered:
fieldset {
overflow: hidden;
}
label {
padding: 2px 1em 0 0;
}
input[type=radio], input.radio {
margin: 2px 0 0 2px;
}
The <div class="ratingClass"> is also superfluous and can be removed.
Try making the .ratingClass container either:
.ratingClass {
float:left;
clear:none;
width: 100%;
text-align: center;
}
or
.ratingClass {
float:none;
text-align: center;
}
If you can cope with a fixed width for the ratingClass div you could do it as follows…
.ratingClass {
width:300px; /* insert desired width here */
margin:auto;
}

Resources