Validate text field with regex

http://stackoverflow.com/questions/21017867/how-to-validate-for-money-in-jquery/21018113

by hibbard_eu

HTML

<input id="amount" maxlength="7" type="text" />

JavaScript

$("#amount").on("keyup", function(){
    var valid = /^\d{0,4}(\.\d{0,2})?$/.test(this.value),
        val = this.value;
    
    if(!valid){
        console.log("Invalid input!");
        this.value = val.substring(0, val.length - 1);
    }
});