Pass your own arguments to the ClientValidationFunction in a CustomValidator
When using a CustomValidator in asp.net you can specify a ClientValidationFunction to run on your client side. This function need to have 2 parameters (sender, args). The sender is the customvalidator control and the args is where you can specify the result of your validation(args.IsValid) and also access to the value of the control attached to you custom validator(args.Value). This is very useful for more complex validation on client side.
But what if you need to pass additional arguments. For example if you want to use the same client function for more than one control. Here is an option, you can add properties to your validation object, and then access them from the source parameter in your client function. Take a look at the example:
<head runat=”server”>
<title></title>
<script language=”JavaScript”>
function validateRadioButtons(sender, args) {
var radioButton1 = document.getElementById(sender.RB1);
var radioButton2 = document.getElementById(sender.RB2);
args.IsValid = radioButton1.checked != radioButton2.checked;
}
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
Question 1:
<asp:RadioButton ID=”RadioButton1″ runat=”server” Text=”Yes” />
<asp:RadioButton ID=”RadioButton2″ runat=”server” Text=”No” />
<asp:CustomValidator ID=”CustomValidator1″ ClientValidationFunction=”validateRadioButtons”
runat=”server” ErrorMessage=”CustomValidator”></asp:CustomValidator>
<br />
Question 2:
<asp:RadioButton ID=”RadioButton3″ runat=”server” Text=”Yes” />
<asp:RadioButton ID=”RadioButton4″ runat=”server” Text=”No” />
<asp:CustomValidator ID=”CustomValidator2″ ClientValidationFunction=”validateRadioButtons”
runat=”server” ErrorMessage=”CustomValidator”></asp:CustomValidator>
<br />
Question 3:
<asp:RadioButton ID=”RadioButton5″ runat=”server” Text=”Yes” />
<asp:RadioButton ID=”RadioButton6″ runat=”server” Text=”No” />
<asp:CustomValidator ID=”CustomValidator4″ ClientValidationFunction=”validateRadioButtons”
runat=”server” ErrorMessage=”CustomValidator”></asp:CustomValidator>
<br />
</div>
</form>
</body>
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterExpandoAttribute(CustomValidator1.ClientID, “RB1″, RadioButton1.ClientID, false);
Page.ClientScript.RegisterExpandoAttribute(CustomValidator1.ClientID, “RB2″, RadioButton2.ClientID, false);
Page.ClientScript.RegisterExpandoAttribute(CustomValidator2.ClientID, “RB1″, RadioButton3.ClientID, false);
Page.ClientScript.RegisterExpandoAttribute(CustomValidator2.ClientID, “RB2″, RadioButton4.ClientID, false);
Page.ClientScript.RegisterExpandoAttribute(CustomValidator3.ClientID, “RB1″, RadioButton5.ClientID, false);
Page.ClientScript.RegisterExpandoAttribute(CustomValidator3.ClientID, “RB2″, RadioButton6.ClientID, false);
}
I tried your method and works great. But when I tried to open the old records which were entered before this validator and edit it, it gives me ‘object required’ error in javascript!!!
Could you be more specific or paste your code please.
Hello from Russia!
Can I quote a post in your blog with the link to you?
Of course feel free to do that.
Hi,
This is a great post. I am trying to do something very similar, but am having trouble. I need to do the same sort of thing, but with a textbox instead. I need to read something from another textbox, to determine the validation of another textbox. Here is the code.
//javascript in seperate file being referenced by .aspx file
function CheckInsCAGE(sender, arguments) {
//var insPart = document.getElementById(“”).value
var insPart = document.getElementById(sender.inspart).Value;
var expr = /^[A-Z 0-9]*$/;
if (arguments.Value == “” && insPart == “”)
arguments.IsValid = true;
else if (arguments.Value == “” && insPart != “”)
arguments.IsValid = false;
else if (arguments.Value != “” && insPart != “” && !expr.test(arguments.Value))
arguments.IsValid = false;
sender.errormessage = “If Installed Part is entered, then Installed CAGE be entered and must contain 5 characters or less”;
}
//Code behind page load
Page.ClientScript.RegisterExpandoAttribute(cusValInsCAGE.ClientID, “inspart”, txtInsPart.ClientID, false);
.aspx code
It even works to include the ExpandoAttribute direct into the .aspx
ASPX:
JS:
var id = sender.MyCustomAttrib;
Dang… Your blog ate my ASPX Line…
[ asp:CustomValidator ID="cust_val_foo" runat="server" ClientValidationFunction="check_foo" MyCustomAttrib="2" /]
This solved my problem, great work!