Form validation in JAvascript with Regexp

Posted by Nikita Barsukov on Stack Overflow See other posts from Stack Overflow or by Nikita Barsukov
Published on 2010-05-05T18:24:58Z Indexed on 2010/06/11 8:12 UTC
Read the original article Hit count: 148

Filed under:
|
|

I have a webpage with an input field where only digits are allowed. The input field has an onkeyup event that starts this validating function:

function validate() {
    var uah_amount = document.getElementById("UAH").value;
    var allowed = /^\d+$/;
    document.getElementById("error").innerHTML = document.getElementById("UAH").value;

    if (!allowed.test(uah_amount)) {
        document.getElementById("error").style.backgroundColor = "red";
    }
}

Everything works as I expect until I hit Backspace button to remove some characters. In this case function always behaves as if I entered letters.

How to correct this?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex