Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Dynamic textbox like
HTML
<input type='text' Class='TxtBox' id='txtPhNo' placeholder='Enter Phone No' />


How to allow only integers in this textbox?
Posted

Refer this code : Allow only integers jquery
 
Share this answer
 
Using Jquery you can do this easily

HTML
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://digitalbush.com/wp-content/uploads/2013/01/jquery.maskedinput-1.3.1.min_.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
  <script type='text/javascript'>
    $(function(){
        //Define your mask
        $('#Textbox1').mask('99999.9999');
    });
  </script>
</head>
<body>
  <!-- Example of your TextBox -->
  <input id='Textbox1' />
</body>
</html>


You can also use ajax control toolkit control check this links

http://aspsnippets.com/Articles/ASPNet-AJAX-FilteredTextBoxExtender-Control-Example.aspx

https://ajaxcontroltoolkit.codeplex.com/wikipage?title=FilteredTextBox%20Control&referringTitle=Tutorials[^]
 
Share this answer
 
Hi,

If you are using HTML 5 you can do it as :
HTML
<input type="text" class="TxtBox" id="txtPhNo" placeholder="Enter Phone No" pattern="[0-9]" /> 


Refer to this : http://www.pageresource.com/html5/input-validation-tutorial/[^]

If not HTML5, then you can do it using JavaScript or Jquery both.

1) JavaScript :
JavaScript
<input type="text" class="TxtBox" id="txtPhNo" placeholder="Enter Phone No"  önkeypress="return event.charCode >= 48 && event.charCode <= 57"></input></input>


2) JQuery :
JavaScript
$(document).ready(function() {
    $("#txtboxID").keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
             // Allow: Ctrl+A
            (e.keyCode == 65 && e.ctrlKey === true) || 
             // Allow: home, end, left, right
            (e.keyCode >= 35 && e.keyCode <= 39)) {
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });
});


Hope this helps !!

Regards,
Praneet
 
Share this answer
 
v2

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