1

I have a TextBot control in my page and its TextMode property is set to Number.

Now what I need is to change the eror message when user inputs an invalid value. Which is as default:

"You must enter a valid value."

<asp:TextBox runat="server" ID="tbPrice" TextMode="Number" CssClass="form-control" />

1 Answers1

2

TextMode doesn't have Number option. I know you see the option with intellisense but see this URL:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textmode(v=vs.110).aspx

It can only have MultiLine, SingleLine or Password. To get validation you are expecting you can use a CompareValidator as mentioned below which will show the message if you enter anything other than numbers.

<asp:CompareValidator runat="server" ID="valNumber" ControlToValidate="tbPrice" Type="Integer" Operator="DataTypeCheck" ErrorMessage="You must enter a valid value." />

You can also use AjaxControlToolkit control FilteredTextBox. Which will not allow you to type anything else but number.

http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/FilteredTextBox/FilteredTextBox.aspx

The other option you have is to restrict user to only enter numbers. See below link:

How to allow only numeric (0-9) in HTML inputbox using jQuery?

Community
  • 1
  • 1
Adarsh Shah
  • 6,755
  • 2
  • 25
  • 39
  • Thanks for reply @Adarsh. But for me TextMode=Number works perfectly well. What I needed was to be able to change its warning essage. I can achieve that by using a CompareValidator but I rather learn the other way. :) –  Dec 25 '13 at 15:23
  • So you can't type anything other than number in the textbox or it shows the error msg after submitting? – Adarsh Shah Dec 25 '13 at 17:16
  • I can type non-numeric values but it shows a message like Tooltip when I try to submit and stops postback, just before postback, which means client side. –  Dec 25 '13 at 18:57