"required" is not Required

One of the new attributes that is proposed as a part of HTML 5 is the "required" attribute which can be added to input fields in forms to indicate that the field requires that something be entered in it - ie that it is a mandatory field. Another attribute that HTML 5 proposes to allow on those same input fields is a "pattern" attribute that allows you to specify a regular expression that the data entered into the field must match.

The introduction of the pattern attribute does away with the need to ever use the required attribute. So HTML 5 is introducing two new attributes where one makes the other obsolete right from the start.

The first thing to consider in deciding that pattern makes required obsolete is in considering just what required is supposed to mean. Has the requirement that something be entered into the field been met if the person filling out the form enters a single space in that field? What about if they enter as many spaces as the field can hold? Simply saying that a field is required does not directly prevent that condition being met simply through filling the field with spaces. Of course spaces are unlikely to be valid input for any input field that is mandatory and so specifying that the field is required usually ought to mean that it contains at least one character in the field that isn't whitespace. There isn't anything obvious to say which way that browsers should treat a required field as to whether or not whitespace should count as input. There also isn't any real way that you can clearly identify to the browser as to whether or not you intend that required should or shouldn't allow whitespace - at least not using "required".

In fact you can tell the browser that a field is required and also tell it at the same time whether or not entering nothing but whitespace into the field is acceptable. You can do this very easily using the pattern attribute.


These two values for the pattern attribute identify that the field is mandatory. The first of these will allow any characters entered into the field to satisfy the requirement that something be entered into the field while the second version specifically requires that something other than whitespace be entered into the field. So using pattern instead of required allows you to be more specific as to exactly what meets the criteria for a mandatory input field.

The next thing to consider is with regard to where you want to both specify that a field is required and provide a pattern that the content must match. We can't set the pattern to one of these two values to indicate that a field is required if we want to use some other value with the pattern. Well as it happens, any value at all that you specify as the pattern with the exception of pattern="" (which does absolutely nothing and therefore may as well be omitted) will also make the field mandatory since the input has to match the pattern. The only way in which "required" could ever have any use on a field that has a pattern would be if the browser were to only test the pattern if something were actually input into the field and there's absolutely no reason why the two attributes should be implemented that way since you can easily identify where a field is optional simply by making a small modification to the pattern.


Let's take ... as representing any pattern at all that the input must match if something is entered into the field. All we need to do in order to specify that the field is not mandatory is to modify that pattern by adding the changes shown in the second line above. The pattern now identifies two alternatives that the content can match - the second one is the same pattern that we had as mandatory before we made the change and the new pattern we have added in front of it tests if absolutely nothing at all has been entered into the field. Since any pattern at all can be easily modified like this (which is why I represented the original pattern with ... and not something more specific) there is absolutely no reason why browsers need to treat the pattern attribute as an option that only needs testing when something is actually entered into the field. The pattern itself can easily identify whether or not the field is optional.

These two aspects of what can be done clearly demonstrate that there is no need whatsoever for HTML 5 to keep the "required" attribute since the new "pattern" attribute has effectively replaced it with something more specific. For fields that are mandatory but where anything is allowed to be entered the two alternative patterns allow you to be more specific as to whether whitespace by itself is valid or not. The seven extra characters that can be added to any pattern to specify that the field is optional also do away completely with any ambiguity as to whether or not a pattern ought to be tested where nothing at all has been entered into the field.

Doing away with the redundant "required" attribute will also do away with the possibility of the two attributes conflicting with one another.

required="required" pattern="^$"

Any field containing these two attributes exactly as shown will never be valid. The required attribute makes it mandatory that the field contain something while the pattern specifies that the field must be left empty. So not entering anything will fail the required test and entering anything at all will fail the pattern test. By doing away with the "required" attribute it will make it impossible to specify that a field must both contain something and be empty at the same time in order to be valid.

One last point. You will note that in the last example the required attribute was specified with a value rather than just as the name by itself. With all the latest browsers now supporting XHTML we are only waiting for IE8 and earlier to die out and XHTML will become a practical alternative to HTML. As XHTML (which became a standard back in 1997) is more logically structured and provides better validation than HTML, there will be a lot of people switch from HTML 4 to XHTML 1.0 once it becomes practical to do so. Most likely all professionally developed pages will use XHTML while hobbyists will continue with HTML. Those developing HTML 5 have recognised this because as well as HTML 5 there is also XHTML 5 which is the XHTML equivalent. So when (X)HTML 5 finally becomes a standard and enough time after that elapses for the current and future browsers that do not fully support it to die out XHTML should already be well established as the alternative that all the professionals use for their web pages. XHTML requires that all attributes have values and so you end up with 19 characters to specify that a field is required using the "required" attribute compared to using 11 or 12 characters to be more specific with respect to whether whitespace satisfies the test. So not only does doing away with "required" make things less ambiguous, it also makes the XHTML a lot shorter.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow