Solved

ASP.Net - Custom Validator Failing To Fire

Posted on 2004-04-06
8
468 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I think the second one I posted should do just that...
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:Psychotext
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

15 Experts available now in Live!

Get 1:1 Help Now