Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have HTML5's a inputs pattern attribute ignore case

Tags:

html

regex

input

I need to have HTML's input elements pattern ignore the case of the value,
like have if the regex is /[a-z]*/ could I get it to match all uppercase letters too?
(I know I could just do /[a-zA-Z]*/, but that was an example.)

like image 516
Nick Beeuwsaert Avatar asked Apr 02 '11 17:04

Nick Beeuwsaert


People also ask

What does html5's pattern attribute do?

The pattern attribute specifies a regular expression the form control's value should match. If a non- null value doesn't conform to the constraints set by the pattern value, the ValidityState object's read-only patternMismatch property will be true.

Which input types can the attribute pattern be applied to?

Note: The pattern attribute works with the following input types: text, date, search, url, tel, email, and password. Tip: Use the global title attribute to describe the pattern to help the user. Tip: Learn more about regular expressions in our JavaScript tutorial.

Is regex case sensitive by default?

The default behavior of the regex is case sensitive which means upper and lowercase letters are interpreted as different.


1 Answers

I don't think it is possible.

  1. The specification on <input pattern> [1,2] specifies that

    • the pattern uses the ECMAScript (i.e. Javascript) flavor of regex

    • it is compiled "with the global, ignoreCase, and multiline flags disabled"

  2. In Javascript, the only way to make a regex ignore case is to set the modifier externally (/.../i). The PCRE syntax (?i) is not supported.

Therefore, the pattern is always case-sensitive and [a-zA-Z]* (i.e. making the regex itself explicitly case insensitive) is the only way to match the pattern in a case-insensitive way.

like image 60
kennytm Avatar answered Oct 11 '22 21:10

kennytm