简体   繁体   中英

Javascript for a textbox to accept 4 numbers and two decimals

I have a textbox in my aspx page which should accept only values like 9999.00 where two decimal positions are mandatory. need a javascript for the above functionality please help

 function isNumber(evt) {
        try {
            evt = (evt) ? evt : window.event;
            var charCode = (evt.which) ? evt.which : evt.keyCode;

            if (charCode > 47 && charCode < 58) {
                return true;
            }
            else if (charCode == 46) {
                return true;
            }
            else {
                alert('please enter a number');
                return false;
            }
        }
        catch (err) {
            alert(err);
        }
    }

This is the code that i have written to validate only numbers are enterred now in the same way only 4 numbers and after that two decimals should be enterred

You have to use Regular expression validator

the pattern should be

[0-9]{4}[.][0-9]{2}
function checkStringInput(val){
    var regex= /^\d+\.\d{2}$/;
    if(val.match(regex)===null)
        return false;
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM