In this tutorial I show you how to display custom message for required form fields and how to emit dinamically the invalid event on field validation in JavaScript.
- Details are in the comments in code.

Show custom message for required input fields

To show a custom message for required input fields, apply the invalid event to that field, then use the setCustomValidity() method in the function called by the "invalid" event.
To remove the invalidate message on validated field, apply setCustomValidity('') in the input event.

- Example, if the email address added into a required email field is not valid; when to try to submit the form it will display a custom message.
For test, add some text and click Send.<br>
<form action='#' method='post'>
Email: <input type='email' id='email_1' required placeholder='Email address'/><br>
<input type='submit' value='Send'/>
var email_1 = document.querySelector('#email_1');

//register invalid event that shows a custom message on invalidated of the field
email_1.addEventListener('invalid', function(ev){'Your custom message');

//this is necessary to remove the invalidate mark on validated filed
email_1.addEventListener('input', function(ev){'');
- Demo:
For test, add some text and click Send.

Emitting the invalid event in JavaScript

By default, the invalid event is emitted when the form is submited. To emit this event dinamically on field validation in JS, call the reportValidity() method.
- For example, we have a date input field and we want the user to add a date equal or higher than current date; otherwise the field to display a custom message for required. So, we need to emit the invalid event if the value added in the "date" field is not valid or is lower than current date.
For test, select a date lower then curent date.<br>
<input type="date" id="date1" required placeholder="Select a date">
//function called by the change event
//receives the default event argument, a js object with target event
function dateActiv(ev){
 var elm =; //get the element from event
 var sel_date = new Date(elm.value);

 //if selected date is lower than current date
 if(sel_date <= new Date()){
 elm.value =''; //set empty to detect as not valid when invalid event is emitted
 elm.reportValidity(); //to emit invalid event

//register change event that cals dateActiv()
document.querySelector('#date1').addEventListener('change', dateActiv);

//register invalid event that shows a custom message
document.querySelector('#date1').addEventListener('invalid', function(ev){
 if( =='' || {'Your custom message');
 return false;
 return true;
- Demo:
For test, select a date lower then curent date.

Daily Test with Code Example

Which HTML5 tag defines marked text? (can be used to highlight parts of text)
<mark> <embed> <span>
<p>Free corses: <mark></mark> for Web Development.</p>
Which CSS pseudo-class adds a style to an element when the mouse is over it?
:focus :hover :active
a:hover {
  font-weight: bold;
  color: #00da01;
Click on the function which returns a string value that represents the number rounded to the x digits after the decimal point.
toPrecision(x) toFixed(x) floor(x)
var num = 12.34567;
num = num.toFixed(2);
alert(num);       // 12.35
Indicate the PHP function which reads an entire file into an array.
[) file() readfile()
$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
Set custom message for required and field validation

Last accessed pages

  1. Get Duration of Audio /Video file before Upload (5913)
  2. Follow the mouse cursor with a DIV inside a Parent (2703)
  3. Register and show online users and visitors (23278)
  4. Node.js Move and Copy Directory (1127)
  5. SHA1 Encrypt data in JavaScript (8256)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (390)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (332)
  3. Register and show online users and visitors (302)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (234)
  5. Insert, Select and Update NULL value in MySQL (223)