Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I am validating a textbox as accepting only numbers and one point..


for that i am doing like this

XML
<asp:TextBox ID="txtBandwidth" runat="server"></asp:TextBox>
                          <asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" TargetControlID="txtBandwidth"
                              FilterType="Numbers,Custom" ValidChars="." Enabled="True">
                          </asp:FilteredTextBoxExtender>




But it is allowing multiple points (eg: 12....3444...) like this

I want to restrict like this 12.3444

Please help me..
Thanks in advance
Posted

Hai
Try ur code for restrict character,and add javascript for allow only one dot like this

ASP.NET
<asp:textbox id="txtBandwidth" onkeypress="return isNumberKey(event)" runat="server" xmlns:asp="#unknown"></asp:textbox>

                         <asp:filteredtextboxextender id="FilteredTextBoxExtender1" runat="server" targetcontrolid="txtBandwidth" xmlns:asp="#unknown">
                             FilterType="Numbers,Custom" ValidChars="." Enabled="True">
                         </asp:filteredtextboxextender>


in above code i add onkeypress event for handle allow only one dot in textbox and following is javascript,add javascript code in between header tag like

HTML
<script type ="text/javascript" lang ="javascript" >

      function isNumberKey(event) {
          document.getElementById('txtBandwidth').onkeypress = function (e) {
                 if (e.keyCode === 46 && this.value.split('.').length === 2) {
                  return false;
              }
          }
      }

  </script>



Best Regards
AravindB
 
Share this answer
 
v2
 
Share this answer
 
Try this Jquery

http://way2designing.github.io/iframe/w2d-iFrame.html?iframe=http://plentz.github.com/jquery-maskmoney/[^]

Instead of input replace the following
XML
<asp:textbox id="txtDecimal" runat="server"  />
<script type="text/javascript">
    $(function(){
        $("#txtDecimal").maskMoney();
    })
    </script>
 
Share this answer
 
v2
Hi,

If you want to allow only numeric values along with single dot(.) in textbox, than kindly do have the following link which provides a REGEX based solution.

You can use these regex inside the aspRegularExpressionValidator to get the results as you want.


Regex allow digits and a single dot

Regular expression to allow a number with one dot or comma in it

Hope it will help you... :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900