2

How can I accept only one "." in Textbox that is accepting only numeric value? I am using an ajax tool called FilteredTextBoxExtender

this is my sample code:

<asp:TextBox ID="txtMNumberOfHours" runat="server" 
Width="50px" MaxLength="10"></asp:TextBox>

<asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" 
 FilterType="Numbers, Custom" ValidChars="." 
 TargetControlID="txtMNumberOfHours" ></asp:FilteredTextBoxExtender>

in this code, I can enter multiple "." which is not good.

rs.
  • 26,707
  • 12
  • 68
  • 90
portgas d ace
  • 309
  • 3
  • 9
  • 21
  • 2
    http://stackoverflow.com/questions/5761965/how-to-restrict-the-textbox-to-accept-only-one-dot-in-decimal-number-in-keypress – Inisheer Jan 15 '13 at 03:36

5 Answers5

3

How about using regexvalidator or maskededit extender?

With maskededitextender you can do

<asp:MaskedEditExtender ID="MaskedEditExtender1" 
   runat="server" TargetControlID="txtMNumberOfHours"
   Mask="99.99" MaskType="Number" InputDirection="RightToLeft" />

This will allow numbers with total four digits, 2 on left and 2 on right of dot.

rs.
  • 26,707
  • 12
  • 68
  • 90
1

FilteredTextBoxExtender only looks at one character at a time as they're being entered so it's not looking at the whole thing to see how many periods have been entered.

I'd suggest using RegularExpressionValidator.

allen.mn
  • 467
  • 3
  • 11
0

One way to do so is to check the string for any extra .'s using RegEx as I am not a RegEx Expert by no means I'll leave that to another member. But you would do a check in the OnTextChanged Event on the TextBox.

0

The best way to handle this in asp.net is to write some custom javascript validation.

You can use the following reg-ex to validate the value.

^[-]?((\d+)|(\d+\.\d+)|(\.\d+))$

If you use jQuery, you could use the blur event to validate at client side.

Ofcourse, you should always parse it again at the server side and not assume the client side will give you correct data.

nunespascal
  • 17,584
  • 2
  • 43
  • 46
-1

this is the solution

 <ajaxToolkit:FilteredTextBoxExtender ID="txtPrice_Filter" runat="server" BehaviorID="txtPrice_Filter" FilterType="Custom, Numbers" TargetControlID="txtPrice" ValidChars="." />
A.Awada
  • 134
  • 2
  • 8