Results 1 to 8 of 8

Thread: Validation

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Validation

    Hey,

    Ok I'm trying to use the .Net validator controls to do some validation I have a form set up with labels. Now Ideally if the user enters incorrect data I want the label to turn red. Without writing custom javascript how can I achieve this using the validator controls?

    Assume this is the form,

    Code:
    <label> * Membership Number</label>
                <asp:TextBox ID="txtMembershipNumber" runat="server" CssClass="txtInput"></asp:TextBox>
                
                <label>* Company name: </label>
                <asp:TextBox ID="txtPassword" runat="server" CssClass="txtInput"></asp:TextBox>
    I can do all the code for the validator including the reg expresions etc. I can get the validator working below the label but I need it to repalce the label on invalid entry.

    Any help?

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Validation

    Hey,

    Are you talking about making this change on the client, or after the page is submitted?

    All the .Net Validator controls do validation on both the client and the server. This gives the user a slightly better user experience (assuming they have javascript enabled) in that they don't have to wait for a postback in order to see that they haven't filled in a textbox.

    If you want the colour change to happen on the client then it is going to have to be a custom javascript function. Assuming you are using a CustomValidator, you should be able to easily add this functionality into the function.

    Is you are wanting to do this server side, then again this should be reasonably simple. Have a look at the Page.IsValid Property. Based on the state of that property, you could change the colours of the label.

    As another option, have you thought about using the ValidationSummary Class.

    Hope that helps (or at least gives you some ideas )

    Gary

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Validation

    I'm fairly familiar with most of this, however I rarley dive into the front end stuff. Which is why I'm checking what I am doing is latest and greatest.

    So theres no way to give the validator control a default piece of tetx to output?

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Validation

    Oh no, there certainly is, you can set the ErrorMessage, which would be automatically displayed in the ValidationSummary, or you can set the Text Property.

    The way I normally do it is to set the Text Property to "*" then position it beside the control it is validating. Then, when validation fails, the "*" shows beside the control that is wrong, and then in the ValidationSummary you give more information about why it is wrong.

    Gary

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Validation

    Ok I'm either overcomplicating this or missing somthing infront of me.

    If I have the following when the form is shown

    Code:
    * Membership Number:   [ Field Here ]
    and after I want it to look like

    Code:
    * Membership Number:   [ Field Here ]
    (On invalid data) - Can this be done, without me writing jquery?

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Validation

    You can have a look at the validation controls here

    http://www.peterblum.com/DES/Home.aspx

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Validation

    Oh, lots going on while I dallied. Yeah, you'll need a javascript solution if not that. Use a custom validator, use your own javascript to validate and set the label to red.

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Validation

    Quote Originally Posted by Pino
    Ok I'm either overcomplicating this or missing somthing infront of me.

    If I have the following when the form is shown

    Code:
    * Membership Number:   [ Field Here ]
    and after I want it to look like

    Code:
    * Membership Number:   [ Field Here ]
    (On invalid data) - Can this be done, without me writing jquery?
    Yeah, I agree with Mendhak. What I would normally do in your situation is set the Text of the validation class to "*" and position it to the left of the label which says "Membership Number:", that way, the asterik will only appear (and you can set it to be red in colour) when something is missing from that control.

    To get the whole label to turn red, then you would need do implement the custom javascript that mend mentioned.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width