lundi 22 février 2021

How to set RegEx global flag in HTNL pattern?

I want to client validate a form input (username + password) before sending it to the server (php). Therefore I applied the pattern attribute in the input tag. I came up with a RegEx expression that does the job on the server side:

(preg_match_all('/^[a-zA-Z0-9. _äöüßÄÖÜ@-]{1,50}$/', $_POST['username']) == 0)

thereby the global flag is set using preg_match_all (instead of preg_match.

Now I wanted to implement the same RegEx in my pattern attribute in the HTML form. HTML standard defines that RegEx in pattern follows RegEx in JavaScript, which devides the expression into "pattern, flags" devided by a comma. I would translate that into HTML like this:

pattern="^[a-zA-Z0-9. _äöüßÄÖÜ@-]{1,50}$,g"

That doesn't work. All JavaScript RegEx validators I have found enclose the pattern into slashes:

/^[a-zA-Z0-9. _äöüßÄÖÜ@-]{1,50}$/

and say, that the global flag would be bhind the last slash:

pattern="/^[a-zA-Z0-9. _äöüßÄÖÜ@-]{1,50}$/g"

That doesn't work either. Mozilla also states in their developer guide (I also read it elswhere):

No forward slashes should be specified around the pattern text.

So, how can I get the global flag into the pattern attribute of the input element?

Aucun commentaire:

Enregistrer un commentaire