简体   繁体   English

"HTML 电话号码验证"

[英]HTML phone number validation

I am working on an HTML website and I am a little stuck in one situation.我正在开发一个 HTML 网站,但我有点陷入一种情况。

The client wants to add phone numerical numbers only, without alphabetic characters with the condition that the phone number field should be accepted and submit phone number if someone enters +91 in front of 10 digit phone number (ie +917894561236) and if he didn't add +91 the field should also accept 10 digit phone number but not less than 10 numbers.客户只想添加电话号码,没有字母字符,条件是如果有人在 10 位电话号码前输入 +91(即 +917894561236)并且如果他没有,则应接受电话号码字段并提交电话号码' t 添加 +91 该字段还应接受 10 位电话号码,但不少于 10 个号码。

Here is my html:这是我的html:

<div class="form-group">
  <label for="">Phone number *</label>
  <input type="text" class="form-control" minlength=10 required name="phone_m" id="phoned"
         placeholder="Enter Your Phone Number" pattern="^(\+91[\-\s]?)?[0]?(91)?[6789]\d{9}$"
         oninput="if (typeof this.reportValidity === 'function') {this.reportValidity();}" / >
</div>

The below jQuery script is used to remove alphabets from phone field, however adding type="tel" in input field dosn't stop users to type alphabets下面的 jQuery 脚本用于从电话字段中删除字母,但是在输入字段中添加type="tel"不会阻止用户输入字母

<script>
//to get only numeric values in phone number field
jQuery('#phoned').keyup(function () { 
    this.value = this.value.replace(/[^0-9+\.-]/g, '');
});
</script>

I am working on an HTML website and I am a little stuck in one situation.我正在一个HTML网站上工作,在一种情况下我有些卡住了。

The client wants to add phone numerical numbers only, without alphabetic characters with the condition that the phone number field should be accepted and submit phone number if someone enters +91 in front of 10 digit phone number (ie +917894561236) and if he didn't add +91 the field should also accept 10 digit phone number but not less than 10 numbers.客户只想添加电话号码,不带字母字符,条件是应接受电话号码字段,如果有人在10位数电话号码前面输入+91(即+917894561236),并且如果他不输入电话号码,则提交电话号码t加+91,该字段还应接受10位数的电话号码,但不得少于10个数字。

Here is my html:这是我的html:

<div class="form-group">
  <label for="">Phone number *</label>
  <input type="text" class="form-control" minlength=10 required name="phone_m" id="phoned"
         placeholder="Enter Your Phone Number" pattern="^(\+91[\-\s]?)?[0]?(91)?[6789]\d{9}$"
         oninput="if (typeof this.reportValidity === 'function') {this.reportValidity();}" / >
</div>

The below jQuery script is used to remove alphabets from phone field, however adding type="tel" in input field dosn't stop users to type alphabets下面的jQuery脚本用于从电话字段中删除字母,但是在输入字段中添加type="tel"不会阻止用户键入字母

<script>
//to get only numeric values in phone number field
jQuery('#phoned').keyup(function () { 
    this.value = this.value.replace(/[^0-9+\.-]/g, '');
});
</script>

I am working on an HTML website and I am a little stuck in one situation.我正在一个HTML网站上工作,在一种情况下我有些卡住了。

The client wants to add phone numerical numbers only, without alphabetic characters with the condition that the phone number field should be accepted and submit phone number if someone enters +91 in front of 10 digit phone number (ie +917894561236) and if he didn't add +91 the field should also accept 10 digit phone number but not less than 10 numbers.客户只想添加电话号码,不带字母字符,条件是应接受电话号码字段,如果有人在10位数电话号码前面输入+91(即+917894561236),并且如果他不输入电话号码,则提交电话号码t加+91,该字段还应接受10位数的电话号码,但不得少于10个数字。

Here is my html:这是我的html:

<div class="form-group">
  <label for="">Phone number *</label>
  <input type="text" class="form-control" minlength=10 required name="phone_m" id="phoned"
         placeholder="Enter Your Phone Number" pattern="^(\+91[\-\s]?)?[0]?(91)?[6789]\d{9}$"
         oninput="if (typeof this.reportValidity === 'function') {this.reportValidity();}" / >
</div>

The below jQuery script is used to remove alphabets from phone field, however adding type="tel" in input field dosn't stop users to type alphabets下面的jQuery脚本用于从电话字段中删除字母,但是在输入字段中添加type="tel"不会阻止用户键入字母

<script>
//to get only numeric values in phone number field
jQuery('#phoned').keyup(function () { 
    this.value = this.value.replace(/[^0-9+\.-]/g, '');
});
</script>

I wrote and used this function for my client, it formats while they type it, and actually check if the char entered is numerical, trims the lenght as well.我为我的客户编写并使用了这个函数,它在他们输入时格式化,并实际检查输入的字符是否为数字,并修剪长度。 Well the format they wanted is "XXX-XXX-XXXX" but I am sure you can mod my code to suite your needs.他们想要的格式是“XXX-XXX-XXXX”,但我相信你可以修改我的代码以满足你的需要。 The regex is not perfect but if they copy and past s*** it will stop them from submitting.正则表达式并不完美,但如果他们复制并过去 s*** 它将阻止他们提交。

 document.getElementById("phone").onkeyup = function() { const CHAR_AMOUNT = this.value.length; \/\/ Check each character as the user types it, do not exceed the format we want of "XXX-XXX-XXXX". if (CHAR_AMOUNT < 13) { \/\/ Get the position of the last char, and check if the string has at least 1 char. \/\/ THAN get the last character typed by the user and check if it is a valid numerical type, if not, get rid of it. const LAST_CHAR_POS = CHAR_AMOUNT - 1; if (LAST_CHAR_POS >= 0) { const LAST_CHAR = this.value.charAt(LAST_CHAR_POS); if (isNaN(LAST_CHAR)) { this.value = this.value.slice(0, -1); return; } } \/\/ Here we auto fill the '-' char after the 3rd and 7th digit of the phone number. if ((CHAR_AMOUNT === 3) || (CHAR_AMOUNT === 7)) { this.value += "-"; } } else \/\/ Trim any excess characters on the phone number. including if someone is copy and poaste a long string. { this.value = this.value.slice(0, (12 - CHAR_AMOUNT)); \/\/ Ho well, if they copy past garbadge, let the HTML pattern stop them from submitting (-_-). } return; };<\/code><\/pre>
 <input type="tel" id="phone" pattern="\\d{3}-\\d{3}-\\d{4}" placeholder="123-456-7890" title="Must be XXX-XXX-XXXX" required \/><\/code><\/pre>

"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM