Solved

ASP.Net - Custom Validator Failing To Fire

Posted on 2004-04-06
8
469 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
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 10765309
I think the second one I posted should do just that...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 information …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

896 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

11 Experts available now in Live!

Get 1:1 Help Now