Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can hide the invalid value (✘) symbol on page load using either one of the following options.</p> <p><strong>Option 1:</strong> Hide the <code>span</code> which contains the symbol on page load and display it only when some <code>keypress</code> event has happened on the input text box.</p> <p><div class="snippet" data-lang="js" data-hide="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>window.onload = function() { var el = document.querySelectorAll("input[type='text']"); for (var i = 0; i &lt; 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. } }</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>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; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;input name="pw" autocomplete="off" type="text" id="pw" class="new" pattern="[a-zA-Z0-9]{6,22}" autofocus required/&gt; &lt;span class="input-validation"&gt;&lt;/span&gt;</code></pre> </div> </div> </p> <hr> <p><strong>Option 2:</strong> Define the CSS rules based on the presence of certain class (say <code>visited</code>) and assign this class only when some key is pressed in the input box.</p> <p><div class="snippet" data-lang="js" data-hide="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>window.onload = function() { var el = document.querySelectorAll("input[type='text']"); for (var i = 0; i &lt; el.length; i++) { el[i].onkeypress = showSymbol; } function showSymbol() { this.classList.add("visited"); // add the visited class } }</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>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; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;input name="pw" autocomplete="off" type="text" id="pw" class="new" pattern="[a-zA-Z0-9]{6,22}" autofocus required/&gt; &lt;span class="input-validation"&gt;&lt;/span&gt;</code></pre> </div> </div> </p> <p><strong>Note:</strong> </p> <ul> <li>I have replaced the <code>~</code> in your CSS selectors with <code>+</code> because <code>~</code> selects all siblings which match the selector whereas the <code>+</code> selects only the adjacent sibling. Using <code>~</code> would make the <code>span</code> 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.</li> <li>I have also modified the <code>.input-validation</code> from <code>div</code> to <code>span</code> but that is more of a personal preference and you can just retain the original <code>div</code> itself without any difference in functionality.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload