1

I have regular expression for each input field and I want to have all of them at one place - may be in one controller. I have tried it like this and I am not sure this is the right way of doing it

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="${pageContext.request.contextPath}/js/Registratonvalidation.js"></script>
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        <script>
            angular.module('myApp', []).controller("numOnlyRegex", function ($scope)
            {
                $scope.numOnlyRegex = /^\d{1,6}$/;
                $scope.firstNameAndLastName = /^[a-zA-Z]{3,20}$/;
                $scope.password= /^[a-zA-Z]{3,5}$/;
                $scope.mobile= /^[7-9][0-9]{1,9}$/;
                $scope.email= /\S+@\S+\.\S+/;
            });
        </script>
        <!--<script src="${pageContext.request.contextPath}/numOnlyRegex.js"></script>-->
        <title>Registration Form</title>
    </head>
    <body>
        <div class="container">
            <div class="col-md-6 col-md-offset-3">
                <div class="panel panel-login">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-12">
                                <h2 class="text-muted">Registration form</h2>
                                <!--onSubmit="return validate()"-->
                                <div ng-app="myApp" ng-controller="numOnlyRegex">
                                    <form name="myForm" action="RegistrationServlet.do" method="POST" novalidate >
                                        First name:<input type="text" class="form-control input-sm" name="uname" ng-pattern="/^[a-zA-Z]{3,20}/" ng-model="uname | lowercase" placeholder="First Name" required/>
                                        <span style="color:red" ng-show="myForm.uname.$error.pattern">First name cannot be less than 3 letters with no digits</span><br/>
                                        Last name:<input type="text" class="form-control input-sm" name="lname" ng-model="lname" name="lname | lowercase" ng-pattern="/^[a-zA-Z]{3,20}/" required placeholder="Last Name"/>
                                        <span style="color:red" ng-show="myForm.lname.$error.pattern">First name cannot be less than 3 letters with no digits</span><br/>
                                        <p>Password:
                                            <input type="password" class="form-control input-sm glyphicon glyphicon-ok" name="pwd" ng-minlength="3" ng-model="pwd" required placeholder="Password"/>
                                            <span style="color:red" ng-show="myForm.pwd.$error.minlength">password cannot be less than 3 letters</span><br/>
                                            Confirm Password:<input type="password" class="form-control input-sm glyphicon glyphicon-ok" name="pwd2" ng-minlength="3" ng-model="pwd2" required placeholder="Confirm Password"/><br/>
                                            <span style="color:red" ng-show="myForm.pwd2.$error.minlength">password cannot be less than 3 letters</span><br/>
                                            Gender: <input type="radio" name="female" />Female <input type="radio" name="male" />Male <br/><br/>
                                            Mobile:<input type="text" class="form-control input-sm" name="mobile" ng-pattern="/^[7-9][0-9]{1,9}$/" ng-model="mobile" required placeholder="Mobile"/>
                                            <span style="color:red" ng-show="myForm.mobile.$error.pattern">Please enter a valid mobile number</span><br/>
                                            Email:<input type="email" class="form-control input-sm" name="email" ng-pattern="/\S+@\S+\.\S+/" ng-model="email" required placeholder="Email"/>
                                            <span style="color:red" ng-show="myForm.email.$error.pattern">Invalid email address</span><br/>
                                            Address:<textarea class="form-control input-sm" name="address" ng-model="address" required placeholder="Address"></textarea>
                                            <span style="color:red" ng-show="myForm.address.$error.require==true">Address cannot be empty</span><br/>
                                            Street:<input type="text" class="form-control input-sm" name="street" ng-model="street" required placeholder="Street"/>
                                            Area:<input type="text" class="form-control input-sm" name="area" ng-model="area" required placeholder="Area"/>

                                            City:   <select name="city" class="form-control" ng-model="city" required>
                                                <option value="hyderabad">Hyderabad</option>
                                                <option value="secunderabad">Secunderabad</option>
                                                <option value="delhi">Delhi</option>
                                                <option value="mumbai">Mumbai</option>
                                            </select><br/>
                                            State: <input type="text" class="form-control input-sm" name="state" ng-model="state" required placeholder="State"/>
                                            Country: <input type="text" class="form-control input-sm" name="country" ng-model="country" required placeholder="Country"/>
                                            Pin:<input type="text" class="form-control input-sm" ng-pattern="numOnlyRegex" name="pin" ng-model="pin" required placeholder="Pin"/>
                                            <span style="color:red" ng-show="myForm.pin.$error.pattern">Only six-digit number is allowed</span>
                                            <input type="Submit" class="form-control btn btn-success" ng-disabled="myForm.$invalid" value="Submit" />
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>            
    </body>
</html>

1 Answer 1

2

You can define constants

    angular.module('test').constant('regEx', {
       numOnlyRegex: '/^\d{1,6}$/',
       firstNameAndLastName: '/^[a-zA-Z]{3,20}$/',
       password: '/^[a-zA-Z]{3,5}$/',
       mobile: '/^[7-9][0-9]{1,9}$/',
       email: '/\S+@\S+\.\S+/'
    })

This constants can be injected into controllers/services/directives

.controller('TestCtrl', ['$scope','regEx', function ($scope,regEx) {
    $scope.numOnlyRegex = regEx.numOnlyRegex;
}])

EDIT

With constants you can create static variables that can be used in other controllers/services/directives.

In your example you can put all of your regular Expression in one constant object and use this in your whole application.

And then you can use it like this

ng-pattern expects a regex expression.

From Angular's documentation about ng-pattern:

Sets pattern validation error key if the value does not match the RegExp pattern expression. Expected value is /regexp/ for inline patterns or regexp for patterns defined as scope expressions.

In other words, you could create a RegExp in the controller:

$scope.numOnly= new RegExp(regEx.numOnlyRegex);

and use it:

<input ng-model="foo" ng-pattern="numOnly">
Sign up to request clarification or add additional context in comments.

4 Comments

woah I don't understand a bit. Sorry I am new to angular
I updated it a bit. Will try to explain it further, when i am at home later :)
sounds good. The $scope.variableName will be different for each regex right? Okay take your time. Please do update. Thanks
yh for every regex you wanna use in your controller for example you need to bind it to a $scope variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.