0

is there a way to validate the pattern of an input by it's event object (without using a form)?

something like:

<input type='email' onChange={e => e.isPatternOk} />
1
  • You mean <input oninput="if (re.test(this.value)) ...." Commented Mar 22, 2019 at 13:00

2 Answers 2

3

Beat me to it :), glad you figure it out

Below uses pure css AND a js solution, pick your fancy

.status * {
  display: none;
  margin-bottom: 20px;
}

input:valid+.status .valid {
  display: inline-block;
  background: limegreen;
}

input:invalid+.status .invalid {
  display: inline-block;
  background: red;
}
<input id="textfield" type="text" pattern=".{3,}" required/>
<div class="status">
  <span class="invalid">input is invalid</span>
  <span class="valid">input is valid</span>
</div>
<input type=button onclick="document.getElementById('js-result').innerHTML = 'Input is valid: '+ document.getElementById('textfield').validity.valid;" value="Check with JS" />
<fieldset>
  <legend>JS Result</legend>
  <div id="js-result"></div>
</fieldset>

Sign up to request clarification or add additional context in comments.

Comments

1

ok i just got it.

<input type='email' onChange={e => e.currentTarget.checkValidity()} />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.