Solved

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

Posted on 2006-06-23
5
379 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

734 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