0
我有正則表達式這對我的需求工作正常。現在我想限制這個正則表達式,以便它只允許0到15個字符。所以我把它改成$scope.pa = /^([\[email protected]#\$]+){0,15}$/i;
但是0到15個字符的限制沒有解決。Angularjs正則表達式限制字符數問題
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="app">
<div ng-controller="controllerName">
<ng-form name="mailForm">
Pname: <input type="text" ng-model="pname" name="pname" ng-pattern="pa" /><br />
<span ng-show="mailForm.pname.$error.pattern" style="color:red">Please enter pname</span>
</ng-form>
</div>
<script>
var app = angular.module("app", []);
app.controller('controllerName', ['$scope', function ($scope) {
$scope.pname = "abcd267g";
$scope.pa = /^([\[email protected]#\$]+)$/i;
}]);
</script>
</body>
</html>
'/^[!\ W \ - 〜!@#\ $] {0,15} $/I;' – Tushar
Tushar,工作正常並解決了問題 –