When adding a max value to an input number field in Chrome, it will re-size the field according to width of highest value.
It seems that I cannot control the re-sizing behavior.
<input type="number" name="" value="3500" placeholder="3500" min="500">
<br>
<input type="number" name="" value="3500" placeholder="3500" min="500" max="100000">
<br>
<input type="number" name="" value="3500" placeholder="3500" min="500" max="100000000">
Question: How can I make it behave like without max attribute or a normal text field.
PS: I cannot see any changes in the styles when switch the type.
PPS: I must say that I already tried using -webkit-appearance: textfield; but Chrome was already using textfield by default.
Solution with percentage and div
Although it's been almost 6 years since this question was posted, just a few minutes ago I found myself in the same situation as the OP. The solution is to add a div that acts as a container for the input field. Then to the width of the input we add the rule for fill all the available space and at this point we can just change the size of the div wrapper as desired.
The first two inputs show what happens without div. The last two inputs have the same size despite the different 'max' value.
p{margin-bottom: 0;}
.wrapped{
width: -moz-available;
width: -webkit-fill-available;
width: fill-available;
}
.inputWrapper{width: 30%;}
<p>Min 0, max 10</p>
<input type="number" min="0" max="10">
<p>Min 0, max 1000000</p>
<input type="number" min="0" max="1000000">
<div class="inputWrapper">
<p>Wrapped: min 0, max 10</p>
<input class="wrapped" type="number" min="0" max="10">
<p>Wrapped: min 0, max 1000000</p>
<input class="wrapped" type="number" min="0" max="1000000">
</div>
About the auto-resize of the input field
I honestly didn't read what the documentation says about this automatic resize and I was surprised when I noticed this unexpected and undesired behaviour. From the following simple tests we can see that the resize occurs only if both values 'min' and 'max' are set: the resize seems to be proportional only to the distance between max-0 instead of the distance max-min. Note that this is the default behavior without the proposed solution.
p{margin-bottom:0;}
<p>Min 0</p>
<input type="number" min="0">
<p>Min 100</p>
<input type="number" min="100">
<p>Max 0</p>
<input type="number" max="0">
<p>Max 100</p>
<input type="number" max="100">
<p> The previous input fields have the same width<p>
<p>Min 0, max 10</p>
<input type="number" min="0" max="10">
<p>Min 0, max 100</p>
<input type="number" min="0" max="100">
<p>Min 99, max 100</p>
<input type="number" min="99" max="100">
Simply set a width for input.
input {
width: 100px;
}
for example.
Related
For some reason, when I set min and max values on an input like so:
<p>The placeholder of the second input is cut off (i.e. input is collapsed) when the input is assigned a min and max value?</p>
<input type="number" placeholder="This is my placeholder.">
<input type="number" placeholder="This is my placeholder." min="1" max="15">
The input collapses into a small space:
Example
I'd like to prevent this from happening without having to set a "width" value on the input. Is this possible?
Thanks!
The browser is rendering the <input type="number" /> to the size that will fit its maximum content:
<label for="99">Max is 99</label>
<input name="99" id="99" type="number" min="1" max="99">
<br/>
<label for="99">Max is 999</label>
<input name="99" id="99" type="number" min="1" max="999">
<br/>
<label for="99">Max is 9999</label>
<input name="99" id="99" type="number" min="1" max="9999">
<br/>
<label for="99">Max is 99999</label>
<input name="99" id="99" type="number" min="1" max="99999">
<br/>
<label for="99">Max is 999999</label>
<input name="99" id="99" type="number" min="1" max="999999">
If you want to enter some placeholder value that exceeds that content, you'll have to resize it with CSS; the size and width attributes are not valid for the "number" type.
Also, as A Haworth points out in the comments, the usefulness of the placeholder here is rather limited-- in fact, I would challenge that perhaps placeholders are of rather limited benefit generally. Consider that if this text is important enough to be on the page, it is important enough to be displayed as part of the label, where it is always visible and of sufficient contrast that it can be easily seen and read.
When making an input, like so:
<input type="number" min="0" max="100"><br>
<input type="number" min="0" max="99"><br>
<input type="number" min="1" max="100"><br>
<input type="number" min="1" max="99"><br>
It shortens the width of the input field, specifically when max is at 100. Since i've never seen this, i can only guess it does this thinking i will use percentages or currency, which i actually do, but i do not want this. Is there another way to prevent this from happening besides using different values or changing the size of the input yourself?
This is the default behaviour of how some* browsers render those inputs. So the answer is No. If you want to have certain width, take a look at the other answers.
* Different browsers render elements differently:
And this is the result of your snippet in Mozilla Firefox:
The input field width adapts to the width of the highest value: 100. 99 has one less character, so the input field is shorter. I don't think you can prevent that difference without CSS if your max values have a different character length.
Now, CSS should be a piece of cake....
<style>.medium-input {width:100px;}</style>
<input type="number" min="0" max="100" class="medium-input"><br>
<input type="number" min="0" max="99" class="medium-input"><br>
<input type="number" min="1" max="100" class="medium-input"><br>
<input type="number" min="1" max="99" class="medium-input"><br>
I'm adding the same class to all of the inputs, so that you can also define some other lengths if you like.
You could also do it with input[type=number] as a selector.
you can add css to control the width of inputs.
this will do the job :
input[type=number]{
width: 150px;
}
I'm creating a sign-up form and one of the tags require you to enter your age. You can only create an account if you're older than 18 years old. So I thought I could achieve this by adding "pattern='18-65'". But that isn't working out. Right now, you can choose any number, even negative numbers. What am I doing wrong?
<input class="required" type="number" name="age" placeholder="Age (e.g. 25)" data-type="number" pattern="18-65">
You can set min and max attributes.
<input type="number" name="age" min="18" max="65">
Use min and max attribute to define the desired range.
<input class="required" type="number" name="age" placeholder="Age (e.g. 25)" min="18" max="65" />
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!
<input type="number" maxlength="2" />
Its working in other browsers like Firefox,IE and not working in google chrome.Can any one suggest how to fix issue in chrome?
because it's a number, you can use max, not maxlength.
You need to allow only two digit number then you can use following code
<input type="number" min="1" max="99" />
Use the max attribute for inputs of type="number".
It will specify the highest possible number that you may insert
<input type="number" max="999" />
if you add both a max and a min value you can specify the range of allowed values:
<input type="number" min="1" max="999" />
For user experience, you would prefer the user not to be able to enter more than a certain number, use Javascript/jQuery.
EG: <input type="number" max="99" min="9" id="myInput"/>
$('#myinput').on('keydown', function () {
if($(this).val().length>2)
return false;
});
use max="" and there is has to be a form element for it to work otherwise it won't work.
fiddle
Try this
<input type="tel" maxlength="2" />