Regular expression in HTML5 input pattern with German Umlaute - html

I try to allow only certain letters in the HTML input field including German Umlaute.
However, using:
<input pattern="[a-zA-Z0-9-##.+_ \ä\ö\ü\Ä\Ö\Ü]" type="text" value="">
or alternatively:
<input pattern="[a-zA-Z0-9-##.+_ äöüÄÖÜ]" type="text" value="">
Gives the error (in Chrome):
Pattern attribute value [a-zA-Z0-9-##.+_ \ä\ö\ü\Ä\Ö\Ü] is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /[a-zA-Z0-9-##.+_ \ä\ö\ü\Ä\Ö\Ü]/: Invalid escape
How to include the Umlaute in the input pattern attribute?
Update:
It works now. Escape the special characters: pattern="[a-zA-Z0-9\.\-\+ äöüÄÖÜ]*"

Three errors in your regExp pattern.
\ä can't be escape by Browser like \b \s, because of ä is not a special character.
while - is considered as a string instead of [from-to], it must be escaped as \-.
// don't need escape
. _ = * ^ $ etc.
Four character need to be escaped are :
// need escape
[ ] - \ .
need a * at the end of your pattern to match more than one character.
The - after 9 must be escaped.
<input pattern="[a-zA-Z0-9\-##.+_ äöüÄÖÜ]*" type="text" value="">
Unicode flag used by default in pattern regExp in the current versions of Chrome and FF, and Browser will check your pattern is right or wrong.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode

Related

HTML Regex for domain name entry?

Currently I have a form accepting domain names, so I need to preclude every special character & space except for . and - being entered.
my current code:
<input type="text" name="domain-name" placeholder="i.e. example.com" pattern="^(\d|\w)+$" class="form-field w-input" required id="domain-name">
Won't accept . or -
How can I change it so that it accepts . and -
The pattern attribute should look like
pattern="[\w.-]+"
Details
The ^ (start of string) and $ (end of string) anchors are redundant in a pattern regex since the resulting RegExp object is compiled with the ^(?: before and )$ after the string pattern typed in the attribute value
\w matches digits by itself, so [\w\d] = \w
To match . or -, you need to put \w inside a character class, [], and add . and - to this character class.

Html5 input pattern check

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.

Html regex pattern: [\d\s-]{3} works but [\d-\s]{3} doesn't. Why?

Codepen example:
https://codepen.io/Trost/pen/KXBRbY
Try putting 1 symbol in both fields.
I can't get what's wrong. If I test these regex in https://regex101.com, they appear to be identical.
<form>
Works: <input type="text" name="country_code" pattern="[\d\s-]{3}" title="-23" required>
<input type="submit">
</form>
<form>
Bug: <input type="text" name="country_code" pattern="[\d-\s]{3}" title="- 3" required>
<input type="submit">
</form>
The real root cause here is that the regex [\d-\s] is used in the pattern HTML5 attribute, and in the latest versions of Chrome and FireFox is compiled as an ES2015-compatible regex with the u modifier. The consequence is that there are much stricter escaping rules for the Unicode regex patterns.
What it means is whenever a char cannot be parsed unambiguously, it is an error. When a char is escaped, but does not need escaping, it is again an error.
The chars that you may escape in the character class inside a u based regex are +, $, ^, *, (, ), |, \, [, ], ., ?, -, {, } (see this source). If the - is at the start/end of the character class, it still can go unescaped, as it can only be parsed as a literal hyphen there.
In between two shorthand character classes, an unescaped - will produce an error because it is treated as a user error.
So, either place a hyphen at the start/end (it is always the best option), or escape it inside the character class (and never escape it outside of the character class).
You define two different things:
[a-z] is a definition of a range - all characters from a to z.
[az-] is a definition of a set of three elements - a, z and
-.

HTML pattern is not working [duplicate]

.*(\d{3}\-\d{3}\-\d{2}\-\d{2}|\d{3}\-\d{2}\-\d{2}\-\d{3}|\d{10}).* this pattern was working fine. But suddenly it stop working in chrome and opera lately. What's going on here ? What a problem is here and how it's wrong? Opera is informing about invalid escape, same in chrome. It works fine when im checking it in js.
<form>
<input type="text" pattern=".*(\d{3}\-\d{3}\-\d{2}\-\d{2}|\d{3}\-\d{2}\-\d{2}\-\d{3}|\d{10}).*">
<button>
Send
</button>
</form>
The point is that Chrome and Firefox already support ES6 regex specifications and support the Unicode mode by default.
Unicode patterns have stricter rules as to what characters can be escaped inside the pattern. See this reference:
IdentityEscape: In BMP patterns, many characters can be prefixed with a backslash and are interpreted as themselves (for example: if \u is not followed by four hexadecimal digits, it is interpreted as u). In Unicode patterns that only works for the following characters (which frees up \u for Unicode code point escapes): ^ $ \ . * + ? ( ) [ ] { } |
The same set of chars is referred to as SyntaxCharacter in the ES6 specs page.
So, you can only escape the - inside the character class where it is considered a special character and to make it a literal you can escape it. Everywhere else it must not be escaped.
<form>
<input type="text" pattern=".*(\d{3}-\d{3}-\d{2}-\d{2}|\d{3}-\d{2}-\d{2}-\d{3}|\d{10}).*">
<input type=Submit>
</form>
Try to use below concept to implement to validate the date format
<form onsubmit="alert('Submitted.');return false;"><input required="" pattern="(0[1-9]|1[0-9]|2[0-9]|3[01]).(0[1-9]|1[012]).[0-9]{4}" value="" name="dates_pattern0" id="dates_pattern0" list="dates_pattern0_datalist" placeholder="Try it out." type="text"><input value="»" type="submit"></form>
you can find more validations by this link - http://html5pattern.com/Dates

Firefox input pattern regex range

This is related to the same problem as this question:
Firefox error: Unable to check input because the pattern is not a valid regexp: invalid identity escape in regular expression
When using escaped characters in the <input> pattern attribute, Firefox throws these errors to the console:
Unable to check <input
pattern='^[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEFa-zA-Z\s\'-]{1,50}$'>
because the pattern is not a valid regexp: invalid identity escape in
regular expression
So when using the pattern attribute on an <input> field, the unicode characters no longer need to be escaped. In that case the user simply needs to stop escaping their characters and change \#\% to #%, problem solved.
I've got this somewhat more complicated regex pattern, what do I change it to to work in Firefox?
<input type="text" pattern="^[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEFa-zA-Z\s\'-]{1,50}$">
Essentially it's allowing for any string between 1..50 characters in length as long as all the characters are within these ranges:
\u00A0-\uD7FF
\uF900-\uFDCF
\uFDF0-\uFFEF
a-z
A-Z
as well as whitespace, apostrophes and hyphens. A quick search sees the \u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEFa part of it fairly widely used in all sorts of regexes. I just don't see exactly what to use instead of the escaped unicode character references here.
You need to remove the escaping backslash before the single quote.
Note that in a regular HTML5 pattern field, one does not have to use ^ and $ anchors at the pattern start/end as the HTML5 pattern attribute encloses the passed pattern with ^(?: and )$. However, as per your feedback, the Abide validation circumvents this and passes unanchored pattern to the regex engine. Thus, you should keep the anchors.
<input type="text" pattern="^[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEFa-zA-Z\s'-]{1,50}$">
A quick demo:
<form>
<input type="text" pattern="[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEFa-zA-Z\s'-]{1,50}">
<input type="submit">
</form>