0

<form class="user" action="code_user.php" method="POST">
  <div class="form-group">
      <label>Enter Full Name</label>
      <input class="form-control" pattern="^[a-zA-Z]+(( )+[a-zA-z]+)*$" oninvalid="setCustomValidity('Please enter in alphabets only. ')" type="text" name="name" autocomplete="off" required />
   </div>
    <button type="submit" id="submit"name="signup_btn"class="btn btn-primary btn-user btn-block"> Sign Up </button>

</form> 

In HTML input validation , i have set the pattern of alphebetic only. When testing with numeric input in it , it shows

However, if i correct the input into the alphabets ,it will shows invalid again enter image description here

I have to refresh the page again to input the correct format only it will not show invalid. Is this normal? This is my code:

<div class="form-group">
     <label>Enter Full Name</label>
     <input class="form-control" pattern="^[a-zA-Z]+(( )+[a-zA-z]+)*$" oninvalid="setCustomValidity('Please enter in alphabets only. ')" type="text" name="name" autocomplete="off" required />
</div>
3
  • Are you sure? It works for me. Try adding a code snippet to your question where we could try it. Commented Apr 13, 2020 at 19:32
  • Does it show the error immediately? The check is only performed on submission of the form (developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation) Commented Apr 13, 2020 at 19:36
  • @BartBarnard yes, it showed the error immediately when i enter a letter Commented Apr 13, 2020 at 19:39

1 Answer 1

1

You don't clear your setCustomValidity(). If you set a value with setCustomValidity() then the field is invalid. Just add such attribute to your input tag:

oninput="setCustomValidity('')"

Fixed in your code:

<form class="user" action="code_user.php" method="POST">
    <div class="form-group">
        <label>Enter Full Name</label>
        <input class="form-control" pattern="^[a-zA-Z]+(( )+[a-zA-z]+)*$" oninvalid="setCustomValidity('Please enter in alphabets only. ')" oninput="setCustomValidity('')" type="text" name="name" autocomplete="off" required />
    </div>
    <button type="submit" id="submit"name="signup_btn"class="btn btn-primary btn-user btn-block"> Sign Up </button>
</form> 

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

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.