Solved

Custom  Validator , .Net

Posted on 2009-05-14
6
170 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 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
Technology Partners: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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