ChatGPT解决这个技术问题 Extra ChatGPT

How can I change or remove HTML5 form validation default error messages?

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="">

How can I change HTML 5 form validation errors default messages? If 1st point can be done, so Is there a way to create some property files and set in that files custom error messages?

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/…

M
Mahoor13

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="" />

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
html5 form validation needs modern browsers see this page: diveintohtml5.info/forms.html
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.
H
HTF

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" />

Very nice answer. But as with all things HTML5, reliability depends upon the browser. This solution worked great with FF 15 and Chrome 22, but not with Safari 5. (Tested with OS X Lion)
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.)
C
Coleman
<input type="text" pattern="[a-zA-Z]+"
oninvalid="setCustomValidity('Plz enter on Alphabets ')" />

I found this code in another post.


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/…
R
Rikin Patel

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


M
M-Soley

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

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

u
user3894381

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


a
albert

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/


k
kta

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.");
 };

M
Mohammad Shojaa

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/>

n
noufalcep

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.


U
Umesh Patil

To set custom error message for HTML 5 validation use,

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

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

onkeyup="setCustomValidity('')"

Hope this works for you. Enjoy ;)


A
Abibullah Rahamathulah

This is a very good link I found about checking various attributes of html5 validations.

https://dzone.com/articles/custom-validation-messages

Hope it helps someone.


C
Community

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')" />

a
anastaciu

This is work for me in Chrome


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.
s
sadeq qane

simply you can use "novalidate" for < form > tag

click here for more deteails