164

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?
TylerH
  • 20,799
  • 66
  • 75
  • 101
emilan
  • 12,825
  • 11
  • 32
  • 37
  • 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** http://stackoverflow.com/questions/10361460/how-can-i-change-or-remove-html5-form-validation-default-error-messages/42430173#42430173 – Darshan Gorasia Mar 15 '17 at 08:31

13 Answers13

244

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="" />
Mahoor13
  • 5,297
  • 5
  • 23
  • 24
  • 22
    I suggest against using inline events. It's not a good practise. – Jared May 01 '13 at 06:54
  • 2
    @Mahoor13 I have written it similar to that, but it never validates. Can you gimme a hint ? http://jsfiddle.net/2USxy/ – Sliq May 31 '13 at 23:36
  • 1
    There's also a custom Firefox property you can add which works well: x-moz-errormessage="Please enter only numbers" – coliff Sep 18 '13 at 13:40
  • 4
    better to use "onkeyup" instead of "onchange" to see effectively if the input is valid or not. – Sam San Jun 02 '14 at 02:38
  • 1
    replacing oninput with onchange fixed my problem on firefox when textFiled is focused – h0mayun Dec 14 '14 at 09:56
  • On Chrome 52, oninvalid fired BEFORE onkeyup, so I ended up without the custom message. I successfully used onkeypress instead. – Nate Sep 01 '16 at 01:01
  • 4
    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. – leonbloy Jan 21 '17 at 15:40
110

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" />
HTF
  • 1,141
  • 1
  • 7
  • 2
  • 1
    Nice answer, but be careful about backward (or even current) compatibility here. – bohney Oct 08 '12 at 19:32
  • 9
    When I set a title to "foo" I get "Please match the requested format. foo", so this won't work for me – Daan Mar 26 '14 at 08:35
  • 8
    Title is also used as the tooltip text on mouse hover, so I would stay away from this approach. – fotijr Mar 02 '15 at 19:30
  • 4
    Title is also used as the tooltip text on mouse hover, so this is probably the best approach. – OdraEncoded Aug 23 '15 at 22:33
  • 2
    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.) – Ken Lyon Apr 11 '19 at 17:49
  • this will most assuredly fail a11y – albert May 16 '19 at 23:39
38
<input type="text" pattern="[a-zA-Z]+"
oninvalid="setCustomValidity('Plz enter on Alphabets ')" />

I found this code in another post.

Coleman
  • 565
  • 7
  • 15
Ankur Loriya
  • 3,276
  • 8
  • 31
  • 58
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

Rikin Patel
  • 8,848
  • 7
  • 70
  • 78
16

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

$('input, select, textarea').on("invalid", function(e) {
    e.preventDefault();
});
M-Soley
  • 265
  • 1
  • 4
  • 13
Luca Fagioli
  • 12,722
  • 5
  • 59
  • 57
14

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

user3894381
  • 141
  • 1
  • 2
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/

albert
  • 8,112
  • 3
  • 47
  • 63
6

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

<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/>
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.");
 };
kta
  • 19,412
  • 7
  • 65
  • 47
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.

noufalcep
  • 3,446
  • 15
  • 33
  • 51
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')" />
anastaciu
  • 23,467
  • 7
  • 28
  • 53
Sahan Pasindu Nirmal
  • 433
  • 4
  • 13
  • 36
  • 1
    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 – Ben Clayton Mar 02 '22 at 11:47
  • 1
    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. – Ben Clayton Mar 02 '22 at 11:53
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('')"
TylerH
  • 20,799
  • 66
  • 75
  • 101
Umesh Patil
  • 4,668
  • 32
  • 24
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')" />
Community
  • 1
  • 1
Jonathan Machado
  • 522
  • 4
  • 14