[英]How to validate a HTML input for a phone number
Brief简介
Validate a UK Mobile Phone Number field in a HTML Form to take into account the following rules:验证 HTML 表单中的英国手机号码字段以考虑以下规则:
Valid Input Examples有效输入示例
Where I am我在哪里
So far, I've only been able to validate my second point using:到目前为止,我只能使用以下方法验证我的第二点:
<input id="phonenum" type="tel" pattern="^\\d{11}$" required >
function validatePhoneNumber(){
var e = document.getElementById('phonenum');
var phoneNo;
var firstLetter;
var secondLetter;
if(!e){
return false;
}
phoneNo = e.value.replace(/ /g, ''); // remove spaces
if(phoneNo.length != 11 ){
return false;
}
firstLetter = phoneNo[0];
secondLetter = phoneNo[1];
if(firstLetter != "0" || secondLetter != "7"){
return false;
}
return true;
}
如果你想使用正则表达式,你可以使用以下
^(0\W*7\W*(?:\d\W*){9})$
希望能帮助到你
<input class="form-control" pattern="[789][0-9]{9}" type="text">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.