简体   繁体   中英

Toggle div when text input has pattern match

I'm trying to show a div when someone has entered the correct answer into a text field. I'm using CSS psuedo selectors to hide the div when the field is invalid, but an empty field counts as valid, which shows the div before anything has been entered.

input:invalid + #next {
  display:none;
}

input:valid + #next {
  display:block;
}
<form>
  <input type="text" maxlength="4" pattern="1234">
  <div id="next">SUCCESS</div>
</form>

Here you just need to add required attribute so that some value should be needed before validating the input

 input:invalid + #next { display:none; } input:valid + #next { display:block; }
 <form> <input type="text" maxlength="4" pattern="1234" required> <div id="next">SUCCESS</div> </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM