Validate text field with regex

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

HTML

<p> First </p>
<input id="amount" maxlength="7" type="text" />
<p> Second solution </p>
<input id="amount1" maxlength="7" type="text" />

JavaScript

$("#amount").on("keyup", function(){
        var valid = /^\d{0,4}(\.\d{0,3})?$/.test(this.value),
            val = this.value;
        
        if(!valid){
            alert("Please enter price with decimal format, eg x.xx!");
            this.value = val.substring(0, val.length - 1);
        }
    });


$("#amount1").on("change paste keyup", function(){
    var valid = /^\d*(\.\d*)?$/.test(this.value),
        val = this.value;

    if(!valid){
        alert("Please enter price with decimal format, eg x.xx!");
        this.value = val.substring(0, val.length - 7);
    }
});