0

我正在尝试使用 Angular JS 在 Visualforce 页面上实现手机号码验证,但是在如何编写正则表达式时遇到问题。

给我的要求非常简单:

数字应该是 10 位长(我已经在字段上设置了 maxlength 属性,所以这个已经被处理了)它应该以 04 开头(因为它是澳大利亚手机)只应该允许数字。

我正在使用的正则表达式(在我的电话号码输入字段的 ng-pattern 属性中)是:

^/(04)[0-9]{10}/$

这在一定程度上起作用 - 它不允许除数字以外的任何内容,并且允许我输入以 04 开头的 10 位数字。但是它也允许我输入以 02、03 等开头的数字......

可能我错过了一件很简单的事情,但我查看了很多网站,包括这个网站,但找不到答案。

任何帮助将不胜感激,因为这已经使我头发白了几根。

4

3 回答 3

1

尝试使用此模式

在你的 HTML 文件中使用它:

<input type="text" (keypress)="keyPress($event)" minlength=10 maxlength=10>

在你的 JS 文件中使用它:

keyPress(event: any) {
    const pattern = /[0-9\+\-\ ]/;
    let inputChar = String.fromCharCode(event.charCode);
    if (event.keyCode != 8 && !pattern.test(inputChar)) {
    event.preventDefault();
    }
}
于 2017-10-26T07:24:02.517 回答
0

试试这个:

手机号码 :

//inside view:
<input type="text" class="form-control" ng-model="mobileNo" name="mobileNo" ng-pattern="regEx" />

//inside controller:
$scope.regEx="/^[0-9]{10,10}$/";
于 2016-09-14T09:17:42.377 回答
0

对于初学者,我必须为它创建一个完整的 js 函数......并且它会在你输入时进行验证。这是我的完整代码,我希望这可以帮助你到达你需要的地方。此函数在每次按下键时获取字符串,并允许 carrete 移动前、后、删除和退格。检查一下,如果它对你有帮助,请告诉我。您可以在任何情况下运行它,这就是我添加“04”验证的方式

//phone validation 10 digits and parenthesis (344)567-0011
function validatePhone(inputId) {
  let validKey = false;
  const input = document.getElementById(inputId);
  let enteredDigits = input.value;

  //switch to remove the country code added by default on autoComplete forms.
  if (enteredDigits.length > 10 && enteredDigits[0] == '+') {
    switch (enteredDigits.length) {
      case 12:
        enteredDigits = enteredDigits.slice(2);
        break;
      case 13:
        enteredDigits = enteredDigits.slice(3);
        break;
      case 14:
        enteredDigits = enteredDigits.slice(4);
        break;
      default:
        enteredDigits = enteredDigits.replace(/\D+/g, '');
    }
  }

  let newPhone = enteredDigits.replace(/\D+/g, ''); // This replace any character that is not a number.
  const key = event.keyCode || event.charCode; // Get the pressed key.
  let caretPosition = input.selectionStart; // get the carret position.

  // splits, removes the "-" and converts from array to string and gives the needed digits.
  const areaCode = newPhone.split('').splice(0, 3).toString().replace(/,/g, '');
  const threeDigit = newPhone.split('').splice(3, 3).toString().replace(/,/g, '');
  const fourDigit = newPhone.split('').splice(6, 8).toString().replace(/,/g, '');

  // Moving carret on different positions. when the numeric keys are being pressed.
  // Key >= 48 && key <= 58 number keys.
  // Key >= 96 && key <= 105 numeric path number keys.
  if ((key >= 48 && key <= 58) || (key >= 96 && key <= 105)) {
    validKey = true;

    if (threeDigit.length > 0) {
      if (caretPosition == 1) {
        caretPosition = caretPosition + 1;
      } else if (caretPosition == 4 && newPhone.length == 4) {
        caretPosition = caretPosition + 2;
      } else if (caretPosition == 4 && newPhone.length >= 5) {
        caretPosition = caretPosition + 1;
      } else if (caretPosition == 5) {
        caretPosition = caretPosition + 1;
      } else if (caretPosition >= 2 && caretPosition <= 3 && newPhone.length <= 4) {
        caretPosition = caretPosition + 1;
      }
    }
    if (fourDigit.length > 0 && caretPosition == 9) {
      caretPosition = caretPosition + 1;
    }
  }

  // Key = 8 = Backspace.
  else if (key == 8) {
    validKey = true;

    if (caretPosition == 3 && newPhone.length == 3) {
      caretPosition = caretPosition - 1;
    } else if (caretPosition == 2 && newPhone.length == 3) {
      caretPosition = caretPosition - 1;
    } else if (caretPosition == 1 && newPhone.length == 3 && threeDigit.length == 0) {
      caretPosition = caretPosition - 1;
    }
  }

  // Key = 46 = Delete. Key =37  = ArrowLeft. Key = 39 = ArrowRight.
  else if (key == 46 || key == 39 || key == 37) {
    validKey = true;

    // Delete
    if (key == 46) {
      if (caretPosition == 0 && newPhone.length > 3) {
        caretPosition = caretPosition + 1;
      } else if (caretPosition == 1 && newPhone.length == 3) {
        caretPosition = caretPosition - 1;
      } else if (caretPosition == 2 && newPhone.length == 3) {
        caretPosition = caretPosition - 1;
      } else if (caretPosition == 3 && newPhone.length == 3) {
        caretPosition = caretPosition - 1;
      } else if ((newPhone.length >= 4 && caretPosition == 4) || (newPhone.length >= 4 && caretPosition == 8)) {
        caretPosition = caretPosition + 1;
      }
    }
  }

  //here is the validation for the country that you need.
  if ((newPhone.length == 1 && newPhone[0] != '0') || (newPhone.length >= 2 && newPhone[1] != '4')) {
    alert('Must start with 04');
    newPhone = '';
  }

  // Adding the special character for formatting.
  if (threeDigit.length > 0 && fourDigit.length == 0) {
    newPhone = '(' + areaCode + ')' + threeDigit;
  } else if (fourDigit.length > 0 && threeDigit.length == 3) {
    newPhone = '(' + areaCode + ')' + threeDigit + '-' + fourDigit;
  }

  if (!validKey) {
    caretPosition = caretPosition - 1;
  }

  // Set new values.
  newPhone = newPhone.substring(0, 13);
  input.value = newPhone;
  input.focus();
  input.setSelectionRange(caretPosition, caretPosition);
}
<form name="myForm"
  onsubmit="return validateForm()"
  method="post">

  Phone number: <input type="text"
    id="phoneNumber"
    name="fPhone"
    onkeyup="validatePhone('phoneNumber')">
  <input type="submit"
    value="Submit">
</form>

于 2021-09-24T14:21:42.993 回答