Client Side Validation with JavaScript in ASP.NET :-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="Validation.Employee" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
////java script code///
<script type="text/javascript">
function validation() {
var errorsummary = "";
errorsummary += checkname();
errorsummary += checkpassword();
if(errorsummary !="")
{
alert(errorsummary);
return false;
}
}
function checkname() {
var TN = document.getElementById('txtname');
var exp = /^[a-zA-Z ]+$/
if (TN.value== "") {
return 'please enter your name !!\n';
}
else if (exp.test(TN.value))
{
return "";
}
else {
return 'plaeese enter only alphabets !!\n';
}
}
function checkpassword() {
var TP = document.getElementById('txtpassword');
if (TP.value == "") {
return 'please enter your password !!\n';
}
else {
return "";
}
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Name :</td>
<td><asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Password :</td>
<td><asp:TextBox ID="txtpassword" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnsave" runat="server" Text="Save" OnClientClick="return validation()" OnClick="btnsave_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
No comments:
Post a Comment