Translate

Showing posts with label numeric validation in jquery in textbox. Show all posts
Showing posts with label numeric validation in jquery in textbox. Show all posts

Tuesday, 14 April 2015

numeric validation in jquery textbox

Only number or validate text box which receive only number in javascript function


 <script type="text/javascript">
function isNumber(evt) {
            evt = (evt) ? evt : window.event;
            var charCode = (evt.which) ? evt.which : evt.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                return false;
            }
            return true;
        }
    </script>



For html input Control
-------------------------
<input type="text" id="txtEidNo" onkeypress="isNumber(event)" />



For ASPX TextBox Control
-------------------------
<asp:TextBox ID="txtEidNo" runat="server" onkeypress="return isNumber(event)"></asp:TextBox> 




In jquery 
----------------
For html input Control
------------------------------
<input type="text" id="txtEidNo" />

<script type="text/javascript">
        $(document).ready(function () {
            $("#txtEidNo").keydown(function (e) {
                if (e.shiftKey)
                    e.preventDefault();
                else {
                    var nKeyCode = e.keyCode;
                    //Ignore Backspace and Tab keys    
                    if (nKeyCode == 8 || nKeyCode == 9)
                        return;
                    if (nKeyCode < 95) {
                        if (nKeyCode < 48 || nKeyCode > 57)
                            e.preventDefault();
                    }
                    else {
                        if (nKeyCode < 96 || nKeyCode > 105)
                            e.preventDefault();
                    }
                }
            });
        });
    </script>




For ASPX TextBox Control
-------------------------
<asp:TextBox ID="txtEidNo" runat="server" ></asp:TextBox> 

<script type="text/javascript">
        $(document).ready(function () {
            $("#<%= txtEidNo.ClientID%>").keydown(function (e) {
                if (e.shiftKey)
                    e.preventDefault();
                else {
                    var nKeyCode = e.keyCode;
                    //Ignore Backspace and Tab keys      
                    if (nKeyCode == 8 || nKeyCode == 9)
                        return;
                    if (nKeyCode < 95) {
                        if (nKeyCode < 48 || nKeyCode > 57)
                            e.preventDefault();
                    }
                    else {
                        if (nKeyCode < 96 || nKeyCode > 105)
                            e.preventDefault();
                    }
                }
            });
        });
    </script>