167

For example I have a textfield. The field is mandatory, only numbers are required and length of value must be 10. When I try to submit form with value which length is 5, the default error message appears: Please match the requested format

<input type="text" required="" pattern="[0-9]{10}" value="">
  1. How can I change HTML form validation errors default messages?
  2. If the 1st point can be done, is there a way to create some property files and set in that files custom error messages?
1
  • 1
    I found a bug on Mahoor13 answer, it's not working in loop so I've fixed it & give answer with some correction, which you can find under the answer section. Here is the link stackoverflow.com/questions/10361460/… Commented Mar 15, 2017 at 8:31

13 Answers 13

252

This is the JavaScript solution:

 <input type="text"
    pattern="[a-zA-Z]+"
    oninvalid="setCustomValidity('Please enter Alphabets.')"
    onchange="try{setCustomValidity('')}catch(e){}" />

The "onchange" event needs when you set an invalid input data, then correct the input and send the form again. I've tested it on Firefox, Chrome and Safari.

But for Modern Browsers:

Modern browsers didn't need any JavaScript for validation. Just do it like this:

<input type="text"
    pattern="[a-zA-Z]+"
    title="Please enter Alphabets."
    required="" />
Sign up to request clarification or add additional context in comments.

7 Comments

I suggest against using inline events. It's not a good practise.
@Mahoor13 I have written it similar to that, but it never validates. Can you gimme a hint ? jsfiddle.net/2USxy
There's also a custom Firefox property you can add which works well: x-moz-errormessage="Please enter only numbers"
better to use "onkeyup" instead of "onchange" to see effectively if the input is valid or not.
This has the following problem (at least in Chrome 55) : when the invalid message pops , if the user keeps focus in the invalid field (without clicking) and edits the field the message keeps poping out - even when the field is valid - it's necessary to either focus out or trigger submit.
|
113

When using pattern= it will display whatever you put in the title attrib, so no JS required just do:

<input type="text" required="" pattern="[0-9]{10}" value="" title="This is an error message" />

6 Comments

Nice answer, but be careful about backward (or even current) compatibility here.
When I set a title to "foo" I get "Please match the requested format. foo", so this won't work for me
Title is also used as the tooltip text on mouse hover, so I would stay away from this approach.
Title is also used as the tooltip text on mouse hover, so this is probably the best approach.
I think the idea with the title attribute is to describe the requirement like "Enter a 10-digit number." rather than an error like "This is not a valid number". When describing the requirement, there is no problem with the same text appearing both in mouse hover and in the validation error message. (I'm not saying my wording is the best, I know it could be argued whether or not to say "Enter..." vs. "Value must be..." etc.)
|
38

<form>
 <input type="text" pattern="[a-zA-Z]+"
 oninvalid="setCustomValidity('Plz enter on Alphabets ')" />
 <br /><input type="submit">
</form>

I found this code in another post.

2 Comments

thanks, it worked for me. Though I did not find post or blog or site mentioning this.
Take note on the following post when considering using setCustomValitidy stackoverflow.com/questions/16867407/…
17

HTML:

<input type="text" pattern="[0-9]{10}" oninvalid="InvalidMsg(this);" name="email" oninput="InvalidMsg(this);"  />

JAVASCRIPT :

function InvalidMsg(textbox) {

     if(textbox.validity.patternMismatch){
        textbox.setCustomValidity('please enter 10 numeric value.');
    }    
    else {
        textbox.setCustomValidity('');
    }
    return true;
}

Fiddle Demo

Comments

15

To prevent the browser validation message from appearing in your document, with jQuery:

$('input, select, textarea').on("invalid", function(e) {
    e.preventDefault();
});

Comments

13

you can remove this alert by doing following:

<input type="text" pattern="[a-zA-Z]+"
    oninvalid="setCustomValidity(' ')"
/>

just set the custom message to one blank space

Comments

11

you can change them via constraint validation api: http://www.w3.org/TR/html5/constraints.html#dom-cva-setcustomvalidity

if you want an easy solution, you can rock out civem.js, Custom Input Validation Error Messages JavaScript lib download here: https://github.com/javanto/civem.js live demo here: http://jsfiddle.net/hleinone/njSbH/

Comments

6

I Found a way Accidentally Now: you can need use this: data-error:""

 <form>
    <input type="username" class="form-control" name="username" value=""
    placeholder="the least 4 character"
    data-minlength="4" data-minlength-error="the least 4 character"
    data-error="This is a custom Errot Text fot patern and fill blank"
    max-length="15" pattern="[A-Za-z0-9]{4,}"
    title="4~15 character" required/>
    <br /><input type="submit">
</form>

Comments

5

The setCustomValidity let you change the default validation message.Here is a simple exmaple of how to use it.

var age  =  document.getElementById('age');
    age.form.onsubmit = function () {
    age.setCustomValidity("This is not a valid age.");
 };

Comments

4

I found a bug on Mahoor13 answer, it's not working in loop so I've fixed it with this correction:

HTML:

<input type="email" id="eid" name="email_field" oninput="check(this)">

Javascript:

function check(input) {  
    if(input.validity.typeMismatch){  
        input.setCustomValidity("Dude '" + input.value + "' is not a valid email. Enter something nice!!");  
    }  
    else {  
        input.setCustomValidity("");  
    }                 
}  

It will perfectly running in loop.

Comments

4

This is work for me in Chrome

<input type="text" name="product_title" class="form-control" 
    required placeholder="Product Name" value="" pattern="([A-z0-9À-ž\s]){2,}"
    oninvalid="setCustomValidity('Please enter on Producut Name at least 2 characters long')" />

2 Comments

when I try this, the invalid message appears when the value is invalid, but it doesn't go away when the field becomes valid again - it just stays there forever
update: I got the same behaviour in Chrome and Safari. I had to set it to '' in onChange, then it worked ok, like in some other answers here.
4

To set custom error message for HTML validation use,

oninvalid="this.setCustomValidity('Your custom message goes here.')"

and to remove this message when user enters valid data use,

onkeyup="setCustomValidity('')"

Comments

2

As you can see here:

html5 oninvalid doesn't work after fixed the input field

Is good to you put in that way, for when you fix the error disapear the warning message.

<input type="text" pattern="[a-zA-Z]+"
oninvalid="this.setCustomValidity(this.willValidate?'':'your custom message')" />

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.