Display HTML5 error message/validation on hidden radio/checkbox

前端 未结 3 1928
醉酒成梦
醉酒成梦 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:32

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

    Gender

提交回复
热议问题