Solved

ASP.Net - Custom Validator Failing To Fire

Posted on 2004-04-06
8
473 Views
Last Modified: 2008-03-06
For some reason the custom validator I am trying to use does not get called when I submit the page unless I have entered some data into the control that is being validated.  Given what I am trying to do, I can't use a required field validator.  Here is the code for the validator:

<script language="javascript">
    function cvldCommentHTML(oSrc, args){
        // If there is an entry in txtComment, txtCommentHTML must be filled
        if (document.Form1.txtComment.value == "")
            args.IsValid = true;
        else
            if (document.Form1.txtCommentHTML.value == "")
                args.IsValid = false;
            else
                args.IsValid = true;
    }
</script>

<asp:CustomValidator
    id="cvldCommentHTML"
    runat="server"
    ErrorMessage="You must enter Comment HTML when a comment is specified."
    ControlToValidate="txtCommentHTML"
    ClientValidationFunction="cvldCommentHTML">
</asp:CustomValidator>

Basically I want the validator to check if anything is entered in "txtComment" and if there is there must also be something in "txtCommentHTML".  Is there any way I can do this without having to enter data into "txtCommentHTML" to make the validation function call?
0
Comment
Question by:Psychotext
[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
  • 4
  • 4
8 Comments
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 10764980
CVhange the function to this::

function cvldCommentHTML(oSrc, args){
        // If there is an entry in txtComment, txtCommentHTML must be filled
        if (document.Form1.txtComment.value != ""){
if (document.Form1.txtCommentHTML.value == ""){
                args.IsValid = false;
            else
                args.IsValid = true;
}}
       }
0
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 10765078
Actually, you will need to do the following: (not tested !)

function cvldCommentHTML(oSrc, args){
        // If there is an entry in txtComment, txtCommentHTML must be filled
        if (document.Form1.txtComment.value != ""){
              if (document.Form1.txtCommentHTML.value == ""){
                args.IsValid = false;}
            else{
                args.IsValid = true;
               }            
          }else{
           args.IsValid = false;
       }
}
0
 
LVL 2

Author Comment

by:Psychotext
ID: 10765079
Thank you, that code is better than mine - But perhaps I wasn't clear enough.  That validation function does not get called at all unless something is typed into "txtCommentHTML".  I can remove the checking code completely, put an alert in and it does not display (Unless "txtCommentHTML" contains text).
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 11

Expert Comment

by:Slimshaneey
ID: 10765309
I think the second one I posted should do just that...
0
 
LVL 2

Author Comment

by:Psychotext
ID: 10765374
No, none of the code gets called unless I have first typed something in to "txtCommentHTML".  I dont think the problem is with the Javascript function.
0
 
LVL 11

Accepted Solution

by:
Slimshaneey earned 150 total points
ID: 10765400
Sorry, maybe im not understanding.
If you want to first check if commentHTML has something, THEN there must be something in comment try this(Ive just switched the 2 texts logic):

function cvldCommentHTML(oSrc, args){
        // If there is an entry in txtComment, txtCommentHTML must be filled
        if (document.Form1.txtCommentHTML.value != ""){
              if (document.Form1.txtComment.value == ""){
                args.IsValid = false;}
            else{
                args.IsValid = true;
               }            
          }else{
           args.IsValid = false;
       }
}
0
 
LVL 2

Author Comment

by:Psychotext
ID: 10765442
I think I know where the confusion is, you want me to change the control to be validated to the "txtComment" control.  That way, the validate will always fire when data is entered.
0
 
LVL 2

Author Comment

by:Psychotext
ID: 10765484
Updated code:

<script language="javascript">
    function cvldComment(oSrc, args){
        // If there is an entry in txtComment, txtCommentHTML must be filled
        if (document.Form1.txtComment.value != "")
        {
            if (document.Form1.txtCommentHTML.value == "")
                args.IsValid = false;
            else
                args.IsValid = true;
        }
        else
            args.IsValid = true;
    }
</script>

<asp:CustomValidator
    id="cvldComment"
    runat="server"
    ControlToValidate="txtComment"
    ErrorMessage="You must enter Comment HTML when a comment is specified."
    ClientValidationFunction="cvldComment">
</asp:CustomValidator>

Works fine now, funny - Never really thought of validating the other control.  Thanks.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

634 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