How to use the pattern attribute?

2

I've already reviewed the internet, even in W3school, and I can only see them reporting the "ready" code. With keys, questions, bars, dollar sign and several other parameters.

But I did not find anything explaining how to use these parameters, what are they for?

    
asked by anonymous 04.02.2017 / 18:07

1 answer

6

The pattern attribute in HTML 5 forms allow that form to be validated through a Regular Expression

a>.

Regular expressions are a way of validating textual expressions that follow some kind of pattern and by applying this expression within the pattern parameter HTML will ensure that this pattern is satisfied.

Example:

You have a form that needs to receive the license plate of a car. This type of information in Brazil follows a certain pattern: 3 letters from A to Z, followed by a dash and finally 4 numbers from 0 to 9. You can express this in the following form:

<form action="#">
  <label for="placa">Placa: </label>
  <input type="text" name="website" pattern="[A-Za-z]{3}-[0-9]{4}">
  <input type="submit">
</form>

Before using the pattern attribute, you need an understanding of how to work with regular expressions. Here are some articles that can help you:

04.02.2017 / 18:35