www.webdeveloper.com
Results 1 to 2 of 2

Thread: Custom error UI using requiredfieldvalidator assistance needed

  1. #1
    Join Date
    May 2004
    Posts
    39

    Custom error UI using requiredfieldvalidator assistance needed

    Allow me to thank you in advance.

    I'm presently using a standard asp.net control to validate a (Name) text field. As you can see by the code, I have moved the asterisk to the left of the textbox label (rather than put it next to the text box). What I would like to do it show the asp.net error "*" in place of the "<span class="required-asterisk">*</span>"

    So if the user has no error they will see the <span class="required-asterisk">*</span> which is blue for example. However, if they have an error the blue "*" will be replaced by the asp.net default "*".

    This is my code:

    <asp:requiredfieldvalidator id="Name" runat="server" controltovalidate="Name" enableclientscript="True">*</asp:requiredfieldvalidator>
    <span class="required-asterisk">*</span>Name</span>

    Basically I would like something similar to an esle statement.

    Any help would be greatly appreciated.

    Tim

  2. #2
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    I would use the CustomValitator control. That way you can use an if statement to determine whether valid input was entered. Something like this may suffice.
    Code:
    sub ValidateField(Sender As Object, Args As ServerValidateEventArgs)
       If Args.Value = Nothing Then
          ErrMessage.Style("color") = "Red"
          Args.IsValid = False
       Else
          ErrMessage.Style("color") = "Blue"
          Args.IsValid = True
       End If
    end sub
    
    ...
    
      <asp:CustomValidator runat="server" id="custVal_Name"
            ControlToValidate="Name"
            OnServerValidate="ValidateName" />
      <span id="ErrMessage" style="color:blue;" runat="server">*</span>
    The following article explains how to use the control (if you do not already know how to): http://aspnet.4guysfromrolla.com/articles/073102-1.aspx

    Also, you have posted in the incorrect forum. I will move this to the ASP.NET for you.

    Regards,
    Andrew Buntine.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles