Html5 to accept only Number and characters - html

I need user to enter only numbers (0-9) and characters (a-zA-z)
<input type="text" pattern="^[a-zA-Z0-9]+$" />
I tried the above but this accepts a special character like :#,#$%^&*)! and no space in between
What I am looking for
Ex: Test123, 123John,

It needs to be wrapped in a <form>
<form>
<input type="text" pattern="^[a-zA-Z0-9]+$" />
</form>

Try this:
<input type="text" pattern="[a-zA-Z0-9]+" required>

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>

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

How to prevent PO Box in address field?

I use HTML5 patter to validate address field input. Here is example of my code:
<input class="form-control" type="text" name="address1" id="address1" value="" maxlength="40" required>
I would like to use pattern attribute to prevent PO box in the address field. Can anyone help me achieve that with patter regex?
Use a pattern with a negative lookahead. See Regular expression to match a line that doesn't contain a word and replace the word in those patterns with the pattern that matches box followed by a number.
.*(?![Bb][Oo][Xx]\s+\d)
<form>
<input type="text" pattern="((?![Bb][Oo][Xx]\s+\d).)*">
<input type="submit" value="Submit">
</form>

HTML form - input validation with Alphanumeric with symbols ()._-‘ only

I want to validate below input with Alphanumeric and ()._-‘ symbol only.
<input type="text" name="companyName" value="" pattern="[A-Za-z0-9()._-‘]+" title="Alphanumeric with symbols ()._-‘ only" required> </p>
But, when I try the input, only ._‘ can be used, ()- are not working.
You need to escape those characters. They have significance in RegEx and aren't treated as part of the expression. Try the following: [A-Za-z0-9\(\)._\-‘]+
Works fine for me, with the exception of the '-' which probably needs to be first:
[-A-Za-z0-9()._‘]
Try the code below. I don't know why, but the order for the pattern attribute matters.
<form>
<input type="text" name="companyName" value="" pattern="[A-Za-z0-9()_.'-]+" title="Alphanumeric with symbols ()._-‘ only" required>
<input type="submit">
</form>

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.