需要显示关于手机的验证错误消息

Need to display validation error message about Mobile phone

本文关键字:验证 错误 消息 手机 显示 于手机      更新时间:2023-09-26

我需要在以下情况下显示关于手机号码的错误消息:a)长度为10位。b)它不应该以0或1开头。c) 10位数字不能都相同。例如:(222)2222 - 222。

下面是html和js代码。

<label>Work Phone</label>
                                    <input id="" type="text" ui-mask="(999) 999-9999" name="workPhone"
                                           class="form-control"
                                           tabindex="16"
                                           ng-model="patientIn.addressList[0].phoneNumbers['work']"
                                           ng-change="setPhoneNumber('work')"
                                           ui-mask-placeholder-char="space"
                                           model-view-value="true"/>
                                    <p ng-show="(frmPatientEdit.$submitted && frmPatientEdit.workPhone.$invalid) || (frmPatientEdit.workPhone.$invalid && frmPatientEdit.workPhone.$touched)"
                                       class="error">Work Phone is Invalid.</p>

JS代码:

 $scope.setPhoneNumber = function (type) {
            if (typeof $scope.patientIn.addressList[0].phoneNumbers[type] !== "undefined" && $scope.patientIn.addressList[0].phoneNumbers[type] !== "") {
                var obj = $scope.patientIn.addressList[0].phoneNumbers.filter(function (item) {
                    return item.type === type;
                });
                var index = $scope.patientIn.addressList[0].phoneNumbers.indexOf(obj[0]);
                if (index > -1) {
                    $scope.patientIn.addressList[0].phoneNumbers[index].number = $scope.patientIn.addressList[0].phoneNumbers[type];
                } else {
                    //console.log('Adding number: ', $scope.patientIn.addressList[0].phoneNumbers[type], type);
                    $scope.patientIn.addressList[0].phoneNumbers.push({"type": type, "number": $scope.patientIn.addressList[0].phoneNumbers[type]});
                }
            }

        };

试试这个就行了

Html:

<ng-form name="myForm" ng-controller="mainCtrl">
    <div>Phone No :</div>
    <div>
      <input type="text" placeholder="9101234567" name="phone" minlength="10" maxlength="10" ng-pattern="phoneNumbr" ng-model="phone"/>
      <span class="error" ng-show="myForm.phone.$error.minlength">Phone no not less that 10 char.</span>
      <span class="error" ng-show="myForm.phone.$error.maxlength">Phone no not more than 10 char.</span>
      <br><span class="error" ng-show="myForm.phone.$error.pattern">Please match pattern [9101234567]</span>
    </div>
  </ng-form>

脚本:

var app = angular.module('myApp', []);
app.controller('mainCtrl', function($scope) {
  $scope.phoneNumbr = /^([2-9])(?!'1+$)'d{9}$/;
});

在这里,10位中的1被第一个位置2-9消耗,然后接下来的9位可以来自0-9

需求实现:

a)包含10位数字。- 已完成

b)不应该以0或1开头。——

c)所有10位数字不应相同。——

工作小提琴: https://jsfiddle.net/rohitjindal/p0rcwon2/

谢谢。

一种方法是结合使用正则表达式和JavaScript。

正则表达式将负责:

  1. 不能以0或1开头
  2. 应该包含10位数字

JavaScript代码将负责:

  1. 10位数字不能相同

函数如下:

function validatePhone(p){
    p = ''+p; //convert it to a string
    var regex = /^[2-9]'d{9}$/;
    return regex.test(Number(p)) && p.split('').reverse().join('') !== p
}