Solved

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

Posted on 2006-06-23
5
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

636 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