Solved

Custom  Validator , .Net

Posted on 2009-05-14
6
167 Views
Last Modified: 2013-12-17
I want to design a custom validator , that fires only when the element which it validates is visible in the form, otherwise it should not fire.

I know that the javascript function for Custom Validator takes the source and eventargs parameters, but how can I get the ID of the element on which the custom validator was applied ?

If I get to know that I can handle the rest . Please tell me how can I do that ?
0
Comment
Question by:puneetdudeja
  • 3
  • 3
6 Comments
 
LVL 9

Accepted Solution

by:
tetorvik earned 500 total points
ID: 24382806
in javascript use source.controltovalidate for this
0
 
LVL 9

Expert Comment

by:tetorvik
ID: 24382847
sample below:
js:
        function Validate(source, args) {
              if (document.getElementById(source.controltovalidate).value == "invalidString") {
                   args.IsValid = false;
                }
            }
 
aspx:
    <asp:CustomValidator ID="CustomValidator1"
            runat="server" ErrorMessage="CustomValidator" 
            ControlToValidate="someTextBox" ClientValidationFunction="Validate">
     </asp:CustomValidator>

Open in new window

0
 
LVL 2

Author Comment

by:puneetdudeja
ID: 24382987
source.controltovalidate is undefined when the Validate function gets called.

It's not working.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 9

Expert Comment

by:tetorvik
ID: 24383026
it's working here. do you have ControlToValidate set up correctly? Like below:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1"
            runat="server" ErrorMessage="CustomValidator" ControlToValidate="TextBox1" ClientValidationFunction="Validate"></asp:CustomValidator>

Open in new window

0
 
LVL 2

Author Comment

by:puneetdudeja
ID: 24383116
Your above code is working fine.
But what I need to do is in the following function which is not working :

function validate(source,e)
    {
        if(document.getElementById(source.controltovalidate).style.display=='none')
            e.IsValid=true;
            else
            {
                      if(document.getElementById(source.controltovalidate).value=='')
                             e.IsValid=false;
                     else
                           e.IsValid=true;
            }
    }
0
 
LVL 2

Author Comment

by:puneetdudeja
ID: 24383220
I have found the problem in my function.

thanks for your help.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now