Solved

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

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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