Replace comma and allow only one dot (period) in TextBox using JavaScript

Last Reply 2 months ago By dharmendr

Posted 2 months ago

Hello!

Here is two (2) BUG.

1) Allow only one dot (.) but if I typing -9,9,9 then results is 9.9.9

2) If I hold down the comma button on the keyboard then results is -9.,,,,

<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        var specialKeys = new Array();
        specialKeys.push(8);
        specialKeys.push(9);
        specialKeys.push(46);
        specialKeys.push(36);
        specialKeys.push(35);
        specialKeys.push(37);
        specialKeys.push(39);
 
        function IsNumeric(e) {
            if (e.keyCode === 46 && e.target.value.split('.').length === 2) {
                return false;
            }
            var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
            return ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90)
                || keyCode == 32 || keyCode == 44 || keyCode == 46
                || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
        }
 
        function ReplaceComma(ele) {
            if (ele.value.indexOf('.') == 0) {
                ele.value = ele.value.replace('.', '');
            }
            if (ele.value.indexOf(',') == 0) {
                ele.value = ele.value.replace(',', '');
            }
            ele.value = ele.value.replace(',', '.').replace('..', '.');
        }
 
        function doCheck(id, evt) {
            var temp = parseInt($(id).val());
            if (isNaN(temp)) {
                $(id).val('-');
            }
 
            $(id).val($(id).val());
        }
    </script>
</head>
<body>
    <input type="text" value="-" maxlength=7 id="fmargin" onkeypress="return IsNumeric(event);"
        onkeyup="ReplaceComma(this);doCheck('#fmargin', event);">
</body>
</html>

 

You are viewing reply posted by: dharmendr 2 months ago.
Posted 2 months ago Modified on 2 months ago

Hi letstry,

Refer below code.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        var specialKeys = new Array();
        specialKeys.push(8);
        specialKeys.push(9);
        specialKeys.push(46);
        specialKeys.push(36);
        specialKeys.push(35);
        specialKeys.push(37);
        specialKeys.push(39);

        function IsNumeric(e) {
            if (e.keyCode === 46 &&
                e.target.value.split(/\.|,/).length >= 2) {
                return false;
            }
            var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
            return ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90)
                || keyCode == 32 || keyCode == 44 || keyCode == 46
                || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
        }

        function ReplaceComma(ele, evt) {
            ele.value = ele.value.replace(',', '.').replace('.,', '.').replace(',.', '.');
            if (ele.value.split('.').length > 2) {
                ele.value = ele.value.split('.').slice(0, 2).join('.')
            }
        }

        function doCheck(id, evt) {
            var temp = parseInt($(id).val());
            if (isNaN(temp)) {
                $(id).val('-');
            }

            $(id).val($(id).val());
        }
    </script>
    <input type="text" value="-" maxlength="70" id="fmargin" onkeypress="return IsNumeric(event);"
        onkeyup="ReplaceComma(this,event);doCheck('#fmargin', event);" />
</body>
</html>

Demo