Allow only numbers on keypress in KendoGrid TextBox using AngularJS and JavaScript in ASP.Net MVC

mahesh213
 
on Aug 27, 2021 05:53 AM
Sample_127875.zip
794 Views

Hi,

I have one field Age. on keypress of Age it should only allow numbers

can you please help me

@{
    Layout = null;
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.1.114/styles/kendo.default-v2.min.css" />
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="https://kendo.cdn.telerik.com/2020.1.114/js/angular.min.js"></script>
    <script type="text/javascript" src="https://kendo.cdn.telerik.com/2020.1.114/js/kendo.all.min.js"></script>
    <script type="text/javascript">
        var app = angular.module("MyApp", ["kendo.directives"]);
        app.controller("MyController", function ($scope, $http, $window, $interval) {
            ApplyKendoGrid();
            function ApplyKendoGrid() {
                $scope.mainGridOptions = {
                    dataSource: {
                        transport: {
                            read: { url: "/home/GetCustomers/", cache: false }
                        },
                        pageSize: 10,
                        serverPaging: false,
                        serverSorting: false
                    },

                    columns: [{ field: "Id", title: "Id", width: '60px' },

                    {
                        field: "Age",
                        template: '<input style="width:120px" type="text" id="Age"  />',
                        width: 150
                    },
                    ],
                    dataBound: function (e) {
                        var grid = e.sender;
                        var items = e.sender.items();
                        items.each(function (e) {
                            var dataItem = grid.dataItem(this);
                            var txtAge = $(this).find('#Age');
                            $(txtAge).val(dataItem.Age);
                        });
                    }
                };
            }
        })
    </script>
</head>
<body ng-app="MyApp" ng-controller="MyController">    
    <kendo-grid k-options="mainGridOptions" id="tblCustomers"></kendo-grid><br />
</body>
</html>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 27, 2021 06:50 AM

Hi mahesh213,

Check this example. Now please take its reference and correct your code.

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }

    public JsonResult GetCustomers()
    {
        List<Customer> customers = new List<Customer>();
        customers.Add(new Customer { Id = 1, Name = "John Hammond", Age = 60 });
        customers.Add(new Customer { Id = 2, Name = "Mudassar Khan", Age = 36 });
        customers.Add(new Customer { Id = 3, Name = "Suzanne Mathews", Age = 65 });
        customers.Add(new Customer { Id = 4, Name = "Robert Schidner", Age = 70 });
        return Json(customers, JsonRequestBehavior.AllowGet);
    }

    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

View

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.1.114/styles/kendo.default-v2.min.css" />
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="https://kendo.cdn.telerik.com/2020.1.114/js/angular.min.js"></script>
    <script type="text/javascript" src="https://kendo.cdn.telerik.com/2020.1.114/js/kendo.all.min.js"></script>
    <script type="text/javascript">
        var app = angular.module("MyApp", ["kendo.directives"]);
        app.controller("MyController", function ($scope, $http, $window, $interval) {
            ApplyKendoGrid();
            function ApplyKendoGrid() {
                $scope.mainGridOptions = {
                    dataSource: {
                        transport: {
                            read: { url: "/Home/GetCustomers", cache: false }
                        },
                        pageSize: 10,
                        serverPaging: false,
                        serverSorting: false
                    },
                    columns: [
                        { field: "Id", title: "Id", width: '60px' },
                        {
                            field: "Age",
                            template: '<input style="width:120px" type="text" id="Age" class="age" onkeypress="return IsNumeric(event);" />',
                            width: 150
                        }
                    ],
                    dataBound: function (e) {
                        var grid = e.sender;
                        var items = e.sender.items();
                        items.each(function (e) {
                            var dataItem = grid.dataItem(this);
                            var txtAge = $(this).find('.age');
                            $(txtAge).val(dataItem.Age);
                        });
                    }
                };
            }
        })

        function IsNumeric(e) {
            var specialKeys = new Array();
            specialKeys.push(8);
            var keyCode = e.which ? e.which : e.keyCode
            var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
            return ret;
        }
    </script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
    <kendo-grid k-options="mainGridOptions" id="tblCustomers"></kendo-grid>
</body>
</html>