How do i make a specific pattern for my html input - html

I'm trying to make a custom pattern for my html input but have no idea how to do so!
The pattern I want: ABC-A0-01
So basically the first part has uppercase alphabets only, second part has uppercase with numeric values, and the last part is numeric only and is separated by a '-'
<input type="text" class="form-control txtsize" id="equi" placeholder="Insert equipment name e.g ABC-A0-12" data-ng-model="equipmentToAdd">

You could use the pattern attribute with a RegEx such as this: pattern="[A-Z]{3}[-][A-Z]{1}[0-9]{1}[-][0-9]{2}".
Try inputting an invalid value, and hit submit. The browser will give an error with the message from title property.
<form>
<input type="text" class="form-control txtsize" id="equi" placeholder="Insert equipment name e.g ABC-A0-12" data-ng-model="equipmentToAdd" pattern="[A-Z]{3}[-][A-Z]{1}[0-9]{1}[-][0-9]{2}" title="Insert equipment name e.g ABC-A0-12">
<button type="submit">Submit</button>
</form>

Related

Pattern attribute not working as expected?

So I'm trying to make a form for my assignment, and I am using the pattern attribute in html so only certain characters are required to be typed on the text box. But for some reason, the form is saying using that I'm using an incorrect format even though I made my pattern attribute that way.
Here's an example of the code and a picture of the problem.
<form>
<label for="mnumber">Mobile Number:</label>
<input type="text" required id="mnumber" pattern="[0-9]"><br><br>
<input type="submit" value="submit">
</form>
You did write:
pattern="[0-9]"
You are asking for only one number. You just forget to add '+' to accept more than one number. I guess what you are searching for is this:
pattern="[0-9]+"
pattern="[0-9]"
allows for only a single number character to validate (and be submitted). If you want to allow more than one character, here's your options:
One or more numbers:
pattern="[0-9]+"
Zero or more numbers:
pattern="[0-9]*"
One to three numbers:
pattern="[0-9]{1,3}"
you just need to change type="text" to type="number"

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

HTML5 pattern attribute being ignored

Just trying to make a field that only accepts 8-digit numbers and every browser lets it pass with any number of digits.
<form>
<input type="number" pattern="[0-9]{8}" required>
<input type="submit">
</form>
I feel like I'm missing something really obvious here am not seeing the problem with my pattern.
Codepen: https://codepen.io/mavelo/pen/VVZvoP
<input type="number"> elements do not support use of the pattern attribute for making entered values conform to a specific regex pattern.
Source:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number#Pattern_validation

html pattern="[A-Za-z]{50}" is saying valid input is invalid

I have a page with some text fields and I would like to restrict user input, so I have used the html5 pattern attribute, like so:
<input name="team_name" type="text" pattern="[A-Za-z]{50}" value="<?php echo $team_name;?>">
This should allow me to enter only letters b/w Aa-Zz for a name, but when I try to enter a valid name, it still tells me to match the requested format.
What am I missing? How come it is always telling me that my input is invalid?
The rule [A-Za-z]{50} will make the name acceptable only when it contains exact 50 alphabets.
[A-Za-z]{10,50}
This will make the name minimum 10 and maximum 50 characters.
Update the expression according to your requirements.
Demo:
input:invalid {
color: red;
}
<form>
<input name="team_name" type="text" pattern="[A-Za-z]{10,50}" value="">
<br />
<input type="submit">
</form>
{50} in a regex pattern means exactly 50 characters. That's unlikely what you want.
If you want to restrict the input to maximum 50 characters, use the maxlength attribute.
<input name="team_name" type="text" pattern="[A-Za-z]+" maxlength="50">
As you can see in the following :
type="text" -> text validation happen.
pattern="[A-Za-z]{50}" -> only Aa - Zz alphabets are valid, character inputs are not valid. and string length should be equal to 50.