?
Solved

Custom  Validator , .Net

Posted on 2009-05-14
6
Medium Priority
?
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 9

Accepted Solution

by:
tetorvik earned 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 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