minlength attribute doesn't seem to be working - html

Say I have the following HTML:
<form>
Fax #: <input type="number" name="fax" minlength="10" required />
<button>Print</button>
</form>
If I enter in "11" as the Fax # and hit "Print" the form submits without issue. I would like it to present some sort of error. If the minlength attribute doesn't do that then what exactly does the minlength attribute do?
I'm using Google Chrome 74..

The minlength attribute doesn't apply for input of type number. This is actually quite reasonable. Numbers don't have a length, text do. For reference, see The official documentation.
Using input type="number" for a fax field is semantically incorrect, anyway. You should use input type="text". Then you can limit its length by the maxlength or minlength attributes or even use the pattern one.
If you absolutely need to use number as input type and you need to limit the value to 10 digits, you can do it by using min and max attributes:
Fax #: <input type="number" name="fax" min="1000000000" max="9999999999" required />
Like I said, though, this is absolutely incorrect semantically.

Related

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>

Why doesn't input minlength check work with initial value?

Consider the following form:
<form>
<input type="text" minlength="5" value="1234">
<button type="submit">submit</button>
</form>
When I click the submit button without changing anything, the minimum length validation doesn't work and the form submits successfully.
But after changing the input value, e.g. 1234 -> 12345 -> 1234, the validation works and the form does not get submitted.
Why?
This is by design. The minlength attribute only validates a field once it has been edited by the user. It doesn't validate the field if its value hasn't been changed, even if that value doesn't meet the constraint. From the spec (emphasis mine):
Constraint validation: If an element has a minimum allowed value length, its dirty value flag is true, its value was last changed by a user edit (as opposed to a change made by a script), its value is not the empty string, and the JavaScript string length of the element's API value is less than the element's minimum allowed value length, then the element is suffering from being too short.
If you need to validate the value regardless of whether the user has since edited the field, see Racil Hilan's answer (although their statement about the minlength attribute not being supported everywhere doesn't imply anything and is largely irrelevant — as shown, this is clearly by design; if anything it shows that the browsers that do support the attribute support it fully).
The minlength attribute is not supported in all browsers. You can use the pattern attribute instead. The required attribute is also needed, otherwise an input field with an empty value will be excluded from the validation.
Try this:
<form>
<input type="text" pattern=".{5,}" required value="1234">
<button type="submit">submit</button>
</form>
The added benefit of using the pattern attribute is that it validates initial values, so you will not have the issue that you've seen with the minlength attribute which doesn't validate initial values (as explained in details by BoltClock's answer). The downside, though, is that the validation message is not as elegant. For example, the message in Chrome is "Please match the requested format" for pattern and "Please lengthen this text to 5 characters or more" for minlength.
You can use placeholder="1234" instead of value="1234" and don't forget to put "required" into your input field. So it works after
<form role="form">
<input type="text" name="number" minlength="5" placeholder="1234" required>
<button type="submit">submit</button>
</form>

html pattern="[A-Za-z]{50}" is saying valid input is invalid

I have a page with some text fields and I would like to restrict user input, so I have used the html5 pattern attribute, like so:
<input name="team_name" type="text" pattern="[A-Za-z]{50}" value="<?php echo $team_name;?>">
This should allow me to enter only letters b/w Aa-Zz for a name, but when I try to enter a valid name, it still tells me to match the requested format.
What am I missing? How come it is always telling me that my input is invalid?
The rule [A-Za-z]{50} will make the name acceptable only when it contains exact 50 alphabets.
[A-Za-z]{10,50}
This will make the name minimum 10 and maximum 50 characters.
Update the expression according to your requirements.
Demo:
input:invalid {
color: red;
}
<form>
<input name="team_name" type="text" pattern="[A-Za-z]{10,50}" value="">
<br />
<input type="submit">
</form>
{50} in a regex pattern means exactly 50 characters. That's unlikely what you want.
If you want to restrict the input to maximum 50 characters, use the maxlength attribute.
<input name="team_name" type="text" pattern="[A-Za-z]+" maxlength="50">
As you can see in the following :
type="text" -> text validation happen.
pattern="[A-Za-z]{50}" -> only Aa - Zz alphabets are valid, character inputs are not valid. and string length should be equal to 50.

Can I add pattern attribute with input type as number?

Here I have a HTML5 input.....
<input type="number" pattern="\d{10}" data-pattern-msg="enter a value according to the pattern" />
But this is not validating this pattern ......
What is the reason for this ??
Maintainer of the W3C HTML Checker (validator) here. The reason the checker is emitting an error for your example is that the HTML spec doesn’t allow the pattern attribute to be specified for <input type=number> elements; see the The following content attributes must not be specified and do not apply to the element list in the Bookkeeping details section of the section on the HTML spec on <input type=number>.
And I’m not sure that most browsers support using placeholder with <input type=number>.
This is wrong type = "number" change to type="text" and try
<input type="text" pattern="\d{10}" data-pattern-msg="enter a value according to the pattern" title="only number" />
Definition and Usage
The pattern attribute specifies a regular expression that the element's value is checked against.
Note: The pattern attribute works with the following input types: text, search, url, tel, email, and password.
Tip: Use the global title attribute to describe the pattern to help the user.

how to specify either or placeholder

I have a form that takes phone number as input in a field
I have set the type as tel as per HTML5 standard. I need the input to accept either 8 digit or 10 digit values.
I tried
<input type='tel' placeholder='0123456789 or 12345678'></input>
i also tried adding pattern="[0-9]{8} or [0-9]{10}
but, did not work,
is there an other way
Its not the pattern I would recommend for telephone numbers but this should do as you ask:
pattern='[0-9]{8,10}'
<input type='tel' pattern='[0-9]{8}([0-9]{2})?' title='Phone Number (8 or 10 numbers)' />
First of all, I advise you currently do not use "tel" as the input type. The reason for this is that it is not yet supported by all the major browser providers. It might be suitable for say Google Chrome, but other browsers such as IE aren't able to support this input type yet. I personally stick to keeping the type as text for input on telephone numbers.
Secondly, the way you can limit the persons input amount is using the maxlength attribute.
<input type="text" maxlength="10"/>
Finally, the input tag is self closing. It's used in the format below:
<input />
Not:
<input></input>
Hope this helped. For more, look here: http://www.w3schools.com/tags/att_input_maxlength.asp
Change your input tag to this;
<input type="text" min="8" max="10" />
Found it myself guys
it was to be done with pattern attribute itself
<input type='tel' placeholder='0123456789' pattern='[0-9]{8}|[0-9]{10}'>
here '|' is the or in expression, thank guys however.
pattern='[0-9]{8}|[0-9]{10}'
resolved my problem