Custom Validator: Pass custom parameter to clientside validation function from code behind

Hello readers,

It is common we uses all kind of validators in our daily programming. Custom Validator is one of them and may be very interesting too.

Recently i was needed to add one Custom Validator to the page and i was needed to validate a control dynamically based on condition. so that i was needed to pass some custom parameter from code behind to clientside validation function based on which i will determine which control to validate.

Please refer below code to achieve this:



            function OnClientValidate(sender, args) {
                args.IsValid = sender.CustomParameter;


 protected void Page_Load(object sender, EventArgs e)
        bool HasQuerystring = false;
        if (!string.IsNullOrEmpty(Request.QueryString["q"]))
            HasQuerystring = true;
        Page.ClientScript.RegisterExpandoAttribute(cmtxtForm.ClientID, "CustomParameter", HasQuerystring.ToString(), false);

It was so easy right? Thank you 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s