最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change checkbox required message - Stack Overflow

programmeradmin3浏览0评论

I want to change my checkbox required default message.

I am trying with this code but if I check my checkbox then it still shows required message.

my code

<input  required type="checkbox" name="rcheck2" 
oninvalid="this.setCustomValidity('check plz')" oninput="setCustomValidity('')" />

/

I want to change my checkbox required default message.

I am trying with this code but if I check my checkbox then it still shows required message.

my code

<input  required type="checkbox" name="rcheck2" 
oninvalid="this.setCustomValidity('check plz')" oninput="setCustomValidity('')" />

https://jsfiddle/6ekk9upz/

Share Improve this question edited Aug 9, 2016 at 14:02 testset asked Aug 9, 2016 at 13:57 testsettestset 411 gold badge1 silver badge3 bronze badges 3
  • Please post a functional example that illustrates your problem on jsFiddle. – palaѕн Commented Aug 9, 2016 at 13:59
  • Show us all the code. We are not able to help you when we don't see what you are doing ;) BTW Why are you calling JS "this." object in html as i see? – Stefan Stefko Commented Aug 9, 2016 at 14:00
  • please check jsfiddle/6ekk9upz @palaѕн – testset Commented Aug 9, 2016 at 14:02
Add a ment  | 

2 Answers 2

Reset to default 16

checkbox inputs don't have the oninput event, they have the onchange event.

Check this:

<form action="" name="formReg" id="formReg" method="post" class="formReg">
  <div class="checkTxt">
    <input  required type="checkbox" name="rcheck1" oninvalid="this.setCustomValidity('check plz')" onchange="this.setCustomValidity('')"/>
  </div>
  <div class="checkTxt">
    <input  required type="checkbox" name="rcheck2" oninvalid="this.setCustomValidity('check plz')"  onchange="this.setCustomValidity('')"/>
  </div>
  <div class="checkTxt">
    <input  required type="checkbox" name="rcheck3" oninvalid="this.setCustomValidity('check plz')" onchange="this.setCustomValidity('')"/>
  </div>
  <input type="submit" name="btnReg" value="Submite"/>
</form>

simply try this code , this is for typescript (react) but you can do as you like

          onInvalid={(e) => {
            const target = e.target as HTMLInputElement
            target.setCustomValidity("Please tick this box to proceed.")
          }}
发布评论

评论列表(0)

  1. 暂无评论