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. Total max characters are 11.
Sample: 09 493432199 or 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
when the number has exactly 11 digits
pattern = /09[0-9]{9}/
Try it out here 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
<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:
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}
In this example 09[0-9]{8,9}
are valid 0912345678 and 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. 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.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.