Client side validation using form validator in EJ2 JavaScript Timepicker control
26 Apr 20234 minutes to read
To achieve client side validation in a TimePicker component, use Essential JavaScript 2 FormValidator. It provides an option to customize feedback error messages to the corresponding fields for taking action and resolving the issue.
In the following example, the required field validation is implemented by mapping the name attribute value to the rules property. It validates the TimePicker component and displays the validation message when the textbox value is empty during form post back or focus out.
ej.base.enableRipple(true);
var timepicker = new ej.calendars.TimePicker({
placeholder: 'Select a time',
value :new Date()
});
timepicker.appendTo('#element');
var options = {
rules: {
//must specify the name attribute value in rules section
'timevalue': { required: true }
},
customPlacement: (inputElement, errorElement) => {
//to place the error message in custom position
//inputElement - target element where the error text will be appended
//errorElement - error text which will be displayed.
inputElement.parentElement.parentElement.appendChild(errorElement);
}
};
var formObject = new ej.inputs.FormValidator('#form-element', options);
<!DOCTYPE html><html lang="en"><head>
<title>Essential JS 2 TimePicker control</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<!--style reference from the TimePicker component-->
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-lists/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-calendars/styles/material.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<form id="form-element" class="form-vertical">
<div class="form-group">
<div class="col-sm-3 control-label">Required</div>
<div class="col-sm-6">
<input type="text" id="element" name="timevalue" class="form-control">
</div>
</div>
</form>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>