Html5 input pattern check - html

i'm not good in html pattern validation.
I have this problem, my input text is valid only : min 3 max 30 chars,
white space at first and at end of the string is not allowed, is allowed white space between one word and another is allowed, is allowed A-Za-z, first char of word it must be Uppercase and the others word it must be Lowercase.
Thanks.
--UPDATE--
input#name
Valid Examples:
'Mario Giovanni'
'Maria'
'Jacopo Karol Pio'
'Jacopo K'
Invalid Examples:
' Mario Giovanni'
'Mario Giovanni '
' Mario Giovanni '
'Mario Giovanni'
'maria'
'mAria'
'Antonio mario'

If you need pure regex then this should work for you:
<input type="text" pattern="(?=^.{3,30}$)^[A-Z][a-z]*(?: [a-z]+)*$">
(?=^.{3,30}$) - use a positive lookahead to make sure we have between 3 and 30 chars
^[A-Z] - require start with a capital letter
[a-z]* - optionally allow lowercase letters to follow
(?: [a-z]+)* - optionally allow a repeating group of a space char follow by one or more letters
$ - end of string anchor

You will want to use a Regular Expression pattern to check whether the input is valid or not, as well as the maxlength and minlength attributes to ensure that the input is between 3 and 30 characters.
Regarding the RegEx pattern, we must:
Start at the beginning of the input: ^
Verify that the first character is between A and Z: [A-Z]
Verify that the following characters before the last one are lowercase letters or spaces: [a-z ]*, where * indicates that there might be multiple characters matching that part of the pattern; if you only want to allow one space between word, then use ([a-z]* ?)
Verify that the last character is a lowercase letter: [a-z]$, where $ indicates the end of the input
Below is the code I would use.
<input type="text" minlength=3 maxlength=30 pattern="^[A-Z][a-z ]*[a-z]$">

Looks like what you wait is:
<input type="text" pattern="(?=^.{3,30}$)^[A-Z][a-z]+( [A-Z][a-z]+)*$">
Notice this is being validated in the user browser, and doesn't configure a secure input validation. You should check the input again at server-side before using it anywhere.

Related

How to require some characters and restrict others with Regex in HTML input pattern

