1

I have a text box like below :

<asp:TextBox ID="headertxtUserName" runat="server" Text="User Name" onfocus="onfocusOfUserName(this);" 
    onblur="onblurOfUserName(this);" CssClass="header-login-input" ValidationGroup="A" Font-Names="Tahoma">
</asp:TextBox>

as you see there is a default text = "User Name" in that text box.
which validator should i use for this text box for purpose below :

when Text="User Name" or Text="" returns false.
when Text!="User Name" or Text!="" returns true.

I know we can use CustomValidators!
but i don't want any postback, so what can I do?

3
  • 1
    You do understand that validators can be set to use client-side validation so that there is no postback, right? You would use a CompareValidator, not a CustomValidator... This can also be done with pure javascript if you like, but not as easily. Commented Dec 16, 2011 at 22:30
  • @David Stratton thanks for attention / how can i use CompareValidator for that purpose? (should i use two CompareValidators? if yes, how can i use one CompareValidator?) Commented Dec 16, 2011 at 22:45
  • besides, i know we can use javascript. but i am researching on validator components in VS. Commented Dec 16, 2011 at 22:46

2 Answers 2

1

You can use a RegularExpressionValidator in combination with a RequiredFieldValidator control for this. It will validate the value of the textbox on the client, without a postback.

My syntax below may be slightly off. I'm not near a PC with Visual Studio, so I'm doing this off the top of my head. Please forgive me if I spelled something wrong.

<asp:TextBox id="UserNameTextBox" runat="server" Text="User Name" /> 
<asp:RegularExpressionValidator id="UserNameTextBoxRegexValidator" runat="Server" ControlToValidate="UserNameTextBox" ValidationExpression="^User Name$" ErrorMessage="please enter a user name" />
<asp:RequiredFieldValidator id="UserNameTextBoxRequiredFieldValidator" runat="server" ControlToValidate="UserNameTextBox" ErrorMessage="please enter a user name" />
Sign up to request clarification or add additional context in comments.

Comments

0

Have you considered using the Ajax Control Toolkit's TextBox WaterMark to set your text box's background text to "User Name" You can set it so once the user clicks on the text box the watermark will disappear and the user can proceed to enter their values. If the user leaves the textbox blank the text will be set back to the water mark's value.

You can use this in conjunction with the suggestion above fore client and server validation for a fuller user experience.

Check it out here:TextBox Watermark

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.