Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

javascript - Alert when html5 validation fails

So what I need is simply an alert to appear when my html5 validation fails, like :

alert("Error, please fill all required fields before submitting.");

Is there some kind of event that I could use to show this alert using JavaScript or jQuery?

This seems really basic, but I couldn't find anything online about it. All I can find are ways to change the pop up message that appears directly on the field.

I need this because my forms spans over multiples tabs, so the bubble displaying an error message may not always appear depending on where the user is when he submits the form.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's invalid event fired on inputs if they don't pass validation: https://developer.mozilla.org/en-US/docs/Web/Events/invalid

Using jQuery you could do something like this:

$('input[required]').on('invalid', function(e){
  alert("Error, please fill all required fields before submitting.");
});

http://jsfiddle.net/644Lou8k/1/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...