How can I implement a clientsidevalidation required textfields which contain the following texts:
[Name required]
[Address required]
Preferrably I would like to use the asp:CustomValidator if possible? Only if both fields have data the postback will be triggered?
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript"> var $j = jQuery.noConflict();</script>
<script type="text/javascript">
function otherMessageValidator_ClientValidation(source, args) {
args.IsValid = false;
var nm = $j("#name");
if (nm.val() != "" && nm.val() != "[Name required]") {
args.IsValid = true;
}
return args.IsValid;
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="name" runat="server" ValidationGroup="valgroup">[Name required]</asp:TextBox>
<asp:TextBox ID="address" runat="server" ValidationGroup="valgroup">[Address required]</asp:TextBox>
</div>
<asp:Button runat="server" ID="but1" Text="go" OnClick="but1_Click" />
<asp:CustomValidator ID="MyCustomValidator" runat="server" ValidationGroup="valgroup"
ClientValidationFunction="otherMessageValidator_ClientValidation" ErrorMessage="At least one textbox needs to be filled in." />
</form>
</body>
</html>
One way to achieve it is by using regular expression from javascript and validation it client side only. So in the mentioned code it will only go to server if all the required validations are fulfilled else it will show the Error message on the screen in the Summary div tag.
Following is the required code.
and we need to take one div for displaying validationsummary and the button
Hope it will solve your issue