HTML Input Field Min/Max Length - html

I need a input field only accept string 6-10 Upper case and lower case
<input type"text" pattern>

<input type="text" minlength=2 maxlength=10>

use minlength and maxlength to dictate length. For example,
<input minlength="6" maxlength="10" type="text">
will only accept input between six and ten characters.

Related

Contact number in HTML Form

if we uses "tel" in input then we can give max or min length for the input but its drawback is it may take all kind of inputs whether it is numeric or alphabets,
And if we uses "number" for input then it takes only number but we can not give it min-max characters limit.
Even though I have mentioned pattern inside it but no change.
So is there any alternate for the same to give max-min length and can take only numeric?
<div class="form-group">
<label for="exampleFormControlInput3">Mobile Number</label>
<div class="input-group">
<span class="input-group-text" id="basic-addon1">+91</span>
<input type="tel" class="form-control" id="exampleFormControlInput3" maxlength="10" placeholder="012-345-6789" pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}">
</div>
</div>
Note: I have used bootstrap5 for the same.
You using input type tel and can make a mix with javascript if you want avoid completely alphabetic characters. The type tel provides pattern to but it validate afterwards. And not every browser will supported.
<label for="phone">Enter your phone number:</label>
<input type="tel" id="phone" name="phone"
maxlength ="6"
pattern="[0-9]{3}-[0-9]{3}"
oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');"
required>
<small>Format: 123-456</small>
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/tel
Semantically using type="number" is incorrect. You should be only using type='tel'. If you want fine control over what the user inputs, you should be using a pattern for that.
<input type="tel" id="phone" name="phone"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
required>
Read this doc, https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/tel

Set the exact number of elements required for an Input field with type number

I have a field input with type number in my form I would like to allow exact 13 characters to that field no less no more as per the requirement. I wanted to know is there any by default option in HTML using which I can accomplish this? I tried pattern and maxlength but for some reason they are ineffective with the type number. If it is not possible from HTML side then I would like to achieve the same using the angularjs. Any inputs would be really useful.
<input type="number" id="userCode" ng-model="formdata.userCode" title="13 Digit code"></input>
As per the below answer I added the min and max as 13. After adding it when I enter even 13 digits then also I get the error. Can someone please let me know what am I doing wrong here?
<input type="number" min="13" max="13" id="userCode" ng-model="formdata.userCode" placeholder="Enter 13 Digit Code" title="Please Enter the 13 digit code" class="form-control" style="width: 200px;" ng-required="true">
<form action="">
<input type="text" required pattern="[0-9]{13}" maxlength=13 id="userCode" ng-model="formdata.userCode" title="13 Digit code">
<input type="submit">
</form>
You can check here for some other attributes of input to play around with.
If anyone is looking they can also check this answer:
<input type="text" pattern=".{13,13}" oninput="this.value=this.value.replace(/[^0-9]/g,''); title="13 characters length required" ">
With this input field although its text we can make sure that it accepts only the numbers.

how to make the pattern to work with input number?

This is my first time using the pattern attribute. The following code is inside a <form> tag:
<input type="number" name="" placeholder="Number" pattern=".{12,13}" required>
I expect the pattern to make the number has minimal of 12 numbers and a max of 13 numbers. However, I can still submit the form with less than 12 numbers.
You can't use the pattern attribute on inputs of type number:
<input type="number"> elements do not support use of the pattern
attribute for making entered values conform to a specific regex
pattern. The rationale for this is that number inputs won't be valid
if they contain anything except numbers, and you can constrain the
minimum and maximum number of valid digits using the min and max
attributes
- MDN
As explained on MDN, you can use the min and max attributes to specify your domain:
<form>
<input type="number" name="number" placeholder="Number" min="100000000000" max="9999999999999" required />
<input type="submit" />
</form>
Alternatively, you can use an input type of text (which does allow pattern), and match for digits (\d):
<form>
<input type="text" name="number" placeholder="Number" pattern="\d{12,13}" inputmode="numeric" title="Your number must be 12-13 digits long" required />
<input type="submit" />
</form>

HTML5 only set minimum characters for input type number

I have this two inputs one of type text and the other number
<input type="number" class="form-control" id="phone" name="phone" placeholder="Telephone Number" maxlength="10" pattern=".{10,}" required title="10 characters minimum"/>
<input type="text" class="form-control" id="names" name="names" placeholder="Names" pattern=".{5,30}" title="Minimum of 5 characters required"/>
the input text is able to respond when the user fills less than 5 characters but the type number does not respond when the input is less or greater than 10. It does respond however to the required rule.
How can i set the minimum characters for the input type number without having to result to javascript?.
Maybe you could solve it with the min attribute of the input type number like min="1000000000".

pattern for input type="text" with min and max number

how to write pattern for input type="text" (it can't be number and the validation can't be with JS) that allow me to enter only numbers, min:1 and max:5000?
<input type="text" name="someName" id="someId" required pattern=""/>
Here you go - not an input with type="number" and no JS:
<input type="text" name="someName" id="someId" required="required"
pattern="(5000|([1-4][0-9][0-9][0-9])|([1-9][0-9][0-9])|([1-9][0-9])|[1-9])"/>
The basic pattern is to match 5000 or 4-digit number or 3-digit number or 2-digit number or non-zero 1-digit number.
If you can accept 0, the pattern can be even simpler:
(5000|([1-4]?[0-9]?[0-9]?[0-9]?))
You can use the predefined min and max attribute for input type="number" as follows,
<input type="number" min=0 max=10>
<input type="submit">
Here is an example
The error for incorrect input will be displayed when you try to submit the form
Let me know if this works :)
<input type="number">
HTML Version above!