HTML5 pattern attribute being ignored - html

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

Related

Pattern attribute not working as expected?

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"

How do I accept only numbers and reject letters for input tel

Despite my constant research, I have seen studies that accept letters for input tel.
<input
type="tel"
placeholder="Phone Number"
required
pattern="^[0-9-+\s()]*$"
maxLength="16"
minLength="6"
data-for="phoneNumber"
/>
Here I want it to only accept numbers. But it also accepts letters. How can I fix this?
The design team at the UK Government did a bunch of research and concluded that you shouldn't try to prevent that
The web and Android versions of Chrome implement this by silently discarding all letter input except the letter ‘e’.
This means users are not given feedback on what type of characters <input type="number"> accepts, and assistive technologies don’t alert the user that their input has been silently discarded.
According to them, your best bet would be to just use <input type="tel"> and validate server-side to make sure that letters aren't included.
Edit: although the link references <input type="number"> which you haven't asked about, my understanding of your request is that you're trying to prevent people entering certain characters into your input field, which <input type="number"> does, and the advise is: don't try.
Use input : number instead
<input type="number">
This will only accept number
Why not trying following regular expression REGEX, since it will also fit your length requirement:
<input
type="tel"
placeholder="Phone Number"
required
pattern="^[0-9-+\s()]{6,16}"
data-for="phoneNumber"
/>

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>

HTML form validation with the “pattern” attribute

I am trying to come up with a valid pattern for an HTML form. I would like to allow *#student.*.edu.vn
This is my code:
<input
type="text"
name="mail"
pattern="([a-z]|[0-9])+(#{1})+(student)+(.)+[a-z]+(.edu.vn)"
/>
It does not work as I would like. What I am doing wrong?
The pattern (regular expression) is slightly wrong as you haven't escaped all your special characters such as the . and have some unnecessary parts within it. Try using the following slightly modified pattern instead:
([a-z]|[0-9])+#student\.[a-z]+\.edu\.vn
<form>
<input type="text" name="mail" pattern="([a-z]|[0-9])+#student\.[a-z]+\.edu\.vn" />
<input type="submit">
</form>
Note: Make sure that you also do validation on the server-side as anyone entering values into a form can remove the pattern to bypass this check.
I came up with this solution which is slightly more easy to read and is less strict (which is a good thing because e-mail addresses can contain a lot more characters than just lowercase alpha-numeric ones. Don't forget to do proper validation on the backend using an e-mail validator.
<form>
<input
type="text"
name="mail"
pattern=".+#student\..+\.edu.vn"
/>
<input type="submit">
</form>