As a user is typing their new password, I want them to be able to see if their password meets the requirements or not. I have a Regex pattern set up inside the input tag, and if the requirements aren't met, the input box is outlined in red and the form can't be submitted. This looks like:
<input type="password" pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.[!###$%&?]).{8,32})" placeholder="Password" required />
I adapted that pattern from here. I have it so that the user's password must contain 8-32 characters, one uppercase letter, one lowercase letter, one special character (!##$%&?), and one number, and that works fine. However, I also want to exclude some special characters from the input, for example, the semicolon (;). I have tried adding (?!.[;]) like so:
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.[!###$%&?])(?!.[;]).{8,32})
This breaks the entire pattern, though. Could someone explain what I am doing wrong and how I can fix it? Or would it be better to do this manually by using a Javascript listener to check if the password meets the requirements each time the user does a keypress?
Try with:
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!###$%&?])(?!.*[;]).{8,32})
Fine-tunings from your regex:
Added back an asterisk * to the positive lookahead for symbols list [!###$%&?]
Added back an asterisk * to the negative lookahead for symbol [;]
You need an asterisk * after the dot . in order to allow matching of multiple characters (by *) for any character (by .). Otherwise, your regex allows only ONE single character before the symbols to include and exclude.

HTML5 pattern need help making a specific pattern for an input

Im trying to make a pattern that will allow input such as SB 0023467-01 or ST 0023467-02
<input type="text" name="surat_beranak" pattern="[S]+[B\s]+[0-9]{7}+[-]+[0]+[^0-1]"
oninvalid="this.setCustomValidity('Format SB 0029384-01')"value="<?= $surat_beranak;?>"
class="form-control" placeholder="Enter Surat Beranak Number" required>
This is my current input pattern, but it's not working as intended. I need help where the pattern will allow only S as the first letter, then B or T as the second letter, then have a space after. Then there will be 7 numeric digits followed by a dash and a number, 0 then 1 or 2
You're using + between every character, which allows a character to be repeated many times. It is the equivalent of using {1,}. [0-9]{7}+ is invalid.
You're using square brackets, allowing different characters to be used in a same place. A good place to use these would be for the beginning: S[BT] would allow SB or ST. There is no need to wrap single characters with these brackets.
Your current pattern ends with [^0-1], which means "every character except 0 and 1". It goes against your goal of having "01" or "02" at the end.
Your pattern, in HTML, would be S[BT] [0-9]{7}-0[12]. If you're using this pattern anywhere else, you should wrap it between ^ and $, in order to only allow the input you want, and nothing else.
In the future, please use regex testers in order to test your pattern. Example with the working pattern and a few input examples.

Pure HTML input type text validation using pattern

Recently I tried alot but I still unable to figure how should I able to validation for my text field. I hope I can get some help from here, my questions is I want to validate input text field only accept A-Z a-z 0-9 and space within word. And at least one char or number.
For example, "abc def" , "abc092", "abcdef"
Only in HTML input tag element .
I tried this
but this pattern unable to fullfil my requirements.
the pattern i want to achieve is
1) abc def
2) abcdef
3) abc123
4) a1b2c3 d4e5
5) allow to have empty space within words
the pattern i dont want to accept is
1) empty string
2) no alot of whitespace at the begining or end of the string
3) no bracket and etc special characters
Try
<input type="text" pattern="^\w+([\w ]*\w)*$">
Basically the break down is this:
\w+ - Select a word character ("A-z0-9") one or more times
()* - Select what's in here 0 or more times, which is
[\w ]*\w - Select a word character or space one or more times followed by another word character
No leading or trailing white space allowed. Only word characters allowed and internal spaces.
For some unit tests and breakdown of the regex see: https://regex101.com/r/7UnL9J/1
You can use the Pattern attribute with regex but it is supported only in HTML 5.
Like this " id="username" pattern="[A-Za-z0-9]+"
Check the below link for more information
https://html.com/attributes/input-pattern/#Username_Patterns
Have you tried this?
<input type="text" pattern="[a-zA-Z0-9\s]+">

HTML Input Pattern Comma and Letters

What is the correct pattern for a text input to only allow uppercase letters, lowercase letters, and commas?
I know that this is correct for the letters:
pattern="[a-zA-Z]"
but I dont know how to allow commas.
Thanks for any help!
Short answer:
pattern="^[a-zA-Z,]*$"
A couple of comment:
* means zero or more characters which means this patter will allow empty fields as well. If you want to guarantee that it will contain at least one character, use + instead of *.
^ means beginning of the string and $ is the end. If you don't use them then something like this would be possible "!#123asdSDADS,,,21312312(2"

RegEx for at least 1 number, 1 lower case and 1 upper case letter

Currently, I'm using one that I found with a length restriction:
<input #password="ngModel" type="password" name="password" minlength="5" maxlength="30" pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})" required ngModel>
It works, but I don't want the length restriction at the end. I don't know much about regular expressions, so I assumed that removing {6,20} at the end would get the job done, but I was wrong.
So my question is: How do I make this regex work without the length restriction? Thanks!
You are using an HTML5 pattern attribute that anchors a regex by default (it actually wraps the pattern with ^(?: and )$). That means the pattern must match the entire string. This is the reason why you can't just take out .{6,20} and keep the lookaheads.
You need to use the lookaheads at the beginning, and .* (to allow 0 or more chars in the input) or .+ (to disallow empty input) at the end (as the consuming pattern part):
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*"
This will be successfully translated into /^(?:(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*)$/ and will work as expected:
^ - start of string
(?: - start of the non-capturing group
(?=.*\d) - 1 digit after any 0+ chars from the current position is required
(?=.*[a-z]) - 1 lowercase letter after any 0+ chars from the current position is required
(?=.*[A-Z]) - 1 uppercase letter after any 0+ chars from the current position is required
.* - any 0 or more chars
) - end of the non-capturing group
$ - end of string.