简体   繁体   English

如何为电话号码编写仅允许带加号的号码的正则表达式?

[英]How do I write a regular expresssion for phone number that allows number only with plus sign?

I am writing a simple validation for a phone number text input.我正在为电话号码文本输入编写一个简单的验证。 I want this text input not to allow any letters and symbols .我希望这个文本输入不允许任何字母和符号 I only want to allow number and a plus sign at the beginning of the number.我只想在数字的开头允许数字和加号。 The plus sign is optional.加号是可选的。

Current code:当前代码:

$(document).on("input", ".book-appointment-phone", function() {
  let phoneRegex = /[^0-9]/g;
  var digits = this.value.replace(phoneRegex, '');

  return phoneRegex.test(this.value = digits);
});

The current code above can allow numbers only.上面的当前代码只能允许数字。 Do you know how can I update the code above to allow an optional + sign at the beginning of the phone number?您知道如何更新上面的代码以允许在电话号码开头使用可选的+号吗?

For example:例如:

  • 0917678123 - Allowed 0917678123 - 允许
  • +9215671234 - Optional + sign at the beginning is Allowed +9215671234 - 允许在开头使用可选的 + 符号

Any help is greatly appreciated.任何帮助是极大的赞赏。 Thank you.谢谢你。

Try this尝试这个

^[\+\d]?(?:[\d-.\s()]*)$

https://regex101.com/r/npGFhU/1 https://regex101.com/r/npGFhU/1

^\+?\d*$

Matches your + at the start, then any digit, dash, space, dot, or brackets匹配开头的 +,然后匹配任何数字、破折号、空格、点或括号

you can check it here: http://regex101.com/r/mS9gD7你可以在这里查看: http : //regex101.com/r/mS9gD7

Try this;尝试这个;

^[\+]?[\d]*$

In the beginning of the expression, + is optional, then variable numbers.在表达式的开头,+ 是可选的,然后是变量数字。

This code might work for you.此代码可能对您有用。

 $(document).on("input", ".book-appointment-phone", function() { let phoneRegex = /([^0-9\\+])|(?<!^)(\\+)/g; var digits = this.value.replace(phoneRegex, ''); return phoneRegex.test(this.value = digits); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" class="book-appointment-phone">

It doesn't make sense to use the same regex for replace and for test .replacetest使用相同的正则表达式是没有意义的。 For replace, the regex should be对于替换,正则表达式应该是

/(?!^\+)\D+/g

where \\D is any non-digit character, and (?!^\\+) is a negative lookahead that prevents the \\D matching if it is a + at the start of the string.其中\\D是任何非数字字符,并且(?!^\\+)是一个否定的前瞻,如果它是字符串开头的+ ,它会阻止\\D匹配。

 document.querySelector('input').addEventListener('input', function () { let phoneNumber = this.value; // Disallow non-digits. Allow '+' if at start. phoneNumber = phoneNumber.replace(/(?!^\\+)\\D+/g, ''); // Allow, for example, 10 digits maximum. phoneNumber = phoneNumber.replace(/(\\+?\\d{10}).+/, '$1'); this.value = phoneNumber; });
 <input type="text">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL如何处理加号登录电话号码? - How does MySQL deal with plus sign in phone number? 带加号的电话号码验证可选 - Phone number validation with plus sign optional 电话号码验证正则表达式在开头和前面的数字中包含一个加号 - Phone number validation regular expression consist one plus sign in start and preceding digits Javascript正则表达式在开始处仅允许一个加号(+),并且字符串的长度不应超过15个字符数 - Javascript Regular Expression allow only one plus (+) sign at start and length of string should not more than 15 character number only 如何编写一个将电话号码作为字符串接收并验证它是否是美国电话号码的函数? - How do I write a function that takes in a telephone number as a string and validates if it is a US phone number or not? 单击电话号码时如何删除加号? - How to remove a plus when clicking on a phone number? 如何输入允许输入文字的数字? - How do I make a number input which allows text? 正则表达式,允许数字,空格,加号,连字符和括号 - Regular expression which allows numbers, spaces, plus sign, hyphen and brackets 如何使用javascript验证电话号码? - How do I validate a phone number with javascript? 如何编写一个仅允许某些字符重复一次的正则表达式? - How to write a regular expression that allows certain characters to only be repeated once?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM