Home Blog Blog Detail

How to Use ‘Pattern’ Attribute in HTML5 Form Validation?

Online business is on elevation. You need a website for it wherein you can display your storefront or virtual office. And to enable smooth interaction, your website should have active & impeccable backend.  Alongside, your web presence would resemble ineffective if it lacks style & attraction.  Despite running a valuable web business, your web presence will be neglected. So, focus on to making its frontend eye-catching.

This blog contains helpful tips regarding web designing of form-validation in HTML5. It basically highlights ‘pattern’ attribute. Go through these ‘how-to-use’ tips for delivering flawless user experience.     

Let’s move on….

What’s ‘form validation’?

‘Validation’ determines the action of checking accuracy. Or you can say, it’s all about proving validity.  Form is an ideal source of communication virtually with the user. Let’s say, you want to create a gmail account. How can you do it? Obviously, you need to fill the form. By providing credentials in the form, your account would be ready to use.

‘Form validation’ identifies if the input value is in correct or incorrect format.  Have you noticed an error message that pops up onsubmit or onkeypress or onblur? 

  • Onsubmit: It identifies the validity evaluation when the user submits the form.  
  • Onkeypress: It evaluates accuracy of input while typing or entering any value to a field. 
  • Onblur: It verifies the input in the form when the user checks out the form.

Any of these validations returns the message in the pop-up that the input in the pointed field requires correction. Mostly, it figures out correction in the format. For example, the input field of the username mandates entering ‘alphabets’ & ‘upper case’ or ‘lowercase’. 

When this given validation criterion does not match, conflict arises. Consequently, the error message pops up while interrupting submission.

Why do web designers use ‘pattern’ attribute?

The aforementioned example displays the default or standard validation of the username. But what if we want to push validation boundary from standard to customize?

Meantime, the pattern attribute comes forth. It is the greatest tool for web design services provider.  It adds an eligibility of customizing validations to be passed through the input element. Let’s say, I want to include alphanumeric value to the username, like Marc1 in a form while designing web to deliver excellent retail experience till long. To replace the standard or default pattern, I need to use pattern attribute.

How to use ‘pattern’ attribute for HTML5 form validation?

When a web designer sets his own rule to verify the input, he has to deploy regular expressions (RgEx). It is basically used in input element of HTML5 codes.

Let’s continue with the foretold example. I need to set my own standard of username. By restricting its length to just 10 characters, I want to set the limit & the valid name as pattern. Have a look how I would do in codes:

As you run this code, the rendered result will display in the error message near the form’s field. It would read as “Please match the requested format”.

It represents that the set pattern is working fine.