Sunday, 25 February 2018

Numeric with only one dot allowed to textbox in ASP.Net

<script type="text/javascript" language="javascript">
        function validatetxtbx(val) {
            var e = event || evt; // for trans-browser compatibility
            var charCode = e.which || e.keyCode;

            // for only numeric and dot allowed
            if (!(charCode == 46 || (charCode >= 48 && charCode <= 57))) {
                alert("Only Numeric & Decimal values allowed.");
                return false;
            }
            //for press char value
            var curchar = String.fromCharCode(charCode);

            //concate previous value with current press value
            var mainstring = val + curchar;
            if (mainstring.indexOf('.') > -1) {
                if (mainstring.split('.').length > 2) {
                    alert("Only one dot allowed.");
                    return false;
                }
            }
            //alert("hi")
        }
    </script>


and then add below one in code behind at Page_Load()

TextBox1.Attributes.Add("onfocus","validatetxtbx()");

No comments:

Post a Comment