[英]Input pattern - Mobile number - Start with a specific numbers
I trying to configure the pattern to be able to submit a mobile number that should start with 09 followed by a '0-9' digits.我尝试将模式配置为能够提交应以09开头后跟“0-9”数字的手机号码。 Total max characters are 11.最大字符总数为 11。
Sample: 09 493432199 or 09 267778595样品: 09 493432199 或09 267778595
<form action="/action_page.php">
<label for="username">Username:</label>
<input type="text" id="username" name="username" pattern="[09]+[0-9]" maxlength="11"><br><br>
<input type="submit" value="Submit">
</form>
You can use this RegExp你可以使用这个正则表达式
It will match它会匹配
when the number starts with 09 AND当数字以09 AND开头时
when the number has exactly 11 digits当数字正好有11 位数字时
pattern = /09[0-9]{9}/
Try it out here https://regex101.com/r/Nl2c2y/3在这里试试https://regex101.com/r/Nl2c2y/3
<form action="/action_page.php"> <label for="username">Username:</label> <input type="text" id="username" name="username" pattern="09[0-9]{9}" maxlength="11"><br><br> <input type="submit" value="Submit"> </form>
Your html seems nice你的 html 看起来不错
<form action="/action_page.php">
<label for="username">Username:</label>
<input type="text" id="username" name="username" pattern="[0-9]" maxlength="11"><br><br>
<input type="submit" value="Submit">
</form>
You need also have JS like follow:你还需要像下面这样的 JS:
var username_prefix = $('#username').subsubstr(0,2);
$('#validate').click(function(){
if (username_prefix == 09) {
alert('correct');
}
else {
alert('incorrect');
}
});
Use an expression with fixed digits plus a specific amount of digits.使用具有固定位数加上特定位数的表达式。
If you need the exact 11 characters, then use 09[0-9]{9}
如果您需要确切的 11 个字符,请使用09[0-9]{9}
In this example 09[0-9]{8,9}
are valid 0912345678 and 09123456789在这个例子中09[0-9]{8,9}
是有效的 0912345678 和 09123456789
<form action="/action_page.php"> <label for="username">Username:</label> <input type="text" id="username" name="username" pattern="09[0-9]{8,9}" maxlength="11"><br><br> <input type="submit" value="Submit"> </form>
As a side note, even if using input pattern
is supported by the most common browsers, you should not rely only on the client side validation.附带说明一下,即使最常见的浏览器支持使用input pattern
,您也不应该只依赖客户端验证。 Use a similar validation from the PHP backend to verify the information (in this case additional ^
(start of expression) and $
(end of expression) were added.使用来自 PHP 后端的类似验证来验证信息(在这种情况下,添加了额外的^
(表达式开始)和$
(表达式结束)。
if (!preg_match("/^09[0-9]{9}$/",$_GET["username"])){
// not valid
}
<form action="/action_page.php">
<label for="username">Username:</label>
<input type="text" id="username" name="username" pattern="[0-9]" title="Zero is required" maxlength="11"><br><br>
<input type="submit" value="Submit">
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.