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 submitting the form it will display a custom message.
<h4>Example custom message for required input fields</h4>
<p>Add some text in the input field and click Send.</p>

<form action='#' method='post'>
Email: <input type='email' id='email_1' required placeholder='Email address'/><br>
<input type='submit' value='Send'/>
</form>

<script>
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){
 ev.target.setCustomValidity('Your custom message');
});

//this is necessary to remove the invalidate mark on validated filed
email_1.addEventListener('input', function(ev){
 ev.target.setCustomValidity('');
});
</script>

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.

<h4>Example emitting the invalid event</h4>
<p>For test, select a date lower then curent date.</p>

<input type='date' id='date1' required placeholder='Select a date'>

<script>
//function called by the change event
//receives the default event argument, a js object with target event
function dateActiv(ev){
 var elm = ev.target; //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(ev.target.value =='' || ev.target.validity.typeMismatch) {
 ev.target.setCustomValidity('Your custom message');
 return false;
 }
 return true;
});
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Set custom message for required and field validation

Last accessed pages

  1. Contact page - CoursesWeb (10968)
  2. Tabs effect with CSS (11675)
  3. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (17574)
  4. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (15674)
  5. SHA512 Encrypt hash in JavaScript (12625)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1975)
  2. Contact page - CoursesWeb (1923)
  3. Tabs effect with CSS (1923)
  4. Insert, Select and Update NULL value in MySQL (1228)
  5. PHP getElementById and getElementsByTagName (722)