Solved

how to get a control's value in client-side code

Posted on 2006-06-23
5
376 Views
Last Modified: 2008-02-01
Is it possible to get a control's value in client-side script. I have a textbox and one of those anti-automation pictures where you have to type the text in the picture. What I want to do is use client-side scripting to validate what they type in, but i'm not sure how to get the value of that control. In the server-side validation, I access it like:

protected void formShieldDontMatchVd_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            args.IsValid = (formShieldTx.Text.CompareTo(
                this.formShield.Text) == 0);
        }
        catch (Exception)
        {
            args.IsValid = false;
        }
    }

Here's the other code:

<asp:TextBox ID="formShieldTx" runat="server" CausesValidation="True" MaxLength="5"
        Width="96px"></asp:TextBox>
    <asp:RequiredFieldValidator ID="formShieldRequiredVd" runat="server" ControlToValidate="formShieldTx"
        ErrorMessage="Anti-Hacker Text Required" ForeColor="InactiveCaption">*</asp:RequiredFieldValidator><br />
    This is used to help prevent automated registrations.<br />
    <br />
    <dnf:formshield id="formShield" runat="server" alternatetext="" backfillstyle="Random"
        backgradientstyle="Random" backhatchstyle="Random" forefillstyle="Random" foregradientstyle="Random"
        forehatchstyle="Random" height="40px" imagealign="Left" noisecolor="InactiveCaption"
        noisefillstyle="Random" noisegradientstyle="Random" noisehatchstyle="Random" textlength="5" textstyle="Alpha" FontStyle="Bold" Width="192px"></dnf:formshield>
    &nbsp;<asp:CustomValidator
            ID="formShieldDontMatchVd" runat="server"
            ControlToValidate="formShieldTx" ErrorMessage="Anti-Hacker text does not match"
            ForeColor="InactiveCaption" OnServerValidate="formShieldDontMatchVd_ServerValidate" Enabled="true">*</asp:CustomValidator>

On a side question, is client-side validation the best way to do form validation, because don't some browsers not allow it. What are the other options.. can you detect if a browser allows it?
0
Comment
Question by:mbosico
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:gregg1ep00
ID: 16969580
You *could* use a CompareValidator control on your textbox, and set the "ValueToCompare" property to the actual "text value" of the formshield image.

The bad thing about using client script in this scenario is that somewhere on your page (in script), the value of that formshield image will appear in plain text in your page source.

Generally speaking, client-side validation will definitely provide the user with a faster browsing experience.  However, in this case, I'd suggest doing your comparison in server script where the "text value" of the formshield image is not visible on the client.
0
 

Author Comment

by:mbosico
ID: 16970789
I'm just starting with asp, so excuse me if these questions are dumb. Why would the value of the formshield appear in plain text? Ok, so if I continue to keep this validation as server-side, I run into the problem that I couldn't use PostBackUrl because it would just goto that Url regardless if the server-side validation failed right? What would I do in that situation? I could check on the next page if the previouspage.isvalid, and return to it, but I would not have any of the information that was typed in. Am I going about this wrong, is there a better technique.. its basically a register page with client-side and server-side validation. Thanks again for the help so far.
0
 
LVL 4

Accepted Solution

by:
gregg1ep00 earned 125 total points
ID: 16971086
Ok.  I think your best bet is to post back to the same page, do server-side validation, and then redirect if the validation succeeds.  You can check to make sure validation succeeded like this:

if ( Page.IsValid )
   Response.Redirect( "your new url.aspx" );

I think that's going to be the easiest route.  If you post back to a different url, you're not going to be able to save the previous form's values.

The value of the formshield would appear in plain text if you want to do client-side validation.  What you would do is this:
1.  Change your CustomValidator to a CompareValidator
2.  In your code-behind, you would set the CompareValidator's "ValueToCompare" property to the formshield's "Text" property.
3.  View your page in a browser, then view source.
4.  Somewhere in the page source, you will see the javascript compare routine check the textbox's value against a string, which is the formshield's text.  The formshield's text would be there, plain as day!  That's just the way client-side validation works.

I hope that helps.  Let me know if it's unclear and I'll try to explain it better!  :)
0
 

Author Comment

by:mbosico
ID: 16971802
Thanks, I tried that and it worked nicely. I didn't fully understand what the PostBack meant, but makes sense now. Little side question if you don't mind.. so when I redirect to another page after seeing that the page IsValid, how can I check on that page to see that it had really come from the previous page. Like I dont want a user to type in page 2 somehow and it load, it should have to come from page 1. In that case if they went to page 2 w/o going to page 1 first it would redirect to page1. If you want me to post this as another question, aiight. Thanks again. You really know ur stuff.
0
 
LVL 4

Expert Comment

by:gregg1ep00
ID: 16971878
You can try to use the Request.UrlReferrer property to make sure it came from the previous page.

Glad you got it to work!  Good luck!
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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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

24 Experts available now in Live!

Get 1:1 Help Now