You must Sign In to post a response.
  • Category: JavaScript

    Textbox allow only a decimal or numeric

    i want to create a validation using javascript which verify the value entered in a textbox. value must be a decimal or numeric.
  • #747553
    U can use this validation
    function isNumberKey(evt)
    {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;
    return true;
    }



    <asp:TextBox ID="txtra" runat="server" CssClass="ddddd" onkeypress="return isNumberKey(event)">

  • #747554
    Hi

    Use keyup or keypress event



    $("#txtQty").keyup(function() {
    var $this = $(this);
    $this.val($this.val().replace(/[^\d.]/g, ''));
    });

    or

    <html>
    <head>
    <script language="Javascript">
    <!--
    function isNumberKey(evt)
    {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode != 46 && charCode > 31
    && (charCode < 48 || charCode > 57))
    return false;

    return true;
    }
    //-->
    </script>
    </head>
    <body>
    <input id="txtChar" onkeypress="return isNumberKey(event)">
    type="text" name="txtChar">
    </input></body>
    </html>



    Regards
    Sekhar Babu,
    www.aspdotnet-sekhar.blogspot.in

  • #747555
    both the codes are not working for me. actually i am using devexpress controls, is this matter?

    i am using below code.


    function fn_allowDecimal(s, e) {
    var searchSpecial = '$Backspace$Del$Home$Tab$Left$Right$Up$Down$End$';
    if (searchSpecial.indexOf('$' + e.htmlEvent.key + '$') < 0) {
    var theEvent = e.htmlEvent || window.event;
    var key = theEvent.keyCode || theEvent.which;
    key = String.fromCharCode(key);
    var regex = /[0-9 .]/;

    if (!regex.test(key)) {
    theEvent.returnValue = false;
    if (theEvent.preventDefault)
    theEvent.preventDefault();
    }
    }
    }


    Thats code is working for me in some manner but user can enter decimal point "." multiple times.
    how can i avoid that

    Aman Gandhi

  • #747566
    Hi,

    Apart from the above two, you can do it using AJAX by using AJAX Filtered TextBox control.

    <tr>
    <td width="50%" align="right">Enter Digits only:</td>
    <td width="50%" align="left">
    <asp:TextBox ID="txt1" runat="server" ></asp:TextBox>
    </td>
    </tr>


    <ajaxToolkit:FilteredTextBoxExtender ID="fte1" runat="server" TargetControlID="txt1" FilterType="Numbers" ></ajaxToolkit:FilteredTextBoxExtender>



    Try this one, it is simple and easy to handle..

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #747581
    Hi,

    you can do this like this-
    <html>
    <head>
    <script language="Javascript">
    <!--
    function isNumberKey(evt)
    {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode != 46 && charCode > 31
    && (charCode < 48 || charCode > 57))
    return false;

    return true;
    }
    //-->
    </script>
    </head>
    <body>
    <input id="txtChar" onkeypress="return isNumberKey(event)">
    type="text" name="txtChar">
    </input></body>
    </html>

    Thanks,
    Ashutosh Jha
    http://tricksroad.com

  • #747697
    Hi,

    <script type="text/javascript">
    //Function to allow only numbers to textbox
    function validate(key)
    {
    //getting key code of pressed key
    var keycode = (key.which) ? key.which : key.keyCode;
    var phn = document.getElementById('txtPhn');
    //comparing pressed keycodes
    if (!(keycode==8 || keycode==46)&&(keycode < 48 || keycode > 57))
    {
    return false;
    }

    }
    </script>


  • Sign In to post your comments