Allow Numbers After Decimal And Before In Jquery

Example: In this function it will takes 10 digits before decimal and after decimal it will takes 8 digits.

Lets see how it is possible in Javascript, if you want to change it in Jquery also

Please follow these steps:

1. Create a textbox with the name of txtDecimal and add a event with the name of onkeypress to that textbox and give maxlength of that textbox. See below.


<asp:TextBox ID="txtDecimal" runat="server" MaxLength="20" onkeypress="return CheckNumber(event,this.value)"></asp:TextBox>


2. Create a Javascript function with the name of CheckNumber in aspx page.

<script type="text/javascript">

        function CheckNumber(key, value) {
            var dot= /[.]/g;
            var whitespace = /^\S+$/;
            var Minus = /[-]/g;
            var keyCode = key.keyCode == 0 ? key.charCode : key.keyCode;
            var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 45) || (keyCode == 46));
            if (ret == true) {
                if (keyCode != 8) {
                    var Strlength = value.length;
                    var StrValue = value;
                    if (keyCode == 46) {
                        if (value.indexOf('.') > -1) {
                            return false;
                        }
                    }
                    else if (keyCode == 45) {
                        if (value.indexOf('-') > -1) {
                            return false;
                        }
                    }
                    else {
            //  Here it will take upto 10 digits before decimal and after decimal it will take 8 digits.

                        var str = [10, 8];

                        if (value.indexOf('.') > -1) {

                            if (str[0] != "") {
                                var length = value.substr(value.indexOf('.') + 1).length;
                                if (length >= str[1]) {
                                    return false;
                                }
                            }
                        }


                        if (!StrValue.match(dot)) {
                            var strHypen = StrValue.replace("-", "");
                            var strHypenLength = strHypen.length;
                            if (str[0] != "") {

        //Here I am taken maxlength of textbox is 20 for my requirement, whatever your requirement you have to change.

                                if (strHypenLength > 20 - str[1] - 3) {
                                    if (Strlength > 20 - str[1] - 3) {
                                        return false;
                                    }
                                }
                            }

                        }
                    }
                }
            }
            else {
                return ret;
            }
        }
     
    </script>

Result: Actually it will accept only numbers and one dot(.) and one (-) symbol.

Here it is output.

 5 digits before decimal and after


 10 digits before decimal and after 8 digits


 8 digits before decimal and after 6 digits with accepting minus (-) symbol.


 8 digits before decimal and after 8 digits with accepting minus (-) symbol.

 10 digits before decimal and after 8 digits with accepting minus (-) symbol.


Hope it will help you guys..!!!!!!!!!!

Comments

Popular posts from this blog

Send API POST Request in MS SQL Server

Delete Empty Rows or Null Rows in Datatable in C#

Difference of High-Level Design (HLD) and Low-Level Design (LLD)

Use immutable data structures to avoid unnecessary change detection In Angular

Create Custom Form Control for ng-select in Angular

Restrict Special Characters in Windows Application using c#

Domain Driven Design (DDD) in .NET Core

Export to Excel in Angular 6 | 7 | 8 | 9 | 10