Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 12th, 2006, 07:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default Interesting validation problem

Now here is an interesting problem :)

I am trying to do validation of a text field which is required to have a value; i.e. like the RequiredFieldValidator is doing, but... The problem is that I only want the validation if the field is visble. A small sample of reason is included here (extract from asp.net generated page)...
Code:
<html>
<head>
    <script type="text/javascript" language="javascript">
    function toggleVisibility(id1)
    {
        var o1 = document.getElementById(id1);
        o1.style.display =
            ((o1.style.display != "none")? "none" : "");
    }
</script>
</head>
<body>
<table cellspacing="6" cellpadding="0" border="0">
    <tr>
        <td align="Right" valign="Top"><span class="inputLabel">include link</span></td>
        <td valign="Top"><input id="ocms_pageLink" type="checkbox" name="ocms:pageLink"
            onclick="toggleVisibility('ocms_pageTitleLinkComponent');" /></td><td valign="Top"></td>
    </tr>
    <tr id="ocms_pageTitleLinkComponent" style="display:none;">
        <td align="Right" valign="Top"><span class="inputLabel">*link title</span></td>
        <td valign="Top" colspan="2"><input name="ocms:pageTitleLink" type="text" id="ocms_pageTitleLink" /></td>
    </tr>
</table>
</body>
</html>
That is, the visibility of some fields are toggled using a script and I only want the validation to occur while visible. So I cannot use a RequiredFieldValidator since it will validate dispite the visibility. Therefore, I set out to create my own CustomValidator (using client-side scripting). After quite some debugging I final got the client-side script to fire... sometimes!

The client-side script I made asked about the display style property of the element, and if set to block I would validate the field. The problem seems to be that the script is only firing when there is in fact a value in the field. The reason for this is to be found in WebUIValidation.js, which is part of the ASP.NET framework.
The problematic function is this one...
Code:
function CustomValidatorEvaluateIsValid(val) {
    var value = "";
    if (typeof(val.controltovalidate) == "string")
    {
        value = ValidatorGetValue(val.controltovalidate);
        if (ValidatorTrim(value).length == 0)
            return true;
    }
    var args = { Value:value, IsValid:true };

    if (typeof(val.clientvalidationfunction) == "string") {
        eval(val.clientvalidationfunction + "(val, args) ;");
    }
    return args.IsValid;
}
So, if the length of the value of the field is 0 then the validation passes! Therefore, I could solve the problem by changing the function, but it is not a possible solution since I would have to do this on all the machines on which the system is deployed. So there must be another solution.

I have tried to apply a RequiredFieldValidator to the control, and then add dummy text to the field when not visible and the remove it when visible, but wasn't able to make it work.

Summary: I want to do a required field validation on a text field but only when it is visible (display:block). Does anyone have the same problem? Do you have a solution? I am interesed in this solution.

Thanks a lot, Jacob.
__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
 
Old February 12th, 2006, 07:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Alright, I finally made it work. I guess the process of writing down problem often helps solving it (self-coaching). Anyways... the solution where dummy data is written to the field when not displayed worked (not a pretty solution but it works). I forgot the block part in the code underneath...
Code:
<script type="text/javascript" language="javascript">
function toggleVisibility(id1)
{
    var o1 = document.getElementById(id1);
    o1.style.display =
        ((o1.style.display != "none")? "none" : "block");
}
</script>
The dummy data is written with this function...
Code:
<script language="javascript">
function writeDummyInFieldWhenHidden(id1, id2)
{
    document.getElementById(id1).value =
        ((document.getElementById(id2).style.display == "none")? 
            "[dummy]" : "");
}
</script>
... and then it works!

If anyone has another more pretty solution I am interested, and if anyone should want a more detailed description of the solution above let me know.

Thanks, Jacob.
 
Old February 12th, 2006, 08:17 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Jacob,

Why would you need to deploy the file WebUIValidation.js to all clients? If you have this file on your web server, it's streamed to the client's browser automatically whenever validation controls are used.

Did you look into a custom validator? There is no need to modify the original script files, all you need to do is set the property, like this:

<asp:CustomValidator id="CustomValidator1" runat="server"
ClientValidationFunction="SomeClientSideMethod" ControlToValidate="SomeControl"></asp:CustomValidator>

With this validator, .NET ensures that SomeClientSideMethod is called when appropriate. Inside that (client side) method you can check whatever you need to do....

Does this help?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Familiar Feeling by Moloko (Track 1 from the album: Statues) What's This?
 
Old February 12th, 2006, 01:39 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar, thanks for the reply. I probably didn't formulated it correctly, sorry. By deploying, I meant to the web servers running the system I am developing, and not to the clients. The solution which I was looking for shouldn't alter .NET installed javascript files; e.g. as WebUIValidation.js.

Yes, I tried the CustumValidator and it seemed to be the right choice for the problem, but since the client-side javascript didn't fire when the text field value changed from some text to no text, it wasn't possible to use that approach without altering the WebUIValidation.js file (the red piece in the original post), which I naturally would like to avoid.

If the client-side javascript was going to be fired every time the value changed (also to the empty string) then the approach would have been perfect... and even good looking!

Thanks, Jacob.
 
Old February 12th, 2006, 03:09 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Right, I see now. Sorry I couldn't help much more...

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 16th, 2006, 07:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

NP, you are always very helpful, thanks.

BR, Jacob.





Similar Threads
Thread Thread Starter Forum Replies Last Post
An interesting question iPagan BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 February 13th, 2007 05:31 PM
Interesting Problem prasanta2expert ASP.NET 1.0 and 1.1 Basics 1 October 16th, 2006 08:14 PM
Interesting Error kanchan_b XSLT 1 February 1st, 2006 03:10 AM
Interesting Data Type Problem dlandolin SQL Server 2000 3 February 23rd, 2005 06:23 PM
interesting problem - re: database & rollovers highbeef Classic ASP Databases 4 March 2nd, 2004 09:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.