To allow integer only in text field we can use the following jquery function .
We can write below code inside a document.ready function and give the class “integer-allowed” for the text box we needed.
$('.integer-allowed').keyup(function () { this.value = this.value.replace(/[^0-9\.]/g, ''); });
Working demo
Enter value (Numbers only allowed)
Working code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('.integer-allowed').keyup(function () { this.value = this.value.replace(/[^0-9\.]/g, ''); }); }); </script> <strong>Enter value (Numbers only allowed)</strong><br /> <input type="text" class="integer-allowed" name="demoname" /><br /> <input type="submit" class="" name="demosubmit" />
3,093 total views, 1 views today
Leave a Reply