Pattern attribute not working as expected? - html

So I'm trying to make a form for my assignment, and I am using the pattern attribute in html so only certain characters are required to be typed on the text box. But for some reason, the form is saying using that I'm using an incorrect format even though I made my pattern attribute that way.
Here's an example of the code and a picture of the problem.
<form>
<label for="mnumber">Mobile Number:</label>
<input type="text" required id="mnumber" pattern="[0-9]"><br><br>
<input type="submit" value="submit">
</form>

You did write:
pattern="[0-9]"
You are asking for only one number. You just forget to add '+' to accept more than one number. I guess what you are searching for is this:
pattern="[0-9]+"

pattern="[0-9]"
allows for only a single number character to validate (and be submitted). If you want to allow more than one character, here's your options:
One or more numbers:
pattern="[0-9]+"
Zero or more numbers:
pattern="[0-9]*"
One to three numbers:
pattern="[0-9]{1,3}"

you just need to change type="text" to type="number"

Related

HTML Input pattern for "currency" field

Attempting to use a regex pattern for an HTML input element that limits the characters to 0-9, ",","-",".","$"
I have very little experience in regex and used the pattern=[0-9,.-$] on the input element and it does not work. Though I plan on studying regex intensely in the future, I need a little help on this for now. Thank you.
<form>
<input name="currency" pattern=[0-9,.-$]>
<button>Submit</button>
</form>
don't use regex, use native type="number" that will do job for you with no need for regex
coverage is almost full > https://caniuse.com/#feat=input-number
<form>
<input type="number" name="currency">
<button>Submit</button>
</form>
You have two problems.
- indicates a range, you already used it as such for 0-9. .-$ isn't a valid range. A validator would have highlighted this for you. You need to escape the -.
You are only matching one character. You need to match multiple so use something like + to mean one or more
<form>
<input name="currency" pattern="[0-9,.\-$]+">
<button>Submit</button>
</form>

Regex number pattern and maxlength not working on IE

Number pattern and max length are not working on IE.
I have tried variations of the below HTML, though IE appears to bypass that validation.
<input id="phone" maxlength="10" minlength="10" pattern="[0-9.]+" type="text">
Any suggestions on enforcing the above on IE?
The goal is to only allow 10-digits for that input field.
You can use an <input type="tel"> element, but it actually allows you to enter any characters. So, ultimately you need to specify a pattern and be very explicit as to what you are looking for and rely on HTML5 form validation. For example, if you are trying to input a phone number of the format 999.999.9999, then you want a something like the following. If you enter something that does not match the pattern and try to submit the form by hitting enter, you will get an error indication. Of course, use whatever pattern you want. If you just want digits and decimal points in any order (why?) but they must be length 10, then use pattern="[0-9.]{10}".
<form>
<input type="tel" pattern="[0-9]{3}\.[0-9]{3}\.[0-9]{4}" required>
</form>

HTML5 pattern attribute being ignored

Just trying to make a field that only accepts 8-digit numbers and every browser lets it pass with any number of digits.
<form>
<input type="number" pattern="[0-9]{8}" required>
<input type="submit">
</form>
I feel like I'm missing something really obvious here am not seeing the problem with my pattern.
Codepen: https://codepen.io/mavelo/pen/VVZvoP
<input type="number"> elements do not support use of the pattern attribute for making entered values conform to a specific regex pattern.
Source:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number#Pattern_validation

HTML - special input email pattern for more options

I need a specific pattern for email input. There should be 2 options:
john#doe.com
john#doe.com:anything
I need to fit in the ":anything" and make more possibilities after ":".
At the moment I'm using this but it does only validation for normal email:
<input class="input-fields" id="formUsername" name="username" type="text" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}" autofocus required/>
I tried many combinations but I just can figure out the right one.
You could add ':?\w*' to Your regex, so it would look like
[a-z0-9._%+-]+#[a-z0-9.-]+.[a-z]{2,3}:?\w*
can't you just add more textboxes(only for the extra part fiddle here)?
Optional(only for blahblahblah):
<input class="optional-fields" title="optional" id="designation" type="text" pattern="[a-z]{5,}">

HTML5 input pattern - exactly 'N' quantity of numbers

I want an input that it can only enter exactly N quantity of numbers , no less, no more.
I have tried this:
Example with 4 digits.
<input type="text" name="myinput" pattern="[0-9]{4}" title="Only four (4) digits.">
But it does not work, someone knows how to fix it?
By "it does not work" I assume you mean the pattern isn't checked when the input is empty. Otherwise there doesn't seem to be anything wrong with the pattern.
That is how the pattern attribute works. You'll have to add required as well if you don't want to allow empty input.
<form>
<input type="text" name="myinput" pattern="[0-9]{4}" required>
<button type="submit">Submit</button>
</form>