Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the regex used by HTML5 forms for validation

Tags:

Some HTML5 input elements accept the pattern attribute, which is a regex for form validation. Some other HTML5 input elements, such as, input type=email does the validation automatically.

Now it seems that the way validation is handled is different accross browsers. Given a specific browser, say Chrome, is it possible to programmatically extract the regex used for validation? Or maybe there is documentation out there?

like image 230
Randomblue Avatar asked Oct 16 '11 17:10

Randomblue


People also ask

How do you validate a form in RegEx?

You can use regular expressions to match and validate the text that users enter in cfinput and cftextinput tags. Ordinary characters are combined with special characters to define the match pattern. The validation succeeds only if the user input matches the pattern.

Which HTML5 attribute is used for data validation?

The pattern attribute of the <input> element allows you to add basic data validation without resorting to JavaScript. It works by matching the input value against a regular expression.

What is RegEx in validation?

What is RegEx Validation (Regular Expression)? RegEx validation is essentially a syntax check which makes it possible to see whether an email address is spelled correctly, has no spaces, commas, and all the @s, dots and domain extensions are in the right place.

Does HTML5 have form validation?

Using HTML5, we can create a form with built in validation (i.e. no javascript required). Earlier, we were using JAVASCRIPT to control form validation. These form controls are meant for both Desktop, tablets and smart phones.


2 Answers

The HTML5 spec currently lists a valid email address as one matching the ABNF:

1*( atext / "." ) "@" ldh-str *( "." ldh-str )

which is elucidated in this question. @SLaks answer provides a regex equivalent.

That said, with a little digging through the source, shows that WebKit implemented email address validation using basically the same regex as SLaks answer, i.e.,

[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@[a-z0-9-]+(\.[a-z0-9-]+)*

However, there is no requirement that email addresses be validated by a regex. For example, Mozilla (Gecko) implemented email validation using a pretty basic finite state machine. Hence, there needn't be a regex involved in email validation.

like image 186
ig0774 Avatar answered Dec 04 '22 06:12

ig0774


The HTML5 spec now gives a (non-normative) regex which is supposed to exactly match all email addresses that it specifies as valid. There's a copy of it on my blog here: http://blog.gerv.net/2011/05/html5_email_address_regexp/ and in the spec itself: https://html.spec.whatwg.org/#e-mail-state-(type=email))

The version above is incorrect only in that it does not limit domain components to max 255 characters and does not prevent them beginning or ending with a "-".

Gerv

like image 43
Gervase Markham Avatar answered Dec 04 '22 06:12

Gervase Markham