我想验证上面提到的这三个,但我发现使用适当的函数很困难。 ...请帮忙。
3个输入的html代码
<input id="name" ng-model="user.name" ng-blur="checkIfNameIsValid()" placeholder="Name"></input>
<input id="mobile" ng-model="user.phone" ng-blur="checfIfMobileNumIsValid()" placeholder="Mobile Number" maxlength="10"></input>
<input id="Aerror" ng-model="user.aadhar" ng-blur="checkIfAadharIsValid()" placeholder="Aadhar Number" maxlength="12"></input>
.js 代码
$scope.checfIfMobileNumIsValid = function()
{
var mobile=/^\d{10}$/;
if($scope.user.phone.match(mobile) || $scope.user.phone == '' || $scope.user.phone == 'null' || $scope.user.phone == null)
{
$scope.user.mobErrorMsg = 'Mobile number is not valid';
$scope.error = true;
}
else
{
$scope.user.mobErrorMsg = '';
$scope.error = false;
}
}
$scope.checkIfNameIsValid = function()
{
var name='/^[a-zA-Z ]+$/';
if($scope.user.name.match(name)||$scope.user.name == ''||$scope.user.name=='null'||$scope.user.name==null)
{
$scope.user.NameErrorMsg = 'Name is not valid';
$scope.error = true;
}
else
{
$scope.user.NameErrorMsg = '';
$scope.error = false;
}
}
$scope.checkIfAadharIsValid = function()
{
var aadhar='/^\d{12}$/';
if($scope.user.aadhar.match(aadhar) || $scope.user.aadhar == '' || $scope.user.aadhar == 'null' || $scope.user.aadhar == null)
{
$scope.user.aadharErrorMsg = 'Aadhar is not valid';
$scope.error = true;
}
else
{
$scope.user.aadharErrorMsg = '';
$scope.error = false;
}
}
我在浏览器控制台中收到错误,无法读取未定义的属性“匹配”。请帮助。
最佳答案
您尝试过在 Angular 中使用 ng-messages 吗?
以下是使用 ng-messages 获取有效电子邮件的示例代码:
<div>
<label>User Email</label>
<div>
<input type="text" placeholder="paul@lebensraum.com" ng-model="user.email" name="userMail" required ng-pattern="/^[_a-zA-Z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/">
</div>
<div ng-messages="user.userMail.$error" class="error" role="alert" ng-if="user.userMail.$dirty">
<div ng-message="required" style="margin-left: -13%;">Please User Email ID</div>
<div ng-message="pattern" style="margin-left: -13%;">Please Enter A Valid Email Id</div>
</div>
</div>
关于javascript - 使用正则表达式检查姓名、10位手机号码和12位号码,使用ng-blur和AngularJS中的匹配功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41690291/