0

how to restrict user to enter only numbers with 6 digits and two decimal values in textbox using 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 < 48 || keycode > 57)) {
        return false;
    } else {
        if (phn.value.length < 6) {
            return true;
        } else {
            return false;
        }
    }
}

EDIT:

var txtBudget = document.getElementById('MainContent_txtBudget');
    txtBudget.addEventListener('input', function (prev)
     {
        return function (evt) {
            if (!/^\d{0,6}(?:\.\d{0,2})?$/.test(this.value)) {
                this.value = prev;
            }
            else {
                prev = this.value;
            }
        };
    } (txtBudget.value), false);
1
  • Note you want to cover keycodes 96 - 105, which are the numeric keypad as well. You should also cover the backspace, tab and arrow keys too. Commented Mar 28, 2012 at 14:13

2 Answers 2

4

You can try something like this:

var foo = document.getElementById('foo');

foo.addEventListener('input', function (prev) {
    return function (evt) {
        if (!/^\d{0,6}(?:\.\d{0,2})?$/.test(this.value)) {
          this.value = prev;
        }
        else {
          prev = this.value;
        }
    };
}(foo.value), false);​

The code is not cross-browser compliant, but it should give you a hint of how it can be done.

demo: http://jsfiddle.net/v4tGc/


Update: not using the input event.

var foo = document.getElementById('foo');

foo.addEventListener('keypress', function (evt) {
    var value = this.value + String.fromCharCode(evt.which);
    if (!/^\d{0,6}(?:\.\d{0,2})?$/.test(value)) {
      evt.preventDefault();
    }
}, false);​
Sign up to request clarification or add additional context in comments.

4 Comments

how to call this function in onkeypress event of the textbox ??
but how you call this evt.preventDefault();
@kk1076 I don't quite get what you mean?!
its working fine with input as in demo .. but its not working in keypress ?.
0
**When I Googling, found a code. And then i modify to what i need. And I think it will help you**.

    **HTML**

        <html>
            <head>
                <meta charset="utf-8">
                <title>JS Bin</title>
            </head>
            <body>
                <input type="text" id="sal" name="sal" onkeypress="return isDecimalNumber(event,this)">
            </body>
        </html>

    **Javascript**



<script type="text/javascript">

            function isDecimalNumber(evt, c) {
                var charCode = (evt.which) ? evt.which : event.keyCode;
                var dot1 = c.value.indexOf('.');
                var dot2 = c.value.lastIndexOf('.');

                if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
                    return false;
                else if (charCode == 46 && (dot1 == dot2) && dot1 != -1 && dot2 != -1)
                    return false;

                return true;
            }

        </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.