角js中的多个正则表达式

时间:2015-06-12 11:28:54

标签: angularjs

我有每个输入字段的正则表达式,我希望所有这些都在一个地方 - 可能在一个控制器中。我试过这样的,我不确定这是正确的做法

<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 个答案:

答案 0 :(得分:2)

您可以定义常量

    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+/'
    })

这个常量可以注入controller / services / directives

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

修改

使用常量,您可以创建可在其他控制器/服务/指令中使用的静态变量。

在您的示例中,您可以将所有正则表达式放在一个常量对象中,并在整个应用程序中使用它。

然后你可以像这样使用它

ng-pattern期望正则表达式。

来自Angular的documentation关于ng-pattern

  

如果值与值不匹配,则设置模式验证错误键   RegExp模式表达式。预期值为/ regexp / for inline   定义为范围表达式的模式的模式或正则表达式。

换句话说,您可以在控制器中创建一个RegExp:

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

并使用它:

<input ng-model="foo" ng-pattern="numOnly">