Performing logic on input attribute - html

Is there a way to perform checks on input attributes like you can for forms?
https://angular.io/guide/form-validation
I'm trying to get the "Enter a small number" text to appear if a number larger than max was typed
<div class="ui input right labeled">
<input type="number"
placeholder="{{ vm.object.objectTitle }}"
step="any"
min="1"
max="1000"
ng-model="vm.object.model"
ng-change="vm.inputChanged()"
name="inputFieldName">
</div>
<div ng-if="inputFieldName.$error.max"
style="color:red">
Enter a smaller number
</div>

Related

Increment arrows not showing up on form input

I have a basic form input for a number. The form works. The field will not accept letters being type into it. But the increment arrows that usually show up for type="number" aren't showing up. What am I missing?
<div>
<input
type="number"
id="cost"
className="form-field"
placeholder="Cost"
min="0"
onChange={handleChange}
value={hats.cost}/>
</div>

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

Setting min and max on input removes spacing?

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.

How do you validate a number field that is read only in html?

I have a number field and it is read-only as it is auto filled but if this number is negative, it should not accept the value however, it is.
My view:
<div class="mb-3" style="float:left;" style="margin-left: 200px;">
<label for="recipient-name" class="col-form-label">Admin time (minutes):</label>
<input type="number" style="width: 7em" name="admin_time" class="form-control #error('admin_time') is-invalid #enderror" id="admin_time" min="0" step="1" onkeydown="return false;" readonly required>
</div>
use "disabled" instead of "readonly". disabled element isnt fetchable.
and second way - use JS or PHP for avoiding this field (as there are 2 languages you'll need to do it yourself sadly + i dont know all form fields...)

input element isn't working for the pattern I'm trying to use

On my html page I have:
......
<div class="col-md-3">
<input class="form-control" [(ngModel)]="manufacturer.name" [value]="manufacturer.name" maxlength="30" name="name" id="name" #name="ngModel" pattern="[\x20-\x7E]" required />
</div>
<div class="col-md-6">
<validation-message [control]="name" [message]="'Manufacturer Name must be an alphanumeric value of no more than 30 characters'"></validation-message>
</div>
......
Where I'm trying to restrict input to Ascii 32 to 126 inclusive. However, my Pattern doesn't seem to work. I cannot see what is wrong with it. Basically my validation message stays constantly on screen. How should I write my pattern?