In this article, we will cover the following.
- Why do we need validation?
- Types of Validation : Client Side and Server Side
- Client Side Validation Code
- Server Side Validation Code
- Validation in WebForm
Why do we need validation?
Validation is the main threshold to process the data. We store the textbox value into a database table. Before moving to storing the data into the database, first, we check the value of each textbox.
Example
As in the above Form image, we have following fields:
Name, Age, Mobile, Email ID.
I have given a short explanation in the image before moving the form data to a database table.
Other validation :
- Age > 100 or 125 check if the length is not more than 3 digits.
- Mobile number length must not be more than 10 digits and less than 10 digits.
Validation is required before accepting the data because, in future, we process and generate the reports as per the received data.
Types of Validation
In web technology, there are two types of validation.
- Client Side
- Server Side
Client-side Validation
Before validating data in the server, first, we validate it on the client side. User input that is validated in the browser before submitting to the server is called Client-side validation.Server-side validation
This validation is executed after client-side validation if we have it. But we should or must have server-side validation. A user or hacker can submit the data through different channels also.
Server-side validation comes into the picture/existence while the user submits or posts back.
Client Side Validation Code
We can achieve client side validation with the help of following:
- JavaScript
- jQuery
In this article, I had used JavaScript to validate the TEXTBOX. To know more about JavaScript, please visit the following link:
https://en.wikipedia.org/wiki/JavaScript
There are three main functions which validate the Number, Email ID, and entire form fields validator. These functions require textbox object only. Everything else is done by the function itself.
Client Side Number Validator Function
- function checkEmail(event) {
- var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
- if (!re.test(event.value)) {
- alert("Please enter a valid email address");
- return false;
- }
- return true;
- }
This function restricts the TextBox to accept only numeric keys. There is no impact on textbox if other keys are pressed.
You can see that the heart of the above code is charCode and charCode is nothing but a keyCode. keyCode is property of the pressed keyboard key. Every key having there own keycode, like ASCII
A - 65, B - 66.
For more details, you can visit this link,
https://www.w3schools.com/jsref/event_key_keycode.asp
Client Side Email Validator Function
- function checkEmail(event) {
- var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
- if (!re.test(event.value)) {
- alert("Please enter a valid email address");
- return false;
- }
- return true;
- }
Client Side Email Validator Function
- function chkvalidation()
- {
- var exitval = false;
- var rsltcount = 0;
- var msgtext = "";
-
-
- var ageval = document.getElementById("<%= txtAge.ClientID %>").value;
- var agerslt = false;
- if (ageval.length > 0)
- {
- agerslt = OnlyNumbers(document.getElementById("<%= txtAge.ClientID %>"));
- if (agerslt == false)
- {
- msgtext = "Invalid age entered.";
- rsltcount = 1
- }
- else
- {
- if (ageval > 100) {
- msgtext = msgtext + "\n Check your entered age.";
- rsltcount += 1
- }
- }
- }
- else
- {
- msgtext = msgtext + "\n Invalid age or age required.";
- rsltcount += 1
- }
-
-
- var mobileval = document.getElementById("<%= txtMobile.ClientID %>").value;
- var mobilerslt = false;
- if (mobileval.length != 10) {
- msgtext = msgtext + "\n Invalid mobile number or mobile number required";
- rsltcount += 1
- }
- else {
- mobilerslt = OnlyNumbers(document.getElementById("<%= txtMobile.ClientID %>"));
- if (mobilerslt == false) {
- msgtext = msgtext + "\n Invalid mobile number or mobile number required.";
- rsltcount += 1
- }
- }
-
-
- var emailidval = document.getElementById("<%= txtEmailID.ClientID %>").value;
- var emailidrslt = false;
- if (emailidval.length > 1) {
- emailidrslt = checkEmail(document.getElementById("<%= txtEmailID.ClientID %>"));
- if (emailidrslt == false) {
- msgtext = msgtext + "\n Invalid email id entered.";
- rsltcount += 1
- }
- }
- else {
- msgtext = msgtext + "\n Email id required.";
- rsltcount += 1
- }
-
- if (rsltcount > 0)
- {
- exitval = false;
- }
- else
- {
- exitval = true;
- }
-
- alert(msgtext);
- return exitval;
- }
Above function is used to validate the Age, Mobile, and Email text-box of the form.
Right click on project title “TextBoxValidateWebForm” --> Select “Add” -->Select “Web Form”.
This will prompt you for file name.
Provide a file name such as “myprofile”.
After clicking on OK button, you can see your solution explorer which should look like this:
In myprofile, we will create a form which has the same fields as in the above image.
- Name
- Age
- Mobile
- Email ID
Let's create a tag of the above fields in MYPROFILE.ASPX page.
You page will look like this.
Client side blank submission errors:
Server side blank submission errors -
Code of MyProfile.aspx
Code of MyProfile.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class myprofile : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- string MsgText = "";
- Int32 rsltcount = 0;
-
-
-
- bool ageValStatus = VerifyNumericValue(txtAge.Text);
- if (ageValStatus == false)
- {
- rsltcount += 1;
- MsgText += "Invalid age or age required.</br>";
- }
- if (ageValStatus == true)
- {
- if(Convert.ToDecimal(txtAge.Text) > 100)
- {
- rsltcount += 1;
- MsgText += " Check your entered age.</br>";
- }
- }
-
-
- bool mobileValStatus = VerifyNumericValue(txtMobile.Text);
- if (mobileValStatus == false)
- {
- rsltcount += 1;
- MsgText += "Invalid mobile number or mobile number required.</br>";
- }
- if (mobileValStatus == true)
- {
- if (txtMobile.Text.Length != 10)
- {
- rsltcount += 1;
- MsgText += "Check your entered mobile number.</br>";
- }
- }
-
-
- bool emailidValStatus = VerifyEmailID(txtEmailID.Text);
- if (emailidValStatus == false)
- {
- rsltcount += 1;
- MsgText += "Invalid email id or email id required.</br>";
- }
-
- lblResultMessage.Text = MsgText;
- lblResultMessage.Font.Bold = false;
- lblResultMessage.Font.Size = 14;
- lblResultMessage.ForeColor = System.Drawing.Color.Red;
-
- }
-
- public bool VerifyNumericValue(string ValueToCheck)
- {
- Int32 numval;
- bool rslt = false;
-
- rslt = Int32.TryParse(ValueToCheck, out numval);
-
- return rslt;
- }
-
- public static bool VerifyEmailID(string email)
- {
- string expresion;
- expresion = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
- if (Regex.IsMatch(email, expresion))
- {
- if (Regex.Replace(email, expresion, string.Empty).Length == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- }