Solved

ASP.Net - Custom Validator Failing To Fire

Posted on 2004-04-06
8
470 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
  • 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
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 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 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