Validate consecutive numbers simultaneously using Regular Expression in AngularJS

mahesh213
 
on Dec 23, 2020 11:50 PM
Sample_144994.zip
1340 Views

Hi,

I have one textbox in that I am going to allow only numbers and i don't want to repeat same numbers consecutively and length should be only 4 numbers

ex:

1233 - invalid

1355 - invalid

13579 - invalid

135 - invalid

1357 - valid 

Could you please help me?

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', [])
        app.controller('MyController', function ($scope, $http, $window) {
 
            $scope.Save = function () {

             
            }
        });
    </script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
    <input type="text" ng-model="Number" />
    <input type="button" ng-click="Save()" value="Save" />
</body>
</html>  

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 24, 2020 01:56 AM

Hi mahesh213,

Refer below code.

HTML

<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', [])
        app.controller('MyController', function ($scope, $window) {
            $scope.Save = function () {
                var number = $scope.Number;
                var regex = /^(?!.*(\d)\1).*$/;
                if (number.length == 4) {
                    if (!regex.test(number)) {
                        $window.alert('Invalid');
                    } else {
                        $window.alert('Valid');
                    }
                } else {
                    $window.alert('Numbers should be 4 digit.');
                }
            }
        });
    </script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
    <input type="text" ng-model="Number" />
    <input type="button" ng-click="Save()" value="Save" />
</body>
</html>

Demo