{"id":1388,"date":"2016-01-26T14:26:45","date_gmt":"2016-01-26T14:26:45","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=1388"},"modified":"2016-01-26T14:26:45","modified_gmt":"2016-01-26T14:26:45","slug":"validate-an-ipv4-address-with-jquery-inputmask-plugin","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/web-development\/html\/validate-an-ipv4-address-with-jquery-inputmask-plugin\/","title":{"rendered":"Validate an IPv4 address with jquery.inputmask plugin"},"content":{"rendered":"<p>In this article, I will try to show you how to make an IPv4 address validation by using <a href=\"https:\/\/github.com\/RobinHerbots\/jquery.inputmask\" target=\"_blank\">jquery.inputmask<\/a> plugin. But first I would like to write some words about this plugin and his general functionality.<\/p>\n<h3>What does this plugin ?<\/h3>\n<p>This plugin provides a way to add a text format to a text field to help the user by ensuring a predefined format for this. The mask shows the general format and gives the user an idea about how they should input the data. You can use this plugin to add a mask for dates, phone numbers, IP addresses, etc. in a specific format. We will use this plugin to make a text field mask look like an IP address (XXX.XXX.XXX.XXX), for this we&#8217;ll use one of the defined aliases (some examples can be found in <i>jquery.inputmask.extensions.js<\/i> file from the plugin) , namely the <code style=\"padding: 0 5px;margin:0 2px !important;color:#c7254e;font-size:11px;background-color:#f9f2f4;border: 1px solid #dcdcdc\">ip<\/code> alias. For more aliases, check this <a href=\"https:\/\/github.com\/RobinHerbots\/jquery.inputmask#aliases\" target=\"_blank\">page<\/a>.<\/p>\n<h3>What is an IP address ?<\/h3>\n<p>Before we start, I would like to introduce some basic concepts. One of the main functions of Internet Protocol (<code style=\"padding: 0 5px;margin:0 2px !important;color:#c7254e;font-size:11px;background-color:#f9f2f4;border: 1px solid #dcdcdc\">IP<\/code>) is to give logical addressing for hosts. An <strong>IP address<\/strong> provides a hierarchical structure to both uniquely identify a <i>host<\/i>, and what <i>network<\/i> that host exists on. This is most often represented in <strong>decimal<\/strong>, in the following format : <code style=\"padding: 0 5px;margin:0 2px !important;color:#c7254e;font-size:11px;background-color:#f9f2f4;border: 1px solid #dcdcdc\">158.80.164.3<\/code> and is comprised of four <strong>octets<\/strong>, separated by periods.<\/p>\n<h3>What is IPv4 (Internet Protocol Version 4)?<\/h3>\n<p>IPv4 (Internet Protocol Version 4) is the fourth revision of the Internet Protocol (IP) used to identify devices on a network through an addressing system. The Internet Protocol is designed for use in interconnected systems of packet-switched computer communication networks. IPv4 is the most widely deployed Internet Protocol used to connect devices to the Internet.<\/p>\n<h3>Let&#8217;s see how to do the validtion<\/h3>\n<p>For demo purpose, we will use a HTML file, a JavaScript file and the <a href=\"https:\/\/github.com\/RobinHerbots\/jquery.inputmask\" target=\"_blank\">jquery.inputmask<\/a> plugin. Below are some print examples with validation function behavior :<\/p>\n<p class=\"aligncenter\">Placeholder mask<a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/placeholder_mask.png\" rel=\"attachment wp-att-1438\" target=\"_blank\"><img loading=\"lazy\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/placeholder_mask.png\" alt=\"placeholder_mask\" width=\"550\" height=\"60\" class=\"alignnone size-full wp-image-1438\" srcset=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/placeholder_mask.png 550w, https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/placeholder_mask-300x33.png 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p class=\"aligncenter\">Incorrect IP<a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/invalid.jpg\" rel=\"attachment wp-att-1441\" target=\"_blank\"><img loading=\"lazy\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/invalid.jpg\" alt=\"invalid\" width=\"550\" height=\"32\" class=\"alignnone size-full wp-image-1441\" srcset=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/invalid.jpg 550w, https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/invalid-300x17.jpg 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p class=\"aligncenter\">Correct IP<a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/correct.jpg\" rel=\"attachment wp-att-1443\" target=\"_blank\"><img loading=\"lazy\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/correct.jpg\" alt=\"correct\" width=\"550\" height=\"34\" class=\"alignnone size-full wp-image-1443\" srcset=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/correct.jpg 550w, https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/correct-300x19.jpg 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p>The <strong>HTML<\/strong> file must contain the <strong>CSS<\/strong> file, the <strong>jQuery<\/strong> libraries and the custom script file.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!-- stylesheet for validation --&gt;\r\n&lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;css\/validation.css&quot;&gt;\r\n \r\n&lt;!-- jquery library --&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;https:\/\/code.jquery.com\/jquery-2.1.4.min.js&quot;&gt;&lt;\/script&gt;\r\n\r\n&lt;!-- plugin file --&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;https:\/\/rawgit.com\/RobinHerbots\/jquery.inputmask\/3.x\/dist\/jquery.inputmask.bundle.js&quot;&gt;&lt;\/script&gt;\r\n\r\n&lt;!-- script for validation --&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;js\/validation.js&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>After being included the necessary js libraries and the CSS files, we will need to add a <code>&lt;input type=&quot;text&quot;\/&gt;<\/code> which defines a one-line text field, like this below :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;input type=&quot;text&quot; class=&quot;form-input&quot; id=&quot;ipv4&quot; name=&quot;ipv4&quot; \/&gt;\r\n<\/pre>\n<p>The custom JavaScript file (<code style=\"padding: 0 5px;margin: 0 2px !important;color:#c7254e;font-size:11px;background-color:#f9f2f4;border: 1px solid #dcdcdc\">validation.js<\/code>) has three sections:<\/p>\n<p>1. The code to create an input mask. For more explanation about the masking definitions you can visit the plugin <a href=\"https:\/\/github.com\/RobinHerbots\/jquery.inputmask#readme\" target=\"_blank\">documentation page<\/a>.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/* selector for input *\/\r\nvar ipv4_address = $('#ipv4');\r\n\/*\r\ncreate an input mask\r\nfor IP addresses we will use an alias defined\r\nin the core functions of the plugin, to build a mask\r\n*\/\r\nipv4_address.inputmask({\r\n    alias: &quot;ip&quot;,\r\n    &quot;placeholder&quot;: &quot;_&quot;\r\n});\r\n<\/pre>\n<p>2. The function code for validating the value entered into the field.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction validateIPAddress(inputID) {\r\n    \/* trigger the jquery focusout event *\/\r\n    $(inputID).on(&quot;focusout&quot;, function() {\r\n        \/*\r\n        save the value of $(this) in the $this variable\r\n        rather than writing $(this) everywhere\r\n        *\/\r\n        var $this = $(this);\r\n        \/* save the entered string from input *\/\r\n        var entered_ip = $this.val();\r\n        \/* check to see if that something was introduced into the field *\/\r\n        if (entered_ip) {\r\n            \/*\r\n            split the resulted string by &quot;.&quot; character\r\n            through the jquery split method and\r\n            save the splited values into an array\r\n            *\/\r\n            var ip_fields_array = entered_ip.split(&quot;.&quot;);\r\n            \/* ... *\/\r\n            \/* store the length of the array *\/\r\n            var array_length = ip_fields_array.length;\r\n            \/*regular expression that starts with 0 and continue with any digit *\/\r\n            var regex = \/^0([0-9])+\/;\r\n            \/* make an iteration through every item from array *\/\r\n            for (var i = 0; i &lt; array_length; i++) {\r\n                var ip_field = ip_fields_array[i];\r\n                \/*\r\n                remove the &quot;_&quot; char and then test the regex created\r\n                true -&gt; if we encounter a string that starts with 0\r\n                and continue with any character (other than white space)\r\n                false -&gt; otherwise\r\n                *\/\r\n                var regex_result = regex.test(ip_field.replace(\/_\/g, &quot;&quot;));\r\n                \/*\r\n                show the error message if one ip field is not entered (&quot;___&quot;)\r\n                and if the regex created is true\r\n                *\/\r\n                if (ip_field == &quot;___&quot; || regex_result == true) {\r\n                    \/* code for error message *\/\r\n                } else {\r\n                    \/* code for success message *\/\r\n                }\r\n            }\r\n        }\r\n    });\r\n    \/* ... *\/\r\n}\r\n<\/pre>\n<p>3. Calling the validation function for the id of text field given as a parameter.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/* call the function *\/\r\nvalidateIPAddress(&quot;#ipv4&quot;);\r\n<\/pre>\n<p>To view the function behavior you can download the archive with the entire code from <a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2016\/01\/ip_validation.zip\">here<\/a>.<\/p>\n<p>Thanks for reading this article. If you have any susuggestions, improvements or questions, please tell me through the comment form below.<\/p>\n<div id=\"share-and-beer-container\">\t<div id=\"buy_me_a_beer_div\" class=\"single beer\">\n\t \t\n\t\t<div class=\"buy-beer\" onclick=\"document.getElementById('buy_me_a_beer_form').submit();\">\n\t\t\t<form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" id=\"buy_me_a_beer_form\" method=\"post\">\n\t\t\t\t<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" \/>\n\t\t\t\t<input type=\"hidden\" name=\"business\" value=\"info@directaccess.ro\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_name\" value=\"A Beer For Validate an IPv4 address with jquery.inputmask plugin\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_number\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"return\" value=\"https:\/\/www.osd.net\/blog\/web-development\/html\/validate-an-ipv4-address-with-jquery-inputmask-plugin\/\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"amount\" value=\"5\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"undefined_quantity\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"currency_code\" value=\"USD\" \/>  \n\t\t\t<\/form>\n\t\t\t<p class=\"buy-beer-text\">If you liked this post <br \/> you can <strong>buy me a beer<\/strong><\/p>\n\t\t<\/div>\n\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>In this article, I will try to show you how to make an IPv4 address validation by using jquery.inputmask plugin. But first I would like to write some words about this plugin and his general functionality. What does this plugin ? This plugin provides a way to add a text format to a text field &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/web-development\/html\/validate-an-ipv4-address-with-jquery-inputmask-plugin\/\">Read more<\/a><\/div>\n<div class=\"like-excerpt\"><div\n        class=\"fb-like\"\n        data-href=\"https:\/\/www.osd.net\/blog\/web-development\/html\/validate-an-ipv4-address-with-jquery-inputmask-plugin\/\"\n        data-layout=\"button_count\"\n        data-action=\"like\"\n        data-show-faces=\"false\"\n        data-share=\"false\">\n        <\/div><\/div>","protected":false},"author":1001003,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,18],"tags":[55,86],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/1388"}],"collection":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/users\/1001003"}],"replies":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/comments?post=1388"}],"version-history":[{"count":12,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/1388\/revisions"}],"predecessor-version":[{"id":1400,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/1388\/revisions\/1400"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=1388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=1388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=1388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}