简体   繁体   English

电话号码的自定义html5验证正则表达式

[英]Custom html5 validation regex for phone numbers

I have a from in which user has to enter his phone number. 我必须从中输入用户的电话号码。

 <input id="username" title="Please enter valid phone number " maxlength="10" pattern="[0-9]{10}" name="phone" size="20" required oninvalid="setCustomValidity('Please enter a valid phone number.')" onchange="try{setCustomValidity('')}catch(e){}" > 

It works fine when validating but only for Numbers. 验证时效果很好,但仅适用于数字。 I want the numbers to be specifically starting from 971 as it is limited to UAE numbers only. 我希望数字特别从971开始,因为它仅限于阿联酋数字。 How can I do that 我怎样才能做到这一点

Try adding that number at first and reduce the occurrence number 尝试首先添加该数字并减少出现的次数

pattern="971[0-9]{7}"

 <input id="username" title="Please enter valid phone number " maxlength="10" pattern="971[0-9]{7}" name="phone" size="20" required oninvalid="setCustomValidity('Please enter a valid phone number.')" onchange="try{setCustomValidity('')}catch(e){}" > 

如果您要从971开始,则可以使用(假设10位数)

971[0-9]{7}

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

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