HTML5 Input Pattern Case-Insensitivity

2019-04-24 09:16发布

问题:

I'm trying to create something right now and I need the pattern attribute that was introduced in html5 tp be case-insensitive but as far as I know, you can't have flags to do this.

There was a question asked in 2011 where it was stated that input patterns are case-sensitive and I was hoping that this was changed since then. If not, I'd like to know if there is a way to get a pattern to become case-insensitive. I can't just use the basic [a-zA-z] because I need to match a four letter word so as an example, it would start off really long (word)|(WORD)|(Word)|(wOrd)... and so on.

Is there a way to get a case-insensitive pattern like this?

EDIT:

Appears I haven't been to specific in my details, I need a specific word, not just any 4 letter word.

回答1:

Ok I think I figured out an easier way to do the pattern other than listing each combination.

As an example, I'll use the word, "five".

[fF][iI][vV][eE]

so

pattern="[fF][iI][vV][eE]"


回答2:

You can use:

pattern="[A-Za-z]{4}"

For example:

<input type="text" name="username" required pattern="[A-Za-z]{4}" />

This input has to be filled up with four case-insensitive alpha characters.

Update: You didn't respond which specific words you want to match but you can do it like:

pattern="word|WORD|Word|wORD"

So, for example:

<input type="text" name="username" required pattern="word|WORD|Word|wORD" />

Find more variations if needed.