But of course you can put the relevant parts of the above code into a method.

Validating a phone number using regular expression is tricky because the phone number can be written in many formats and can have extensions also.

Let's walk through an example of how we can validate a form using only the browser's built in validation.

Take this simple booking form: attribute for the label tag matches up with the id attribute of the associated input tag.

The bad news is that it is only partially supported in Safari, and isn't supported at all on i OS Safari, or the default Android browser.

If you need to support older versions of IE prior to IE10 you won't find any of those support form validation either.

The first lines of a method are usually devoted to checking the validity of method arguments.

The idea is to fail as quickly as possible in the event of an error. It's a reasonable policy for a class to skip validating arguments of clause of the method's javadoc, since they clearly state the method's requirements to the caller (the pre-conditions).Others, however, feel that manually documenting exceptions should be avoided.If parameters are to be considered non-null unless explicitly stated otherwise.We'll want the 'Name' field to be submitted in the format 'Firstname Lastname', and to only contain letters and a space (NB in real world scenarios, you might need to take account of other locales – this example has been kept simple deliberately).We can achieve this by adding a pattern attribute to the 'Name' field, setting it's value to the regular expression we want the data to be compared against: The text in the title attribute is then appended to the built-in validation message: Note that some screen reader / browser combinations might lead to the title attribute being read out in addition to the aria-describedby text, so watch out for this e.g.I found that using NVDA with IE10 caused the title attribute and the aria-describedby element's text to be read out, but using NVDA with Chrome and Firefox didn’t exhibit this behaviour. Later on we’ll revisit this and show you one solution using CSS3.