Introduction
Data validation is one of the most common but required functionalities in a modern Web page. Implementing data validation can be time-consuming for web developers. Infragistics provides a JavaScript validator control, IGValidator that can be used to validate a form’s input controls without writing too much code.
It can be used with most of the common form input elements such as Input, Radio button, and Checkbox. This control provides a very high interactive design for success and failure of data validation and because it’s written in JavaScript, which makes it very efficient and easy to code. IGValidator provides various validation rules such as required field and data types such as number, date, card, email, and also custom type. IGValidator is a part of
IgniteUI for JavaScript provided by Infragistics.
What we will cover in this article?
In this article, we’ll cover some of the common rules of input validation and how to apply them to various controls.
- Required Validation
- Compare Validation
- Masking
- Multi Selection in Dropdown
- Ignite Date Picker with validation
- Validate Common Controls (i.e textbox, dropdown, checkbox, datepicker, etc. )
Let's get started
Let’s start by creating a simple HTML page. You can use any tool of your choice. We’ll use an HTML page to implement validations so that you can implement in any project.
First, we need to add the CSS and CDN scripts provided by Infragistics as shown in Listing 1.
- <!-- CSS files -->
- <link href="http://cdn-na.infragistics.com/igniteui/2018.1/latest/css/themes/infragistics/ infragistics.theme.css" rel="stylesheet" />
- <link href="http://cdn-na.infragistics.com/igniteui/2018.1/latest/css/structure/infragistics.css" rel="stylesheet" />
- <link href="https://www.igniteui.com/igniteui/css/themes/infragistics/infragistics.theme.css" rel="stylesheet"/>
- <!-- Script files -->
- <script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.8.3.js"></script>
- <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
- <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
-
- <!--Ignite UI Required Combined JavaScript Files-->
- <script src="http://cdn-na.infragistics.com/igniteui/2018.1/latest/js/infragistics.core.js"> </script>
- <script src="http://cdn-na.infragistics.com/igniteui/2018.1/latest/js/infragistics.lob.js"></script>
Listing 1- Required Validation
Now, let’s add two simple HTML input controls for First Name and Last Name to validate as shown in Listing 2.
- <form id="validationForm" action="" method="post">
- <div class="group-fields clearfix">
- <div class="odd">
- <label>First name*</label>
- <input class="group" type="text" id="firstName" />
- </div>
- <div class="even">
- <label>Last name*</label>
- <input class="group" type="text" id="lastName" />
- </div>
- </div>
- </form>
Listing 2
To validate controls in the page, first, we need to mark the form tag as igValidator then in fields option add those fields and their validation options. We also use a custom regex function which returns true if the field has only uppercase or lowercase text. Refer to listing 3.
- $('#validationForm').igValidator({
- onsubmit: true,
- successMessage: "Valid",
- fields: [{
- required: true,
- selector: "#firstName",
- valueRange: [2],
- onblur: true,
- custom: function (value, fieldOptions) {
- var myRegEx = /^[a-zA-Z]+$/;
- var isValid = myRegEx.test(value);
- return isValid;
- }
- },
- {
- required: true,
- selector: "#lastName",
- valueRange: [2],
- onblur: true,
- custom: function (value, fieldOptions) {
- var myRegEx = /^[a-zA-Z]+$/;
- var isValid = myRegEx.test(value);
- return isValid;
- }
- }]
- );
Listing 3
Hit save and open the HTML page in the Web browser. Refer to Figure 1.
Figure 1
To check if two input fields have the same values or not, we use compare validation.
Let’s add two common fields which mostly use comparison validation like password as shown in Listing 4
- <div class="group-fields clearfix">
- <div class="odd">
- <label>Create password*</label>
- <input class="group" type="password" id="createPassword" />
- </div>
- <div class="even">
- <label>Confirm password*</label>
- <input class="group" type="password" id="confirmPassword" />
- </div>
- </div>
Listing 4
Let's add javascript to make the password and the confirm password work and also add the regex for password strength as shown in Listing 5.
- $('#validationForm').igValidator({
- onsubmit: true,
- successMessage: "Valid",
- fields: [
- {
- required: true,
- selector: "#createPassword",
- onblur: true,
- errorMessage: "Should contain at least 8 characters, 1 number, 1 lowercase
- character (a-z), 1 uppercase character",
- custom: function(value, fieldOptions){
- var myRegEx = /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8,})$/;
- var isValid = myRegEx.test(value);
- return isValid;
- }
- },
- {
- required: true,
- selector: "#confirmPassword",
- equalTo: "#createPassword",
- onblur: true,
- }]
- });
Figure 2
We can mask any input field using IgMasking.
Let’s say we have to mask a phone number, add another input field as shown in Listing 6.
- <div class="even">
- <label>Mobile phone</label>
- <input class="group" type="text" id="phone" />
- </div>
Listing 6
After adding Html, we can write 3 lines of code to implement masking as shown in Listing 7. This code is simply placed in the load function of the page.
- $("#phone").igMaskEditor({
- inputMask: "(\+\\91) 0000-000000" });
Listing 7
The output is like Figure 3.
Figure 3
Multi Selection in Dropdown
First, let's add simple div in html with id as shown in Listing 7.
- <div class="odd">
- <label>Your interests (Select at least 2)*</label>
- <div id='igComboInterests'></div>
- </div>
Listing 7
Now let’s create some data and then bind that data to a div and then enable multi selection feature as shown in Listing 8.
- var data = [
- { ID: "ID101", Name: "Business", Code: 101 },
- { ID: "ID102", Name: "Cooking", Code: 102 },
- { ID: "ID103", Name: "Fashion", Code: 103 },
- { ID: "ID104", Name: "Lifestyle", Code: 104 },
- { ID: "ID105", Name: "Photography", Code: 105 },
- { ID: "ID106", Name: "Sports", Code: 106 }];
-
- $('#igComboInterests').igCombo({
- inputName: "products",
- dataSource: data,
- allowCustomValue: true,
- textKey: 'Name',
- valueKey: "ID",
- multiSelection: {
- enabled: true
- }
- });
-
- ('#validationForm').igValidator({
- onsubmit: true,
- successMessage: "Valid",
- fields: [
- required: true,
- selector: "#igComboInterests",
- onchange: true,
- lengthRange: [2]]
- ;
Listing 8
The output will be as in Figure 4.
Figure 4
Ignite Date Picker with validation
Let’s add input tag inside div as shown in Listing 9.
- <div class="odd">
- <label>Date of birth*</label>
- <input class="group" id="dateOfBirth" />
- </div>
Listing 9
Now add javascript for Ignite DatePicker and validation as in Listing 10. When we mark an input as igDatePicker, it restricts the user to enter text in a field by default. We can also set the minimum and maximum dates for DatePicker.
- $("#dateOfBirth").igDatePicker({
- datepickerOptions: { minDate: new Date(1920, 0, 1), maxDate: new Date() }
- });
-
- $('#validationForm').igValidator({
- onsubmit: true,
- successMessage: "Valid",
- fields: [
-
- required: true,
- selector: "#dateOfBirth",
- date: true,
- onblur: true
- }]
- );
Listing 10
When you click on the datepicker default Infragistics Date will pop up. Refer to Figure 5.
To make the form look better I’ve also added some CSS to the form which is as shown in Listing 11.
- <style>
- .group-fields {
- margin-bottom:20px;
- }
- .group-fields .odd {
- float:left;
- width:262px;
- }
- .group-fields .even{
- float:left;
- margin-left:20px;
- }
- .group-fields label {
- display: block;
- line-height: 20px;
- font-family: 'Open Sans',Arial,sans-serif;
- font-size: 13px;
- }
- .group-fields .ui-igedit-container {
- box-sizing: content-box;
- }
- .group-fields input.group {
- width:258px;
- height:32px;
- }
- .group-fields .ui-igedit-container,.group-fields .ui-igcombo-wrapper {
- width:260px;
- height:34px;
- }
- .group-fields .ui-igpopover {
- width:260px;
- }
- .group-fields input.group {
- border-style: solid;
- border-width: 1px;
- border-color:#bcbcbc;
- }
- .group-fields input.group:hover {
- border-color:#444;
- }
- .group-fields .ui-widget.ui-ignotify.ui-ignotify-inline {
- margin-bottom:0;
- }
- .group-fields input.group.ui-ignotify-error {
- border-color:#f33;
- }
- #igCheckboxAccept, #igCheckboxSubscribe {
- float:left;
- margin-right: 5px;
- }
- .group-fields .inline {
- display:inline;
- }
- .clearfix:after {
- content: "";
- display: table;
- clear: both;
- }
- @media screen and (max-width:610px) {
- .group-fields .odd {
- float:left;
- width:202px;
- }
- .group-fields input.group {
- width:200px;
- }
- .group-fields .ui-igedit-container,.group-fields .ui-igcombo-wrapper {
- width:202px;
- }
- .group-fields .ui-igpopover {
- width:202px;
- }
- }
- @media screen and (max-width:478px) {
- .group-fields {
- margin-bottom:0;
- }
- .group-fields .odd, .group-fields .even {
- margin-bottom: 5px;
- }
- .group-fields .odd {
- float:none;
- width:262px;
- }
- .group-fields .even {
- margin-left:0;
- }
- .group-fields input.group {
- width:258px;
- height:32px;
- }
- .group-fields .ui-igedit-container,.group-fields .ui-igcombo-wrapper {
- width:260px;
- height:34px;
- }
- .group-fields .ui-igpopover {
- width:260px;
- }
- }
- </style>
Listing 11
Validate Common Controls
To merge all controls of Html and Javascript refer to Listing 12 & Listing 13.
- <form id="validationForm" action="" method="post">
- <div class="group-fields clearfix">
- <div class="odd">
- <label>First name*</label>
- <input class="group" type="text" id="firstName" />
- </div>
- <div class="even">
- <label>Last name*</label>
- <input class="group" type="text" id="lastName" />
- </div>
- </div>
- <div class="group-fields clearfix">
- <div class="odd">
- <label>Email*</label>
- <input class="group" type="text" id="email" />
- </div>
- <div class="even">
- <label>Mobile phone</label>
- <input class="group" type="text" id="phone" />
- </div>
- </div>
- <div class="group-fields clearfix">
- <div class="odd">
- <label>Create password*</label>
- <input class="group" type="password" id="createPassword" />
- </div>
- <div class="even">
- <label>Confirm password*</label>
- <input class="group" type="password" id="confirmPassword" />
- </div>
- </div>
- <div class="group-fields clearfix">
- <div class="odd">
- <label>Date of birth*</label>
- <input class="group" id="dateOfBirth" />
- </div>
- <div class="even">
- <label>Gender*</label>
- <input id='gender' />
- </div>
- </div>
- <div class="group-fields clearfix">
- <div class="odd">
- <label>Your interests (Select at least 2)*</label>
- <div id='igComboInterests'></div>
- </div>
- </div>
- <div class="group-fields clearfix">
- <div class="odd">
- <label class="inline">I accept the Terms and Conditions *</label>
- <div id="igCheckboxAccept"></div>
- </div>
- <div class="even">
- <label class="inline">Subscribe for our daily mail digest</label>
- <div id="igCheckboxSubscribe"></div>
- </div>
- </div>
- <input type="submit" value="Sign Up" />
- </form>
Listing 12
- <script>
- $(function () {
- var data = [
- { ID: "ID101", Name: "Business", Code: 101 },
- { ID: "ID102", Name: "Cooking", Code: 102 },
- { ID: "ID103", Name: "Fashion", Code: 103 },
- { ID: "ID104", Name: "Lifestyle", Code: 104 },
- { ID: "ID105", Name: "Photography", Code: 105 },
- { ID: "ID106", Name: "Sports", Code: 106 }];
-
- $('#gender').igTextEditor({
- inputName: "gender",
- listItems: ["Male", "Female"]
- });
-
- $("#phone").igMaskEditor({
- inputMask: "(\\0\\01) 000-0000"
- });
-
- $('#igComboInterests').igCombo({
- inputName: "products",
- dataSource: data,
- allowCustomValue: true,
- textKey: 'Name',
- valueKey: "ID",
- multiSelection: {
- enabled: true
- }
- });
- ….
-
- $("#dateOfBirth").igDatePicker({
- datepickerOptions: { minDate: new Date(1920, 0, 1), maxDate: new Date(2015, 0, 1) }
- });
-
- $("#igCheckboxAccept").igCheckboxEditor();
- $("#igCheckboxSubscribe").igCheckboxEditor();
- $('#validationForm').igValidator({
- onsubmit: true,
- successMessage: "Valid",
- fields: [{
- required: true,
- selector: "#firstName",
- valueRange: [2],
- onblur: true,
- custom: function (value, fieldOptions) {
- var myRegEx = /^[a-zA-Z]+$/;
- var isValid = myRegEx.test(value);
- return isValid;
- }
- },
- {
- required: true,
- selector: "#lastName",
- valueRange: [2],
- onblur: true,
- custom: function (value, fieldOptions) {
- var myRegEx = /^[a-zA-Z]+$/;
- var isValid = myRegEx.test(value);
- return isValid;
- }
- },
- {
- required: true,
- selector: "#dateOfBirth",
- date: true,
- onblur: true
- },
- {
- required: true,
- selector: "#gender",
- gender: true,
- onblur: true
- },
- {
- required: true,
- selector: "#email",
- email: true,
- onblur: true
- },
- {
- required: true,
- selector: "#createPassword",
- onblur: true,
- errorMessage: "Should contain at least 8 characters, 1 number, 1 lowercase
- character (a-z), 1 uppercase character",
- custom: function(value, fieldOptions){
- var myRegEx = /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8,})$/;
- var isValid = myRegEx.test(value);
- return isValid;
- }
- },
- {
- required: true,
- selector: "#confirmPassword",
- equalTo: "#createPassword",
- onblur: true,
- },
- {
- required: true,
- selector: "#igComboInterests",
- onchange: true,
- lengthRange: [2]
- },
- {
- required: true,
- required: true,
- selector: "#igCheckboxAccept",
- onchange: true
- },
- {
- selector: "#igCheckboxSubscribe",
- onchange: true
- }
- ]
- });
- });
- </script>
Listing 13
The final output of the code will like Figure 6.
Figure 6
Conclusion
IGValidator provides an easy and simple way to implement data validations in HTML and web pages using JavaScript. It also helps to maintain a centralized method to validate the entire web page. In this article, I have demonstrated how to control IGValidator to validate various input controls on a web page.