HTML pattern is not working [duplicate] - html

.*(\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

Related

Regular expression in HTML5 input pattern with German Umlaute

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

ng-pattern for PCRE

I have this pattern for an input text field: /[\p{L}\'.\- ]{3,30}/ My intention is to accept the most broadly names of people on several alphabets of the world (Latin, Cyrillic, Chinese, etc.) It was tested in Regex101 and it works great. On other testers it doesn't work but my main issue comes as follows:
<form action="mailto:myemail#emailserver.com" id="formula" method="post" enctype="multipart/form-data"
name="formname" class="form-group pt-3" autocomplete="on" ng-submit="register()" novalidate>
<input type="text" name="nombre" ng-pattern="/[\p{L}\'.\- ]{3,30}/">
Here's my code for you to check: https://regex101.com/r/gOvO2M/8
It skips special characters, skips symbols, skips numbers, but when I see the live HTML in the browser, it doesn’t work properly.
In the error message, for validation purposes, I put:
<p class="formu-error" ng-show="formname.nombre.$touched && formname.nombre.$invalid">Please, write a valid name.</p>
The problem is when testing, I write only letters (no spaces, no hyphen because all that is optional) and still giving me the message of the error. Why?
Maybe because I am using \p{L} and that will work only in the server, when I code the server validation in PHP?
You can use
<input type="text" name="nombre" ng-pattern="/^(?=.{3,30}$)\p{L}+(?:['.\s-]\p{L}+)*$/u" ng-trim="false" />
Note the u flag, it enables the Unicode category (property) classes support in JavaScript with the ECMAScript 2018+ support.
Also, ng-trim="false" will prevent trimming whitespace before passing the input to the regex engine.
The regex means:
^ - start of string
(?=.{3,30}$) - the string must consist of 3 to 30 chars other than line break chars
\p{L}+ - one or more Unicode letters
(?:['.\s-]\p{L}+)* - zero or more repetitions of
['.\s-] - a ', ., whitespace or -
\p{L}+ - one or more Unicode letters
$ - end of string.
See the regex demo.

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

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>

urlencode send form

I have a simple search form
<form id="search" name="search" method="GET" action="search.php" enctype="application/x-www-form-urlencoded">
But this form doesn't encode all chars in a URL, ' isn't encoded to %27, shouldn't this be done automatically?
This is rather funny.. On the HTML specification it states that all Non-alphanumeric characters are represented by %HH (according to RFC 1738, Section 2.2). But if you actually go to the RFC1738, Section 2.2 has the following:
Thus, only alphanumerics, the special
characters "$-_.+!*'(),", and
reserved characters used for their
reserved purposes may be used
unencoded within a URL.
But Chrome seems to follow the specification to the letter, where as IE and Firefox choose to not encode those characters..