1

I want my HTML form to show a custom error message when an email address is not in correct format

<form>
    <input type="text" pattern="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$" name="email" oninvalid="this.setCustomValidity('custom message')" oninput="setCustomValidity('')" />
    <input type="submit" value="Send"/>
</form>

JSFiddle example

When I enter an email in the wrong format and click "send", the expected message is shown:

The expected message

However when I start typing again, the following default browser message is shown (my browser is set to Dutch):

Default message

Is there anyway to prevent the default message from showing? Can I disable the validation while typing?

0

1 Answer 1

1

You can do this..Assuming your textbox id is "text1"..

$("#text1").change(function(e) {
         e.preventDefault();
    });

or

 $("#text1").keydown(function(e) {
         e.preventDefault();
    });

Any of above can be used but in your situation I prefer keydown event...

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.