Contact number in HTML Form - html

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

Related

HTML input element won't match pattern

I am having issues with getting a "Phone Number" input validated. I have written the regex pattern to require 10 numbers, but even when I plug in 10 numbers into the input field, I still get the error message: "Please match the requested format" and can't seem to figure out why this is.
All help and advice is greatly appreciated!
<input type="text" id="number" name="number" pattern="/^\d{10}$/" required>
Here is a code example from this page: https://www.w3schools.com/html/html_form_input_types.asp
You can directly do it with an in-build functionality of HTML.
Just use the type type="tel" and you should be good to go.
if you want 10 consecutive numbers without - you can also do: pattern="[0-9]{10}"
<form>
<label for="phone">Enter your phone number:</label>
<input type="tel" id="phone" name="phone" pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}">
</form>
Just place you're input in a <form> and hit submit. It should now match.
I don't believe you need the forward slashes in your regex pattern here.
Try this:
<input type="text" id="number" name="number" pattern="^\d{10}$" required>

HTML Input Field Min/Max Length

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.

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".

Why does my Foundation Abide pattern not work?

I have the following HTML for making sure password length is over 8 characters:
<label>Password <small>required</small>
<input type="password" name="password" id="password" required pattern=".{8,}">
</label>
<small class="error">Your password must be at least 8 characters long.</small>
This works. However, if in a different field I use the pattern [a-zA-Z]+, like this:
<label>Username <small>required</small>
<input type="text" name="username" required pattern="[a-zA-Z0-9]+">
</label>
<small class="error">Username must consist out of letters or numbers only.</small>
It will allow everything. If I change the pattern in the username field to .{8,}, it will only allow inputs that are over 8 characters in length, as expected. Why does the pattern for the username field not work?
The documentation states that this is the correct way to do it:
<label>Your name <small>required</small>
<input type="text" required pattern="[a-zA-Z]+">
</label>
<small class="error">Name is required and must be a string.</small>
The fix is to wrap the pattern in line begin and end characters, like such:
<input type="text" name="username" required pattern="^[a-zA-Z0-9]+$">
Maybe this is a bug in Foundation that needs to be reported on their Github?

Which of the following input element variations will show a numeric keypad in mobile browsers?

what is the correct code. Any idea.
<input type="text" pattern="[0-9]*"/>
<input type="number"/>
<input type="text" keyboard="numeric"/>
<input type="text" keyboard="number11"/>
You should use the following input, both will work:
<input type="number" />
<input type="tel" />
It depends. If you use <input type="number"> the browser will render it as a numerical input containing the up and down buttons to cycle the value. If you want it to be only numbers, but without the buttons, then use the pattern.