Solved

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

Posted on 2006-06-23
5
375 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

16 Experts available now in Live!

Get 1:1 Help Now