if you want to use both server side validation and javascript in one page then you have to include Page_ClientValidate(); in Javascript otherwise it will not check the server side validation..In Actual senario browser check the server side validation and display the error but javascript will return the true value so it will go ahead with the next code...for Example
.aspx page code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function validate() {
if (document.getElementById('ddlfrommonth').selectedIndex >= document.getElementById('ddltomonth').selectedIndex) {
alert("Error");
return false;
}
else {
Page_ClientValidate();
return Page_IsValid;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtname" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="reqtxtname" runat="server" ControlToValidate="txtname" ErrorMessage="name required" Text="name required"></asp:RequiredFieldValidator>
From
<asp:DropDownList ID="ddlfrommonth" runat="server">
<asp:ListItem Selected="True" Text="Select"></asp:ListItem>
<asp:ListItem Text="January"></asp:ListItem>
<asp:ListItem Text="February"></asp:ListItem>
<asp:ListItem Text="March"></asp:ListItem>
<asp:ListItem Text="April"></asp:ListItem>
<asp:ListItem Text="May"></asp:ListItem>
<asp:ListItem Text="June"></asp:ListItem>
<asp:ListItem Text="July"></asp:ListItem>
<asp:ListItem Text="August"></asp:ListItem>
<asp:ListItem Text="September"></asp:ListItem>
<asp:ListItem Text="October"></asp:ListItem>
<asp:ListItem Text="November"></asp:ListItem>
<asp:ListItem Text="December"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="ReqFrommonth" runat="server" InitialValue="Select" ControlToValidate="ddlfrommonth" ErrorMessage="From Month Required" Text="*"></asp:RequiredFieldValidator>
To
<asp:DropDownList ID="ddltomonth" runat="server">
<asp:ListItem Selected="True" Text="Select"></asp:ListItem>
<asp:ListItem Text="January"></asp:ListItem>
<asp:ListItem Text="February"></asp:ListItem>
<asp:ListItem Text="March"></asp:ListItem>
<asp:ListItem Text="April"></asp:ListItem>
<asp:ListItem Text="May"></asp:ListItem>
<asp:ListItem Text="June"></asp:ListItem>
<asp:ListItem Text="July"></asp:ListItem>
<asp:ListItem Text="August"></asp:ListItem>
<asp:ListItem Text="September"></asp:ListItem>
<asp:ListItem Text="October"></asp:ListItem>
<asp:ListItem Text="November"></asp:ListItem>
<asp:ListItem Text="December"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="Reqddltomonth" runat="server" InitialValue="Select" ControlToValidate="ddltomonth" ErrorMessage="To Month Required" Text="*"></asp:RequiredFieldValidator>
<asp:Button ID="btntest" runat="server" Text="Validate"
OnClientClick="return validate();" onclick="btntest_Click"/>
</div>
</form>
</body>
</html>
.aspx.cs page code
protected void btntest_Click(object sender, EventArgs e)
{
Response.Redirect("~/test.aspx");
}
if you dont include the Page_ClientValidate and return Page_IsValid; in Page then it will simply redirect the test.aspx page without if you dont write anything in textbox..means require field for Textbox will not be executed....
0 comments:
Post a Comment