I use the code below with HTML5 pattern matching on the input boxes and the CSS3 :invalid
and :valid
pseudo-selectors to display the output of the validation (valid value or not) in the div.input-validation
available next to the input box. This works as expected but it displays the validation mark (✘ - invalid input) during page load itself and on re-loading the page. How should I avoid this?
Code:
<style type="text/css">
input {
font-size: 1em;
padding: .3em;
border-radius: 3px;
margin: .2em;
}
input[type="text"]:valid {
color: green;
}
input[type="text"]:valid ~ .input-validation::before {
content: "\2714";
color: green;
}
input[type="text"]:invalid ~ .input-validation::before {
content: "\2718";
color: red;
}
.input-validation {
display: inline-block;
}
</style>
<?echo $passwordregister;?>
<input name="pw" autocomplete="off" type="text" id="pw" pattern="[a-zA-Z0-9]{6,22}" autofocus required >
<div class="input-validation"></div>
You can hide the invalid value (✘) symbol on page load using either one of the following options.
Option 1: Hide the span
which contains the symbol on page load and display it only when some keypress
event has happened on the input text box.
window.onload = function() { var el = document.querySelectorAll("input[type='text']"); for (var i = 0; i < el.length; i++) { el[i].onkeypress = showSymbol; } function showSymbol() { this.nextElementSibling.style.display = "inline-block"; // display the span next to the input in which key was pressed. } }
input { font-size: 1em; padding: .3em; border-radius: 3px; margin: .2em; } input[type="text"]:valid { color: green; } input[type="text"]:valid + .input-validation::before { content: "\\2714"; color: green; } input[type="text"]:invalid + .input-validation::before { content: "\\2718"; color: red; } .input-validation { display: none; }
<input name="pw" autocomplete="off" type="text" id="pw" class="new" pattern="[a-zA-Z0-9]{6,22}" autofocus required/> <span class="input-validation"></span>
Option 2: Define the CSS rules based on the presence of certain class (say visited
) and assign this class only when some key is pressed in the input box.
window.onload = function() { var el = document.querySelectorAll("input[type='text']"); for (var i = 0; i < el.length; i++) { el[i].onkeypress = showSymbol; } function showSymbol() { this.classList.add("visited"); // add the visited class } }
input { font-size: 1em; padding: .3em; border-radius: 3px; margin: .2em; } input[type="text"].visited:valid { color: green; } input[type="text"].visited:valid + .input-validation::before { content: "\\2714"; color: green; } input[type="text"].visited:invalid + .input-validation::before { content: "\\2718"; color: red; } .input-validation { display: inline-block; }
<input name="pw" autocomplete="off" type="text" id="pw" class="new" pattern="[a-zA-Z0-9]{6,22}" autofocus required/> <span class="input-validation"></span>
Note:
~
in your CSS selectors with +
because ~
selects all siblings which match the selector whereas the +
selects only the adjacent sibling. Using ~
would make the span
next to all input boxes get displayed (when you have multiple input boxes in the form) as soon as you type a value in the first. .input-validation
from div
to span
but that is more of a personal preference and you can just retain the original div
itself without any difference in functionality.
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.