Having trouble getting help?
Contact Support
Contact Support
Validate slider using FormValidator in EJ2 JavaScript Range Slider control
3 Mar 202521 minutes to read
The Range Slider control can be validated using our FormValidator
. The following steps walk through the slider validation process.
- Render the Range Slider control inside a form.
- Bind the
changed
event in the Range Slider to validate the slider value when it changes. - Initialize and render FormValidator for the form using the form ID.
// Initialize Form validation
let formObj;
formObj = new FormValidator("#formId", options);
- Set the required property in the FormValidator
rules
collection. Here, themin
property of slider that sets the minimum value in the Range Slider control is set, and it has hidden input as enablevalidateHidden
property is set to true.
// Slider element
<div id="default" name="slider"></div>
// sets required property in the FormValidator rules collection
let options = {
rules: {
'default': {
validateHidden: true,
min: [6, "You must select value greater than 5"]
}
}
};
Form validation is done either by ID or name value of the Range Slider control. Above ID of the slider is used to validate it.
Using slider name: Render slider with name attribute. In the following code snippet, name attribute value of slider is used for form validation.
// Slider element
<div id="default" name="slider"></div>
// sets required property in the FormValidator rules collection
let options = {
rules: {
'slider': {
validateHidden: true,
min: [6, "You must select value greater than 5"]
}
}
};
- Validate the form using validate method, and it validates the slider value with the defined rules collection and returns the result. If user selects the value less than the minimum value, form will not submit.
formObj.validate();
- Slider validation can be done during value changes in slider. Refer to the following code snippet.
// change event handler for slider
function onChanged(args) {
formObj.validate();
}
The FormValidator
has following default validation rules, which are used to validate the Range Slider control.
Rules | Description | Example |
---|---|---|
max |
Range Slider control must have value less than or equal to max number |
if max: 3 , 3 is valid and 4 is invalid |
min |
Range Slider control must have value greater than or equal to min number |
if min: 4 , 5 is valid and 2 is invalid |
regex |
Range Slider control must have valid value in regex format |
if regex: '/4/ , 4 is valid and 1 is invalid |
range |
Range Slider control must have value between range number |
if range: [4,5] , 4 is valid and 6 is invalid |
// Initialize Range Slider Control
var SliderMinObj = new ej.inputs.Slider({
type: 'MinRange',
value: 30,
ticks: { placement: 'Before', largeStep: 20, smallStep: 5, showSmallTicks: true },
changed: onMinChanged
});
SliderMinObj.appendTo("#min-slider");
// sets required property in the FormValidator rules collection
var minOptions = {
rules: {
'min-slider': {
validateHidden: true,
min: [40, "You must select value greater than or equal to 40"]
}
}
};
// Initialize Form validation
var formMinObj = new ej.inputs.FormValidator("#formMinId", minOptions);
function onMinChanged(args) {
// validate the slider value in the form
formMinObj.validate();
}
// Initialize Range Slider Control
var SliderMaxObj = new ej.inputs.Slider({
type: 'MinRange',
value: 30,
ticks: { placement: 'Before', largeStep: 20, smallStep: 5, showSmallTicks: true },
changed: onMaxChanged
});
SliderMaxObj.appendTo("#max-slider");
// sets required property in the FormValidator rules collection
var maxOptions = {
rules: {
'max-slider': {
validateHidden: true,
max: [40, "You must select value less than or equal to 40"]
}
}
};
// Initialize Form validation
var formMaxObj = new ej.inputs.FormValidator("#formMaxId", maxOptions);
function onMaxChanged(args) {
// validate the slider value in the form
formMaxObj.validate();
}
// Initialize Range Slider Control
var SliderValObj = new ej.inputs.Slider({
type: 'MinRange',
value: 30,
ticks: { placement: 'Before', largeStep: 20, smallStep: 5, showSmallTicks: true },
changed: onValChanged
});
SliderValObj.appendTo("#val-slider");
// sets required property in the FormValidator rules collection
var valOptions = {
rules: {
'val-slider': {
validateHidden: true,
regex: [/40/, "You must select value equal to 40"]
}
}
};
// Initialize Form validation
var formValObj = new ej.inputs.FormValidator("#formValId", valOptions);
function onValChanged(args) {
// validate the slider value in the form
formValObj.validate();
}
// Initialize Range Slider Control
var SliderRangeObj = new ej.inputs.Slider({
type: 'MinRange',
value: 30,
ticks: { placement: 'Before', largeStep: 20, smallStep: 5, showSmallTicks: true },
changed: onRangeChanged
});
SliderRangeObj.appendTo("#range-slider");
// sets required property in the FormValidator rules collection
var rangeOptions = {
rules: {
'range-slider': {
validateHidden: true,
range: [40, 80, "You must select values between 40 and 80"]
}
}
};
// Initialize Form validation
var formRangeObj = new ej.inputs.FormValidator("#formRangeId", rangeOptions);
function onRangeChanged(args) {
// validate the slider value in the form
formRangeObj.validate();
}
// Initialize Range Slider Control
var SliderCustomObj = new ej.inputs.Slider({
type: 'Range',
value: [30, 70],
ticks: { placement: 'Before', largeStep: 20, smallStep: 5, showSmallTicks: true },
changed: onCustomChanged
});
SliderCustomObj.appendTo("#custom-slider");
// sets required property in the FormValidator rules collection
var customOptions = {
rules: {
'custom-slider': {
validateHidden: true,
range: [validateRange, "You must select values between 40 and 80"]
}
}
};
// Initialize Form validation
var formCustomObj = new ej.inputs.FormValidator("#formCustomId", customOptions);
function onCustomChanged(args) {
// validate the slider value in the form
formCustomObj.validate();
}
function validateRange(args) {
return SliderCustomObj.value[0] >= 40 && SliderCustomObj.value[1] <= 80;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Range Slider</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Essential JS 2 Range Slider Control">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/30.1.37/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/30.1.37/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/30.1.37/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/30.1.37/ej2-buttons/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/30.1.37/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">
<div class="col-lg-12 control-section">
<div class="content-wrapper" style="margin-bottom: 25px;overflow-x: hidden">
<div class="form-title">
<span>Min</span>
</div>
<form id="formMinId" class="form-horizontal">
<div class="form-group">
<div class="e-float-input">
<div id="min-slider" name="min-slider"></div>
</div>
</div>
</form>
<div class="form-title">
<span>Max</span>
</div>
<form id="formMaxId" class="form-horizontal">
<div class="form-group">
<div class="e-float-input">
<div id="max-slider" name="max-slider"></div>
</div>
</div>
</form>
<div class="form-title">
<span>Value</span>
</div>
<form id="formValId" class="form-horizontal">
<div class="form-group">
<div class="e-float-input">
<div id="val-slider" name="val-slider"></div>
</div>
</div>
</form>
<div class="form-title">
<span>Range</span>
</div>
<form id="formRangeId" class="form-horizontal">
<div class="form-group">
<div class="e-float-input">
<div id="range-slider" name="range-slider"></div>
</div>
</div>
</form>
<div class="form-title">
<span>Custom</span>
</div>
<form id="formCustomId" class="form-horizontal">
<div class="form-group">
<div class="e-float-input">
<div id="custom-slider" name="custom-slider"></div>
</div>
</div>
</form>
</div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
<style>
.e-error,
.e-float-text {
font-weight: 500;
}
table,
td,
th {
padding: 5px;
}
.form-horizontal .form-group {
margin-left: 0;
margin-right: 0;
}
form {
border: 1px solid #ccc;
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.36);
border-radius: 5px;
background: #f9f9f9;
padding: 23px;
padding-bottom: 20px;
margin: auto;
max-width: 650px;
}
.form-title {
width: 100%;
text-align: center;
padding: 10px;
font-size: 16px;
font-weight: 600;
color: black;
}
</style>
</body>
</html>
#container {
visibility: hidden;
}
#loader {
color: #008cff;
font-family: 'Helvetica Neue', 'calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}