Wednesday, 4 June 2014

JavaScript Validation for DecimalWithTwoPlaces

function DecimalWithTwoPlaces(control) {
          
            // livingyearsinrajasthan(control);
            var s = document.getElementById(control).value;
            if (s != "") {
                var d = s.split(".");
                var c = d.length;
                if (c > 2) {
                    s = d[0] + "." + d[1];
                }

                if (s.indexOf(".") > -1) {
                    if (d[0] == "") {
                        s = 0 + "." + d[1];
                        document.getElementById(control).value =s;
                    }
                    if (d[1].length == 0) {
                        s = s + "00";
                        document.getElementById(control).value = s;
                    }
                    if (d[1].length == 1) {
                        s = s + "0";
                        document.getElementById(control).value = s;
                    }
                    else if (d[1].length > 2) {

                        d[1] = d[1].substring(0, 2);
                        s = d[0] + "." + d[1];
                        document.getElementById(control).value = s;
                    }
                }
                else {
                    s = s + ".00";
                    document.getElementById(control).value = s;
                }
            }
            document.getElementById(control).value = s;
        }
ex to call:
onkeypress="return DecimalWithTwoPlaces(controlName)"

No comments:

Post a Comment