Display HTML5 error message/validation on hidden radio/checkbox

前端 未结 3 1932
醉酒成梦
醉酒成梦 2021-01-02 10:05

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 <

3条回答
  •  孤街浪徒
    2021-01-02 10:47

    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;}

    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.

提交回复
热议问题