Restricting a text box to enter only numeric values and not alphabetical & alphanumeric values

I have 2 text boxes viz. txt_Long and txt_Lat. I want that only numeric values be able to be inserted into those text boxes and not numeric or alphanumeric values. If tried to insert any other values except numeric value, a validation violation message should be displayed in red color just under the respective text boxes.Can any one please help ?

Answers


Use compare validator to check the value entered is number or not .

<asp:TextBox ID="TextBox" runat="server"></asp:TextBox>
<asp:CompareValidator ID="validator" runat="server" ControlToValidate="TextBox" 
     Operator="DataTypeCheck" Type="Double" ErrorMessage="Value must be a number">   
 </asp:CompareValidator>

If you use a normal html5 input you can solve your problem very quickly:

<input type="number" id="only-numbers" min="0">

It will also show an error automatically if a wrong value is inserted.


You can use Regular Expression Validator like below.

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Please Enter Only Numbers" ValidationExpression="^\d+$"></asp:RegularExpressionValidator>

Need Your Help

How do you show the Finder context menu from a Cocoa application?

cocoa contextmenu finder

I have a file listing in my application and I would like to allow people to right-click on an item and show the same contextual menu as Finder does. Is this possible?

Cmake cannot generate shared library, but static library is generated

c++ cmake shared-libraries

I want to generate a shared library with CMake, and even though I do not get any errors, the library (.so file) is not generated.