[英]Validate phone number using angular js
Want to set phone-number to 10 digits, How can I do this using Angular js. 想要将电话号码设置为10位,如何使用Angular js执行此操作。
This is what I have tried: 这是我尝试过的:
<form class="form-horizontal" role="form" method="post" name="registration" novalidate>
<div class="form-group" ng-class="{'has-error': registration.phone.$error.number}">
<label for="inputPhone" class="col-sm-3 control-label">Phone :</label>
<div class="col-sm-9">
<input type="number"
class="form-control"
ng-minlength="10"
ng-maxlength="10"
id="inputPhone"
name="phone"
placeholder="Phone"
ng-model="user.phone"
ng-required="true">
<span class="help-block"
ng-show="registration.phone.$error.required &&
registration.phone.$error.number">
Valid phone number is required
</span>
<span class="help-block"
ng-show="((registration.password.$error.minlength ||
registration.password.$error.maxlength) &&
registration.phone.$dirty) ">
phone number should be 10 digits
</span>
</div>
</div>
</form>
But I am not getting the validation error. 但我没有收到验证错误。
Try this: 尝试这个:
<form class="form-horizontal" role="form" method="post" name="registration" novalidate>
<div class="form-group" ng-class="{'has-error': registration.phone.$error.number}">
<label for="inputPhone" class="col-sm-3 control-label">Phone :</label>
<div class="col-sm-9">
<input type="number"
class="form-control"
ng-minlength="10"
ng-maxlength="10"
id="inputPhone"
name="phone"
placeholder="Phone"
ng-model="user.phone"
ng-required="true">
<span class="help-block"
ng-show="registration.phone.$error.required ||
registration.phone.$error.number">
Valid phone number is required
</span>
<span class="help-block"
ng-show="((registration.phone.$error.minlength ||
registration.phone.$error.maxlength) &&
registration.phone.$dirty) ">
phone number should be 10 digits
</span>
Basically you can create a regex to fulfil your needs and then assign that pattern to your input field. 基本上,您可以创建一个正则表达式来满足您的需求,然后将该模式分配给您的输入字段。
Or for a more direct approach: 或者更直接的方法:
<input type="number" require ng-pattern="<your regex here>">
More info @ angular docs here and here (built-in validators) 更多信息@ angular docs here and here(内置验证器)
You can also use ng-pattern ,[7-9] = > mobile number must start with 7 or 8 or 9 ,[0-9] = mobile number accepts digits ,{9} mobile number should be 10 digits. 你也可以使用ng-pattern,[7-9] =>手机号码必须以7或8或9开头,[0-9] =手机号码接受数字,{9}手机号码应为10位数。
function form($scope){ $scope.onSubmit = function(){ alert("form submitted"); } }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script> <div ng-app ng-controller="form"> <form name="myForm" ng-submit="onSubmit()"> <input type="number" ng-model="mobile_number" name="mobile_number" ng-pattern="/^[7-9][0-9]{9}$/" required> <span ng-show="myForm.mobile_number.$error.pattern">Please enter valid number!</span> <input type="submit" value="submit"/> </form> </div>
You can also use ng-pattern
and I feel that will be a best practice. 你也可以使用
ng-pattern
,我觉得这将是一个最佳实践。 Similarly try to use ng-message
. 同样尝试使用
ng-message
。 Please look the ng-pattern attribute on the following html. 请查看以下html中的ng-pattern属性。 The code snippet is partial but hope you understand it.
代码片段是部分的,但希望您理解它。
angular.module('myApp', ['ngMessages']);
angular.module("myApp.controllers",[]).controller("registerCtrl", function($scope, Client) {
$scope.ph_numbr = /^(\+?(\d{1}|\d{2}|\d{3})[- ]?)?\d{3}[- ]?\d{3}[- ]?\d{4}$/;
});
<form class="form-horizontal" role="form" method="post" name="registration" novalidate>
<div class="form-group" ng-class="{ 'has-error' : (registration.phone.$invalid || registration.phone.$pristine)}">
<label for="inputPhone" class="col-sm-3 control-label">Phone :</label>
<div class="col-sm-9">
<input type="number" class="form-control" ng-pattern="ph_numbr" id="inputPhone" name="phone" placeholder="Phone" ng-model="user.phone" ng-required="true">
<div class="help-block" ng-messages="registration.phone.$error">
<p ng-message="required">Phone number is required.</p>
<p ng-message="pattern">Phone number is invalid.</p>
</div>
</div>
</div>
</form>
use ng-intl-tel-input to validate mobile numbers for all countries. 使用ng-intl-tel-input验证所有国家/地区的移动电话号码。 you can set default country also.
您也可以设置默认国家/地区。 on npm: https://www.npmjs.com/package/ng-intl-tel-input
在npm: https ://www.npmjs.com/package/ng-intl-tel-input
more details: https://techpituwa.wordpress.com/2017/03/29/angular-js-phone-number-validation-with-ng-intl-tel-input/ 更多详情: https : //techpituwa.wordpress.com/2017/03/29/angular-js-phone-number-validation-with-ng-intl-tel-input/
An even cleaner and more professional look I have found is to use AngularUI Mask. 我发现更清晰,更专业的外观是使用AngularUI Mask。 Very simple to implement and the mask can be customized for other inputs as well.
实现起来非常简单,也可以为其他输入定制掩码。 Then a simple required validation is all you need.
然后,您只需要一个简单的必需验证。
<form name = "numberForm">
<div>
<input type="number"
placeholder = "Enter your phonenumber"
class = "formcontroll"
name = "numbers"
ng-minlength = "10"
ng-maxlength = "10"
ng-model="phno" required/>
<p ng-show = "numberForm.numbers.$error.required ||
numberForm.numbers.$error.number">
Valid phone number is required</p>
<p ng-show = "((numberForm.numbers.$error.minlength ||
numberForm.numbers.$error.maxlength)
&& numberForm.numbers.$dirty)">
Phone number should be 10 digits</p><br><br>
</div>
</form>
Use ng-pattern, in this example you can validate a simple patern with 10 numbers, when the patern is not matched ,the message is show and the button is disabled. 使用ng-pattern,在此示例中,您可以验证具有10个数字的简单patern,当patern不匹配时,将显示消息并禁用该按钮。
<form name="phoneNumber">
<label for="numCell" class="text-strong">Phone number</label>
<input id="numCell" type="text" name="inputCelular" ng-model="phoneNumber"
class="form-control" required ng-pattern="/^[0-9]{10,10}$/"></input>
<div class="alert-warning" ng-show="phoneNumber.inputCelular.$error.pattern">
<p> write a phone number</p>
</div>
<button id="button" class="btn btn-success" click-once ng-disabled="!phoneNumber.$valid" ng-click="callDigitaliza()">Buscar</button>
Also you can use another complex patern like 你也可以使用另一个复杂的模式
^+?\\d{1,3}?[- .]?(?(?:\\d{2,3}))?[- .]?\\d\\d\\d[- .]?\\d\\d\\d\\d$ ^ +?\\ d {1,3}?[ - 。]?(?(?:\\ d {2,3}))?[ - 。]?\\ d \\ d \\ d [ - 。]?\\ d \\ d \\ d \\ d $
, for more complex phone numbers ,更复杂的电话号码
<div ng-class="{'has-error': userForm.mobileno.$error.pattern ,'has-success': userForm.mobileno.$valid}">
<input type="text" name="mobileno" ng-model="mobileno" ng-pattern="/^[7-9][0-9]{9}$/" required>
Here "userForm" is my form name. 这里“userForm”是我的表单名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.