Asp.Net Form Validation using JavaScript how to validate form using Regular Expression
Introduction : This Artical explain How to validate Asp.net Form Using JavaScript with Regular Expression Email ,Phone,Compare Password etc....
Step 1 : Create Asp.net Web project and add Form With Following Control
Step2 : Validate Form With following step you can use Controller id to validate can add script between head tag
<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>
Step 3: Now you can return Above function In Button click event IN BUTTON
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
OnClientClick="return validate();" Text="Submit" style="height: 26px" />
Run Project And Check :
OnClientClick="return validate();" Text="Submit" style="height: 26px" />
Run Project And Check :
See Other Tutorial :
* AngularJS Crude With ASP.NET MVC
* Convert Rows to columns using 'Pivot' in SQL Server
* Mvc Registration page With user exist using Ajax method
* MVC 4 How to Perform Insert Update Delete Edit Select Operation
* MVC4 Edit,update,Delete,cancel inside gridview using sql database
* MVC 4 Gridview To Display Data Using SQL Server Database With Simple code
* Login page in asp.net Mvc4 Web application
* Mvc4 How to bind Dropdown List using Sql Database
* Gridview find control in asp.net
Sign up here with your email
ConversionConversion EmoticonEmoticon