Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.Net - Custom Validator Failing To Fire

Posted on 2004-04-06
8
Medium Priority
?
477 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
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 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 450 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2

783 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