AngularJS 表单验证手机号(非必填)

时间:2022-11-06 19:31:19

代码:

<form ng-app="myApp" ng-controller="validateCtrl" 
name
="myForm" novalidate>

<p>电话:<br>
<input type="text" name="phone" ng-model="phone" ng-pattern="/(^$)|^(((\+86)|(86))?1[34578]\d{9})$/">
<span style="color:red" ng-show="myForm.phone.$dirty && myForm.phone.$invalid">
联系电话格式不正确!
</span>
</p>

<p>
<input type="submit"
ng-disabled
="myForm.$invalid">
</p>
</form>

 

正则表达式:

1. 匹配空:^$ 

2. 匹配手机号:^(((\+86)|(86))?1[34578]\d{9})$

3. 控制提示信息的显示:ng-show="myForm.phone.$dirty && myForm.phone.$invalid"