I'm going to realize input of text like this:
<input type="text" maxlength="16" required/>
And want to use valid and invalid stations like this:
input:invalid {
background: #fdd;
}
input:valid {
background: #dfd;
}
But when i write any text my input is valid always. I tried use pattern:
pattern=".{16,}"
But that did not solve anything. Where is my mistake?
The input must be valid when length of input is equal to 16.
You're close! Here's how to do it.
Allowing only alphanumerics
<form>
<input id="username" type="text" pattern="[a-zA-Z0-9]{16}" required>
<input id="submit" type="submit" value="create">
</form>
Allowing any character
<form>
<input id="username" type="text" pattern=".{16}" maxlength="16" required>
<input id="submit" type="submit" value="create">
</form>
With jQuery mask() plugin
Here, we just force the delimiter to be part of the input value followed by the number of characters in each group. In this case 4 numbers followed by a space.
$(document).ready(function() {
$('#username').mask('9999 9999 9999 9999');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.0/jquery.mask.min.js"></script>
<form>
<input id="username" type="text" pattern="[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}" required>
<input id="submit" type="submit" value="create">
</form>
For more info about regex and patterns, check out Regexr.
In your code, the attribute maxlength="16" does not allow more than 16 characters to be typed in the input box. The regular expression you are using is valid when the input is between 0 and 16 characters. Therefore, you regexp should be:
.{16,16}
And your HTML code:
<input type="text" pattern=".{16,16}" maxlength="16" required/>
You might want to see it working in this fiddle.
Related
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: 09493432199 or 09267778595
<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>
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
}
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>
<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>
So I have this line of code :
<form>
<input max="250" required pattern="[A-Za-z0-9 \.\,\!\?\:\[\]\(\)\"\-\+]+"
class="inputBox" type="text" id="comment" name="CommentContent" placeholder="write here">
</form>
but it doesn't seem to match anything at all.
I got it to work for more simple regex but not for this one.
I used a site to validate it and it looks like it's valid.
Change the wrapping quote and don't escape the chars other than the []
<form>
<input max="250" required pattern='[A-Za-z0-9 .,!?:\[\]()"-+]+'
class="inputBox" type="text" id="comment" name="CommentContent" placeholder="write here">
</form>
<input class='inpuname' type='text' maxlength=5>
maxlength works
Now, I want the all of five characters to be alphanumeric
<input class='inpuname' type='text' pattern='[A-Za-z0-9]{5}' maxlength=5>
either maxlength nor pattern doesn't work. Any characters and any number of characters are allowed !
<form action="demo">
number:
<input type="text" pattern="[0-9]{5}"
maxlength=5
title="5 digit number">
<input type="submit">
</form>
This works. Try submitting the form with valid and invalid data
I have an input field which be should bigger than 0, I'm using min="0.00000001" to validate input number is > 0.
<input type="number" step="0.00000001" min="0.00000001" name="price" value="[%price%]" />
Since I don't want to specify the step and min, I just want to validate if the input number is bigger than 0.
Is there any better way to compare input? For example something like input > 0 or min > 0
I search for a solution but could not find one without using step+min.
Using only html5, can we do this? Thanks for any help
<form method="post">
<b>Number Input:</b>
<input type="number" step="0.00000001" min="0.00000001" name="number" value="" />
<input type="submit" class="submit" value="Save" />
</form>
There is no way doing this in pure HTML5 without JavaScript. As mentioned in comments, the pattern attribute cannot be used.
But this can be handled using trivial JavaScript code, invoked via the oninput attribute, and using setCustomValidity:
<form method="post">
<b>Number Input:</b>
<input type="number" step="any" min="0" name="number" value=""
oninput="check(this)" />
<input type="submit" class="submit" value="Save" />
</form>
<script>
function check(input) {
if (input.value == 0) {
input.setCustomValidity('The number must not be zero.');
} else {
// input is fine -- reset the error message
input.setCustomValidity('');
}
}
</script>
I have the following form
<form name="input" action="http://testdomain.com/search/?" method="get" autocomplete="off">
<input type="text" name="?wpv_paged_preload_reach=1&wpv_view_count=1&wpv_post_id=205499&wpv_post_search=">
<input type="submit" id="searchsubmit" value="">
</form>
However the actual URL displays the following search query:
/search/?%3Fwpv_paged_preload_reach%3D1%26wpv_view_count%3D1%26wpv_post_id%3D205499%26wpv_post_search%3D=test
It seems that special symbols such as ? and = are getting replaced with special Encoding characters.
My question is, how do I get the form to not switch my special symbols with the encoding characters?
Thanks
The name of an input element controls the name of one field. The browser doesn’t blindly mash it and its value together and send that to the server. For a GET request, you can include each one as a hidden field:
<form name="input" action="http://testdomain.com/search/" method="get" autocomplete="off">
<input type="hidden" name="wpv_paged_preload_reach" value="1" />
<input type="hidden" name="wpv_view_count" value="1" />
<input type="hidden" name="wpv_post_id" value="205499" />
<input type="text" name="wpv_post_search" />
<input type="submit" id="searchsubmit" />
</form>