Asp.Net form validation using JavaScript

In this article we have learn Asp.Net form validation using JavaScript,Validation done in the browser before sending the form data to the server using JavaScript, jQuery and VBScript is called client-side validation

In asp.net validation is two type called Client side validation and server side validation

Client side validation :

Validation done in the browser before sending the form data to the server using JavaScript, jQuery and VBScript is called client-side validation

Server side validation :

Validation done at the server level after sending the form data to the server but before entering the data into the database is called server-side validation.

Step 1 : Create Asp.net Web project and add Form With Following Control

Asp.Net form validation using JavaScript

In asp.net form you can add below code and validate the form using JavaScript without use of server load easy validate the form asp.net c# form.

<head id=”Head1” runat=”server”>

<script src=”Scripts/jquery.1.4.2.js” type=”text/javascript“></script>

<script language=”javascript” type=”text/javascript“>

function validate() {

var name = document.getElementById(“txtname“).value;
if(name==””)
{
alert(“Name are Required ??“);
document.getElementById(“txtname“).focus();
return false;
}

var PWD = document.getElementById(“pwd“).value;

if (PWD== “”) {
alert(“password required ???“);
return false;
}
var CPWD = document.getElementById(“cpwd”).value;
if (PWD != “” && CPWD == “”) {
alert(“Cpassword r required ???”);
return false;
}
if (PWD != CPWD) {

alert(“Conform Password are Required ???”);
return false;
}

var  emailExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([com\co\.\in])+$/;
var Email = document.getElementById(“email”).value;
if (Email == “”) {
alert(“Email r Required ???“);
return false;
}
if (Email != “”) {
if (!Email.match(emailExp)) {
alert(“Enter Email  is Not proper ??????“);
return false;
}
}

var phoneEXP = /^(\d{10})$/;
var Phone = document.getElementById(“phone”).value;
if (Phone == “”) {
alert(“Phone  r Required ???“);
return false;
}
if (Phone != “”) {
if (!Phone.match(phoneEXP)) {
alert(“Enter 10 Digit Number  ??????“);
return false;
}
}

}
</script>

 </head>

<asp:Button ID=”Button1” runat=”server” onclick=”Button1_Click
OnClientClick=”return validate();” Text=”Submit” style=”height: 26px” />

Now run asp.net c# project and see Asp.Net form validation using JavaScript

Asp.Net form validation using JavaScript

SEE MORE

 

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *