{"id":28031,"date":"2016-09-19T23:01:43","date_gmt":"2016-09-19T15:01:43","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=28031"},"modified":"2022-07-15T16:58:42","modified_gmt":"2022-07-15T08:58:42","slug":"html5-constraint-validation","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/","title":{"rendered":"Intro Into HTML5 Constraint Validation"},"content":{"rendered":"<p>Interactive websites and applications can\u2019t be imagined without forms that enable us to <strong>connect with our users<\/strong>, and to <strong>obtain the data<\/strong> we need in order to secure smooth transactions with them. We do need <strong>valid user input<\/strong>, however we need to acquire it in a way that <strong>doesn\u2019t frustrate<\/strong> our users too much.<\/p>\n<p>While we can improve the usability of our forms with smartly chosen UX design patterns, HTML5 also has a native mechanism for <a href=\"https:\/\/html.spec.whatwg.org\/multipage\/forms.html#constraints\" target=\"_blank\" rel=\"noopener\">constraint validation<\/a> that allow us to <strong>catch input errors right in the front-end<\/strong>.<\/p>\n<p>In this post, we\u2019ll focus on <strong>browser-supplied constraint validation<\/strong>, and look into how frontend developers can <strong>secure valid user input using HTML5<\/strong>.<\/p>\n<p class=\"recommended_top\">\n\t\t\t\t\t<strong>Read Also:<\/strong>\u00a0\n\t\t\t\t\t<a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/form-ux-design-tips\/\">4 Form Design UX Tips You Should Know (With Examples)<\/a>\n\t\t\t\t<\/p>\n<h2>Why We Need Front-End Input Validation<\/h2>\n<p><strong>Input validation<\/strong> has two main goals. The content we get needs to be:<\/p>\n<h3>1. Useful<\/h3>\n<p>We need <strong>usable data we can work with<\/strong>. We need to make people enter <strong>realistic data in the right format<\/strong>. For instance, no one who is alive today was born 200 years ago. Getting data like this  may seem funny at first, but on the long term it\u2019s annoying, and populates our database with useless data.<\/p>\n<h3><strong>2. Secure<\/strong><strong><\/strong><\/h3>\n<p>When referring to security, this means we need to <strong>prevent the injection of malicious content<\/strong> \u2014 whether deliberate or by accident.<\/p>\n<p>Usefulness (getting reasonable data) can be achieved <strong>only on the client side<\/strong>, the backend team can\u2019t help too much with this. To attain <strong>security<\/strong>, <strong>front- and backend developers need to work together<\/strong>.<\/p>\n<p>If frontend developers properly validate input on the client side, the <strong>backend team will have to deal with much less vulnerabilities<\/strong>. Hacking (a site) often entails <strong>submitting extra data<\/strong>, or <strong>data in the wrong format<\/strong>. Developers can fight security holes like these, successfully fight from the front-end.<\/p>\n<p>For instance, <a href=\"https:\/\/phpsecurity.readthedocs.io\/en\/latest\/Input-Validation.html\" target=\"_blank\" rel=\"noopener\">this PHP security guide<\/a> recommends to check everything we can on the client side. They emphasize the importance of frontend input validation by giving many examples, such as:<\/p>\n<blockquote><p>\"Input validation works best with extremely restricted values, e.g. when something must be an integer, or an alphanumeric string, or a HTTP URL.\"<\/p><\/blockquote>\n<p>In frontend input validation, our job is to <strong>impose reasonable constraints<\/strong> on user input. HTML5\u2019s constraint validation feature provides us with the means to do so.<\/p>\n<p class=\"recommended_top\">\n\t\t\t\t\t<strong>Read Also:<\/strong>\u00a0\n\t\t\t\t\t<a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/html5-loginpage\/\">HTML5 Tutorial: Login Page with HTML5 Forms<\/a>\n\t\t\t\t<\/p>\n<h2>HTML5 Constraint Validation<\/h2>\n<p>Before HTML5, frontend developers were limited to <strong>validating user input with JavaScript<\/strong>, which was a tedious and error-prone process. To improve client-side form validation, HTML5 has introduced a <strong><a href=\"https:\/\/html.spec.whatwg.org\/multipage\/forms.html#constraint-validation\" target=\"_blank\" rel=\"noopener\">constraint validation<\/a><\/strong> algorithm that runs <a href=\"https:\/\/caniuse.com\/#search=form%20validation\" target=\"_blank\" rel=\"noopener\">in modern browsers<\/a>, and <strong>checks the validity of the submitted input<\/strong>.<\/p>\n<p>To make the evaluation, the algorithm uses the <strong>validation-related attributes of input elements<\/strong>, such as <code>&lt;input&gt;<\/code>, <code>&lt;textarea&gt;<\/code>, and <code>&lt;select&gt;<\/code>. If you want to know how constraint validation happens step by step in the browser check out <a href=\"https:\/\/html.spec.whatwg.org\/multipage\/forms.html#constraint-validation\" target=\"_blank\" rel=\"noopener\">this WhatWG doc<\/a>.<\/p>\n<p>Thanks to HTML5\u2019s constraint validation feature, we can execute all <strong>standard input validation tasks<\/strong> on the client side <strong>without JavaScript, solely with HTML5<\/strong>.<\/p>\n<p>To perform more complex validation-related tasks, HTML5 provides us with a <strong><a href=\"https:\/\/html.spec.whatwg.org\/multipage\/forms.html#the-constraint-validation-api\" target=\"_blank\" rel=\"noopener\">Constraint Validation JavaScript API<\/a><\/strong> we can use to set up our custom validation scripts.<\/p>\n<h2>Validate with Semantic Input Types<\/h2>\n<p>HTML5 has introduced <strong><a href=\"https:\/\/www.hongkiat.com\/blog\/html-5-semantics\/\">semantic<\/a> input types<\/strong> that \u2014 apart from indicating the meaning of the element for user agents \u2014 can also be used to <strong>validate user input<\/strong> by limiting users to a certain input format.<\/p>\n<p>Besides the input types that have already existed before HTML5 (<code>text<\/code>, <code>password<\/code>, <code>submit<\/code>, <code>reset<\/code>, <code>radio<\/code>, <code>checkbox<\/code>, <code>button<\/code>, <code>hidden<\/code>), we can also use the following <strong>semantic HTML5 input types<\/strong>: <a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.email.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>email<\/code><\/a>,\n<a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.tel.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>tel<\/code><\/a>,\n<a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.url.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>url<\/code><\/a>,\n<a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.number.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>number<\/code><\/a>,\n<a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.time.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>time<\/code><\/a>,\n<a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.date.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>date<\/code><\/a>,\n<a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.datetime.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>datetime<\/code><\/a>,\n<a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.datetime-local.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>datetime-local<\/code><\/a>, <a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.month.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>month<\/code><\/a>,\n<a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.week.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>week<\/code><\/a>, <a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.range.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>range<\/code><\/a>, <a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.search.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>search<\/code><\/a>,\n<a href=\"https:\/\/www.w3.org\/TR\/html-markup\/input.color.html\" target=\"_blank\" rel=\"noopener nofollow\"><code>color<\/code><\/a>.<\/p>\n<p>We can safely use HTML5 input types with older browsers, as they will behave as an <code>&lt;input type=\"text\"&gt;<\/code> field in browsers that don\u2019t support them.<\/p>\n<p class=\"note\">\n<strong>Recommended Reading:<\/strong>\n<a href=\"https:\/\/www.hongkiat.com\/blog\/html5-form-input-type\/\">A Look Into HTML5 Forms Input Types: Date, Color and Range<\/a><\/p>\n<p>Let\u2019s see what happens when the user enters the wrong input type. Say we have created an email input field with the following code:<\/p>\n<pre>\r\n&lt;form name=\"form\" action=\"#\" method=\"post\"&gt;\r\n\t &lt;label for=\"youremail\"&gt;Your Email:&lt;\/label&gt;\r\n\t &lt;input type=\"email\" name=\"email\" id=\"youremail\"&gt;\r\n\t &lt;input type=\"submit\" value=\"Submit\"&gt;\r\n &lt;\/form&gt;\r\n<\/pre>\n<p>When the user types a string that doesn\u2019t use an email format, the constraint validation algorithm <strong>doesn\u2019t submit the form<\/strong>, and <strong>returns an error message<\/strong>:<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/html5-constraint-validation\/email-validation.jpg\" width=\"600\" height=\"240\" alt=\"Email Validation\"><\/figure>\n<p>The same rule applies to other input types as well, for example for <code>type=\"url\"<\/code> users can only submit an input that follows the URL format (starts with a protocol, such as <code>http:\/\/<\/code> or <code>ftp:\/\/<\/code>).<\/p>\n<p>Some input types use a design that <strong>doesn\u2019t even allow users to enter a wrong input format<\/strong>, for example <code>color<\/code> and <code>range<\/code>.<\/p>\n<pre>\r\n&lt;form name=\"form\" action=\"#\" method=\"post\"&gt;\r\n  &lt;label for=\"bgcol\"&gt;Background Color:&lt;\/label&gt;\r\n  &lt;input type=\"color\" name=\"color\" id=\"bgcol\"&gt;\r\n  &lt;input type=\"submit\" value=\"Submit\"&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<p>If we use the <code>color<\/code> input type the user is constrained to either choosing a color from the color picker or staying with the default black. The input field is <strong>constrained by design<\/strong>, therefore it doesn\u2019t leave much chance for user error.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/html5-constraint-validation\/color-validation.jpg\" width=\"600\" height=\"291\" alt=\"Color Validation\"><\/figure>\n<p>When it\u2019s appropriate, it\u2019s worth considering using the <a href=\"https:\/\/www.w3schools.com\/tags\/tag_select.asp\" target=\"_blank\" rel=\"noopener\"><code>&lt;select&gt;<\/code><\/a> HTML tag which works similarly to these constrained-by-design input types; it lets users  choose from a dropdown list.<\/p>\n<pre>\r\n&lt;form name=\"form\" action=\"#\" method=\"post\"&gt;\r\n  &lt;label for=\"favfruit\"&gt;Your Favourite Fruit:&lt;\/label&gt;\r\n  &lt;select name=\"fruit\" id=\"favfruit\"&gt;\r\n    &lt;option value=\"apple\"&gt;Apple&lt;\/option&gt;\r\n    &lt;option value=\"pear\"&gt;Pear&lt;\/option&gt;\r\n    &lt;option value=\"orange\"&gt;Orange&lt;\/option&gt;\r\n    &lt;option value=\"raspberry\"&gt;Raspberry&lt;\/option&gt;\r\n  &lt;\/select&gt;\r\n  &lt;input type=\"submit\" value=\"Submit\"&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/html5-constraint-validation\/option-validation.jpg\" width=\"600\" height=\"347\" alt=\"Option Validation\"><\/figure>\n<h2>Use HTML5\u2019s Validation Attributes<\/h2>\n<p>Using semantic input types sets certain constraints on what users are allowed to submit, but in many cases we want to go a little bit further. This is when the <strong>validation-related attributes<\/strong> of the <code>&lt;input&gt;<\/code> tag can help us out.<\/p>\n<p>Validation-related attributes belong to certain input types (they can\u2019t be used on <em>all<\/em> types) on which they impose further constraints.<\/p>\n<h3>1. <a href=\"https:\/\/www.w3schools.com\/tags\/att_input_required.asp\" target=\"_blank\" rel=\"noopener\"><code>required<\/code><\/a> for getting a valid input by all means<\/h3>\n<p>The <code>required<\/code> attribute is the most well-known HTML validation attribute. It\u2019s a <strong>boolean attribute<\/strong> which means it <strong>doesn\u2019t take any value<\/strong>, we just simply have to place it inside the <code>&lt;input&gt;<\/code> tag if we want to use it:<\/p>\n<pre>\r\n&lt;input type=\"email\" name=\"email\" id=\"youremail\" required&gt;\r\n<\/pre>\n<p>If the user forgets to enter a value into a required input field, the browser <strong>returns an error message<\/strong> that warns them to fill in the field, and they <strong>can\u2019t submit the form<\/strong> until they have provided a valid input. That\u2019s why it\u2019s important to always <strong>mark visually<\/strong> required fields to users.<\/p>\n<p>The <code>required<\/code> attribute can be <strong>used together with the following input types<\/strong>: <code>text<\/code>, <code>search<\/code>, <code>url<\/code>, <code>tel<\/code>, <code>email<\/code>, <code>password<\/code>, <code>date<\/code>, <code>datetime<\/code>, <code>datetime-local<\/code>, <code>month<\/code>, <code>week<\/code>,\n<code>time<\/code>, <code>number<\/code>, <code>checkbox<\/code>, <code>radio<\/code>, <code>file<\/code>, plus with the <code>&lt;textarea&gt;<\/code>and <code>&lt;select&gt;<\/code> HTML tags.<\/p>\n<h3>2. <a href=\"https:\/\/www.w3schools.com\/tags\/att_input_min.asp\" target=\"_blank\" rel=\"noopener\"><code>min<\/code><\/a>, <a href=\"https:\/\/www.w3schools.com\/tags\/att_input_max.asp\" target=\"_blank\" rel=\"noopener\"><code>max<\/code><\/a> and <a href=\"https:\/\/www.w3schools.com\/tags\/att_input_step.asp\" target=\"_blank\" rel=\"noopener\"><code>step<\/code><\/a> for number validation<\/h3>\n<p>The <code>min<\/code>, <code>max<\/code> and <code>step<\/code> attributes enable us to <strong>put constraints on number input fields<\/strong>. They can be used together with the <code>range<\/code>, <code>number<\/code>, <code>date<\/code>, <code>month<\/code>, <code>week<\/code>, <code>datetime<\/code>, <code>datetime-local<\/code>, and <code>time<\/code> input types.<\/p>\n<p>The <code>min<\/code> and <code>max<\/code> attributes provide a great way to easily <strong>exclude unreasonable data<\/strong>. For instance the example below forces users to submit an age between 18 and 120.<\/p>\n<pre>\r\n&lt;form name=\"form\" action=\"#\" method=\"post\"&gt;\r\n  &lt;label for=\"yourage\"&gt;Your Age:&lt;\/label&gt;\r\n  &lt;input type=\"number\" name=\"age\" id=\"yourage\" min=\"18\" max=\"120\"&gt;\r\n  &lt;input type=\"submit\" value=\"Submit\"&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<p>When the constraint validation algorithm bumps into a user input smaller than the <code>min<\/code>, or larger than the <code>max<\/code> value, it prevents it from reaching the backend, and returns an error message.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/html5-constraint-validation\/age-min-max.jpg\" width=\"600\" height=\"273\" alt=\"Age with Min and Max Attributes\"><\/figure>\n<p>The <code>step<\/code> attribute <strong>specifies a numeric interval<\/strong> between the legal values of a numeric input field. For instance, if we want users to choose only from leap years we can add the <code>step=\"4\"<\/code> attribute to the field. In the example below I used the <code>number<\/code> input type, as there\u2019s no <code>type=\"year\"<\/code> in HTML5.<\/p>\n<pre>\r\n&lt;form name=\"form\" action=\"#\" method=\"post\"&gt;\r\n  &lt;label for=\"yourleapyear\"&gt;Your Favourite Leap Year:&lt;\/label&gt;\r\n  &lt;input type=\"number\" name=\"leapyear\" id=\"yourleapyear\"\r\n\t\t\tmin=\"1972\" max=\"2016\" step=\"4\"&gt;\r\n  &lt;input type=\"submit\" value=\"Submit\"&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<p>With the pre-set constraints, users can only choose from leap years between 1972 and 2016 if they use the little up-arrow that comes with the <code>number<\/code> input type. They can also type a value manually into the input field, but in case it doesn\u2019t meet the constraints, the browser will return an error message.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/html5-constraint-validation\/step-validation.gif\" width=\"600\" height=\"240\" alt=\"Step Validation\"><\/figure>\n<h3>3. <a href=\"https:\/\/www.w3schools.com\/tags\/att_input_maxlength.asp\" target=\"_blank\" rel=\"noopener\"><code>maxlength<\/code><\/a> for text length validation<\/h3>\n<p>The <code>maxlength<\/code> attribute makes it possible to <strong>set a maximum character length<\/strong> for textual input fields. It can be used together with the <code>text<\/code>, <code>search<\/code>, <code>url<\/code>, <code>tel<\/code>, <code>email<\/code> and <code>password<\/code> input types, and with the <code>&lt;textarea&gt;<\/code> HTML tag.<\/p>\n<p>The <code>maxlength<\/code> attribute can be an excellent solution for phone number fields that cannot have more than a certain number of characters, or for contact forms where we don\u2019t want users to write more than a certain length.<\/p>\n<p>The code snippet below shows an example for the latter, it constraints user messages to 500 characters.<\/p>\n<pre>\r\n&lt;form name=\"form\" action=\"#\" method=\"post\"&gt;\r\n  &lt;label for=\"yourmsg\"&gt;Message (max 500 characters):&lt;\/label&gt;\r\n  &lt;textarea name=\"msg\" id=\"yourmsg\" cols=\"25\" rows=\"4\"\r\n\t\t\tmaxlength=\"500\"&gt;&lt;\/textarea&gt;\r\n  &lt;input type=\"submit\" value=\"Submit\"&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<p>The <code>maxlength<\/code> attribute <strong>doesn\u2019t return an error message<\/strong>, but the browser simply doesn\u2019t let users type more than the specified character number. That\u2019s why it\u2019s crucial to <strong>inform users about the constraint<\/strong>, otherwise they won\u2019t understand why they can\u2019t go on with the typing.<\/p>\n<h3>4. <a href=\"https:\/\/www.w3schools.com\/tags\/att_input_pattern.asp\" target=\"_blank\" rel=\"noopener\"><code>pattern<\/code><\/a> for Regex validation<\/h3>\n<p>The <code>pattern<\/code> attribute allow us to <strong>use <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Regular_Expressions\" target=\"_blank\" rel=\"noopener\">Regular Expressions<\/a><\/strong> in our input validation process. A regular expression is a <strong>pre-defined set of characters<\/strong> that form a certain pattern. We can use it either to search strings that follow the pattern, or to enforce a certain format defined by the pattern.<\/p>\n<p>With the <code>pattern<\/code> attribute we can do the latter \u2014 constrain users to submit their input in a format that <strong>matches the given regular expression<\/strong>.<\/p>\n<p class=\"note\">\n<strong>Recommended Reading:<\/strong>\n<a href=\"https:\/\/www.hongkiat.com\/blog\/getting-started-with-regex\/\">Beginner\u2019s Guide to Regular Expression (Regex)\n<\/a><\/p>\n<p>The <code>pattern<\/code> attribute has many use cases, but it can be especially useful when we want to <strong>validate a password field<\/strong>.<\/p>\n<p>The example below requires users to enter a password that\u2019s minimum 8 characters long, and contains at least one letter and one number (<a href=\"https:\/\/stackoverflow.com\/\/questions\/19605150\/regex-for-password-must-be-contain-at-least-8-characters-least-1-number-and-bot#answer-21456918\" target=\"_blank\" rel=\"noopener nofollow\">source of the regex I used<\/a>).\n<\/p>\n<pre>\r\n&lt;form name=\"form\" action=\"#\" method=\"post\"&gt;\r\n  &lt;label for=\"yourpw\"&gt;* Your Password:&lt;\/label&gt;\r\n  &lt;input type=\"password\" name=\"pw\" id=\"yourpw\" required\r\n\t\t\t\tpattern=\"^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$\"&gt;\r\n  &lt;input type=\"submit\" value=\"Submit\"&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<h2>A few more things<\/h2>\n<p>In this article, we had a look at how to make use of the <strong>browser-supplied form validation<\/strong> provided by HTML5\u2019s native constraint validation algorithm. For creating our custom validation scripts, we need to use the <a href=\"https:\/\/www.w3schools.com\/js\/js_validation_api.asp\" target=\"_blank\" rel=\"noopener\">Constraint Validation API<\/a> that can be the next step in refining form validation skills.<\/p>\n<p>HTML5 forms are <a href=\"https:\/\/www.w3.org\/WAI\/tutorials\/forms\/validation\/\" target=\"_blank\" rel=\"noopener nofollow\">accessible by assistive technologies<\/a>, so we don\u2019t necessarily have to use the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Accessibility\/ARIA\/ARIA_Techniques\/Using_the_aria-required_attribute\" target=\"_blank\" rel=\"noopener nofollow\"><code>aria-required<\/code><\/a> <a href=\"https:\/\/www.hongkiat.com\/blog\/aria-web-standards\/\">ARIA attribute<\/a> to mark required input fields for screen readers. However it  can still be useful to add accessibility support for older browsers. It\u2019s also possible to <strong>opt out of constraint validation<\/strong> by adding the <a href=\"https:\/\/www.w3schools.com\/tags\/att_form_novalidate.asp\" target=\"_blank\" rel=\"noopener\"><code>novalidate<\/code><\/a> boolean attribute to the <code>&lt;form&gt;<\/code> element.<\/p>","protected":false},"excerpt":{"rendered":"<p>Interactive websites and applications can\u2019t be imagined without forms that enable us to connect with our users, and to obtain the data we need in order to secure smooth transactions with them. We do need valid user input, however we need to acquire it in a way that doesn\u2019t frustrate our users too much. While&hellip;<\/p>\n","protected":false},"author":146,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[352],"tags":[1312,506],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.6) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Intro Into HTML5 Constraint Validation - Hongkiat<\/title>\n<meta name=\"description\" content=\"Interactive websites and applications can&#039;t be imagined without forms that enable us to connect with our users, and to obtain the data we need in order to\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Intro Into HTML5 Constraint Validation\" \/>\n<meta property=\"og:description\" content=\"Interactive websites and applications can&#039;t be imagined without forms that enable us to connect with our users, and to obtain the data we need in order to\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2016-09-19T15:01:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-15T08:58:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/html5-constraint-validation\/email-validation.jpg\" \/>\n<meta name=\"author\" content=\"Anna Monus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anna Monus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/\"},\"author\":{\"name\":\"Anna Monus\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/a601053a0ab457901e00cdc83bd5359e\"},\"headline\":\"Intro Into HTML5 Constraint Validation\",\"datePublished\":\"2016-09-19T15:01:43+00:00\",\"dateModified\":\"2022-07-15T08:58:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/\"},\"wordCount\":1539,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/html5-constraint-validation\\\/email-validation.jpg\",\"keywords\":[\"Forms\",\"HTML\"],\"articleSection\":[\"Web Design\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/\",\"name\":\"Intro Into HTML5 Constraint Validation - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/html5-constraint-validation\\\/email-validation.jpg\",\"datePublished\":\"2016-09-19T15:01:43+00:00\",\"dateModified\":\"2022-07-15T08:58:42+00:00\",\"description\":\"Interactive websites and applications can't be imagined without forms that enable us to connect with our users, and to obtain the data we need in order to\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/html5-constraint-validation\\\/email-validation.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/html5-constraint-validation\\\/email-validation.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html5-constraint-validation\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intro Into HTML5 Constraint Validation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/a601053a0ab457901e00cdc83bd5359e\",\"name\":\"Anna Monus\",\"description\":\"Anna is Technical Editor and Writer for Hongkiat.com. She mainly covers front-end frameworks, web standards, accessibility, WordPress development, and UX design.\",\"sameAs\":[\"https:\\\/\\\/www.annalytic.com\\\/\"],\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/anna_monus\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Intro Into HTML5 Constraint Validation - Hongkiat","description":"Interactive websites and applications can't be imagined without forms that enable us to connect with our users, and to obtain the data we need in order to","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/","og_locale":"en_US","og_type":"article","og_title":"Intro Into HTML5 Constraint Validation","og_description":"Interactive websites and applications can't be imagined without forms that enable us to connect with our users, and to obtain the data we need in order to","og_url":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2016-09-19T15:01:43+00:00","article_modified_time":"2022-07-15T08:58:42+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/html5-constraint-validation\/email-validation.jpg","type":"","width":"","height":""}],"author":"Anna Monus","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Anna Monus","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/"},"author":{"name":"Anna Monus","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/a601053a0ab457901e00cdc83bd5359e"},"headline":"Intro Into HTML5 Constraint Validation","datePublished":"2016-09-19T15:01:43+00:00","dateModified":"2022-07-15T08:58:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/"},"wordCount":1539,"commentCount":3,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/html5-constraint-validation\/email-validation.jpg","keywords":["Forms","HTML"],"articleSection":["Web Design"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/","url":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/","name":"Intro Into HTML5 Constraint Validation - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/html5-constraint-validation\/email-validation.jpg","datePublished":"2016-09-19T15:01:43+00:00","dateModified":"2022-07-15T08:58:42+00:00","description":"Interactive websites and applications can't be imagined without forms that enable us to connect with our users, and to obtain the data we need in order to","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/html5-constraint-validation\/email-validation.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/html5-constraint-validation\/email-validation.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/html5-constraint-validation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Intro Into HTML5 Constraint Validation"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/a601053a0ab457901e00cdc83bd5359e","name":"Anna Monus","description":"Anna is Technical Editor and Writer for Hongkiat.com. She mainly covers front-end frameworks, web standards, accessibility, WordPress development, and UX design.","sameAs":["https:\/\/www.annalytic.com\/"],"url":"https:\/\/www.hongkiat.com\/blog\/author\/anna_monus\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-7i7","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/28031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/146"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=28031"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/28031\/revisions"}],"predecessor-version":[{"id":60444,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/28031\/revisions\/60444"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=28031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=28031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=28031"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=28031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}