How we can validate a form easily using HTML CSS and JS or jQuery? Solution: See this jQuery Form Validation With Custom Select Box, Validate Form.
Previously I have shared a few validation programs, but this one has a flat design and a custom select box. Basically, Form validation is for checking entered data locally before sending it to the server. If the data is incorrect, then the user will see a message to fix the problems. JavaScript provides a way to validate form’s input data on the user’s computer before sending it to the webserver. And this jQuery the work becomes easier.
Today you will learn to Validate Form using jQuery. Basically, there is a form with 4 inputs and a button. The first input for the name, second for the password, third for email, and the fourth one for select options, after these all there is a button to submit. There are many conditions like the name should be 6 characters, the password should be 6 characters, need a valid email, and an option must be selected. If you avoid any condition then you will see a specific message about fix that.
So, Today I am sharing jQuery Form Validation With Custom Select Box. There I have used jQuery for validation and HTML CSS for creating layout and styling. This is a registration form with basic fields, and it has a flat design. You can use it on your website after backend integration. This form has few input fields, if you can add more then you have to validate those.
If you are thinking now how this form validation actually is, then see the preview given below.
Preview Of Validate Form Flat Design
See this video preview to getting an idea of how this flat form looks like.
Now you can see this program visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
- Customizable Scrolling Navbar
- jQuery Filter Example
- Flat Search Box with Loading Animation
- Full Screen Modal Nav
jQuery Form Validation With Custom Select Box Source Code
Before sharing source code, let’s talk about it. First I have created the main div with class name container and placed all items inside it. Inside the container div, I have placed a text input for the name, an email input for mail id, a password input for the password, and a select tag with options. After that, create a submit button and another div for error/success messages. In the button tag, I have called the JS function which validating data using onclick="return functionName();" method.
Now using CSS I have placed all the items in the right place, as you can see in the preview. With CSS first I gave basic values like size, position, margin, padding, etc to the elements. There I have used transform and z-index commands to create a custom select box, there I have selected options separately using :nth-child() command. Also, I gave color values to the messages for error and success.
jQuery handling here whole validation function in the program, also it handling the custom dropdown select options. There I have used for loop for {} command and put other commands inside it to create the dropdown function. After that, used if{} else{} commands to validate and show messages according it.
Left all other things you will understand after getting the codes, I can’t explain all in writing. For creating this program you have to create 3 files. First file for HTML, second for CSS, and the third file for JavaScript. Follow the steps to creating this program without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<!DOCTYPE html> <!--Code By Webdevtrick ( https://webdevtrick.com )--> <html lang="en" > <head> <meta charset="UTF-8"> <title>Form Validation With Custom Select | Webdevtrick.com</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <form name="registerForm" class="formBlock"> <h2 class="title">Form validaion</h2> <div> <label>Name:</label> <input type="text" id="name" placeholder="Name" autocomplete="off"/> </div> <div> <label>Password:</label> <input type="password" id="password" placeholder="Password"/> </div> <div> <label>Email:</label> <input type="email" id="email" placeholder="Email" autocomplete="off"/> </div> <div> <label>Country:</label> <select name="" id="cusSelectbox"> <option value="select">select</option> <option value="india">india</option> <option value="nepal">nepal</option> <option value="bangladesh">bangladesh</option> <option value="sri lanka">sri lanka</option> <option value="pakistan">pakistan</option> </select> </div> <div class="btnBlock"> <button type="submit" onclick="return validateRegister();" class="btn">Submit</button> </div> <div class="errorBlock"> <p id="regisErr"></p> </div> </form> </div> </div> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ @import url('https://fonts.googleapis.com/css?family=Roboto&display=swap'); * { margin: 0; padding: 0; outline: none; } *, *:after, *:before { box-sizing: border-box; } ::-webkit-scrollbar { display: none; } body { background-color: #fff; color: #333; font-family: 'Roboto', ; font-size: 16px; } .container { text-align: center; margin-top: 10%; } .container:last-child { border-top: 2px dashed #333; } .selected-item { margin: 20px 0; } .cusSelBlock { height: 50px; min-width: 250px; } #cusSelectbox { height: 100%; width: 100%; } .s-hidden { visibility: hidden; } .cusSelBlock { display: inline-block; position: relative; -webkit-perspective: 800px; perspective: 800px; } .selectLabel { position: absolute; left: 0; top: 0; z-index: 9999; background-color: #fff; border: 1px solid #333; box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1); color: #333; cursor: pointer; display: block; height: 100%; width: 100%; letter-spacing: 2px; line-height: 50px; padding: 0 50px 0 20px; text-align: left; -webkit-transform-style: preserve-3d; transform-style: preserve-3d; -webkit-transform-origin: 50% 0%; transform-origin: 50% 0%; -webkit-transition: -webkit-transform 300ms; transition: -webkit-transform 300ms; transition: transform 300ms; transition: transform 300ms, -webkit-transform 300ms; -webkit-backface-visibility: hidden; -webkit-touch-callout: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } .selectLabel:after { content: '\25BC'; border-left: 1px solid #333; color: #333; font-size: 12px; line-height: 17px; padding: 10px; text-align: center; position: absolute; right: 0px; top: 15%; height: 70%; width: 50px; } .selectLabel:active { -webkit-transform: rotateX(30deg); transform: rotateX(30deg); } .selectLabel:active:after { content: '\25B2'; } .selectLabel.active:after { content: '\25B2'; } .options { position: absolute; top: 50px; height: 1px; width: 100%; } .options li { background-color: #ffffff; border-left: 1px solid #333; border-right: 1px solid #333; border-bottom: 1px solid #333; cursor: pointer; display: block; line-height: 50px; list-style: none; opacity: 1; padding: 0 50px 0 20px; text-align: left; -webkit-transition: -webkit-transform 300ms ease; transition: -webkit-transform 300ms ease; transition: transform 300ms ease; transition: transform 300ms ease, -webkit-transform 300ms ease; position: absolute; top: -50px; left: 0; z-index: 0; height: 50px; width: 100%; } .options li:hover, .options li.active { background-color: #333; color: #fff; } .options li:nth-child(1) { -webkit-transform: translateY(2px); transform: translateY(2px); z-index: 6; } .options li:nth-child(2) { -webkit-transform: translateY(4px); transform: translateY(4px); z-index: 5; } .options li:nth-child(3) { z-index: 4; } .options li:nth-child(4) { z-index: 3; } .options li:nth-child(5) { z-index: 2; } .options li:nth-child(6) { z-index: 1; } .title { font-size: 20px; font-weight: normal; margin-bottom: 20px; text-align: center; text-transform: capitalize; } .formBlock { color: #111; margin: 0 auto; padding: 20px 0; text-align: left; width: 320px; } .formBlock > div { margin-bottom: 15px; } .formBlock > div.btnBlock { margin: 30px 0 0; } .formBlock label { display: block; font-size: 17px; margin-bottom: 5px; } .selectLabel { font-size: 17px; text-transform: capitalize; } .selectLabel li { font-size: 17px; } .selectLabel:focus { border-color: blue !important; } .options li { text-transform: capitalize; } input { border: 1px solid #333; font-size: 17px; padding: 0 50px 0 20px; height: 50px; width: 100%; } input:focus { border-color: blue; } select { min-width: 320px; } .btn { border: 1px solid #333; border-radius: 0; background-color: #fff; color: #333; font-size: 17px; padding: 12px 20px; } .btn:hover { background-color: #222; color: #fff; } .errorBlock { margin: 20px 0 0; text-align: center; } .error, .success { font-size: 14px; } .error { border: 1px dashed red; color: red; padding: 5px 10px; } .success { border: 1px dashed green; color: green; padding: 5px 10px; } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
// Code By Webdevtrick ( https://webdevtrick.com ) $(function () { var defaultselectbox = $('#cusSelectbox'); var numOfOptions = $('#cusSelectbox').children('option').length; // hide select tag defaultselectbox.addClass('s-hidden'); // wrapping default selectbox into custom select block defaultselectbox.wrap('<div class="cusSelBlock"></div>'); // creating custom select div defaultselectbox.after('<div class="selectLabel"></div>'); // getting default select box selected value $('.selectLabel').text(defaultselectbox.children('option').eq(0).text()); // appending options to custom un-ordered list tag var cusList = $('<ul/>', { 'class': 'options'} ).insertAfter($('.selectLabel')); // generating custom list items for(var i=0; i< numOfOptions; i++) { $('<li/>', { text: defaultselectbox.children('option').eq(i).text(), rel: defaultselectbox.children('option').eq(i).val() }).appendTo(cusList); } // open-list and close-list items functions function openList() { for(var i=0; i< numOfOptions; i++) { $('.options').children('li').eq(i).attr('tabindex', i).css( 'transform', 'translateY('+(i*100+100)+'%)').css( 'transition-delay', i*30+'ms'); } } function closeList() { for(var i=0; i< numOfOptions; i++) { $('.options').children('li').eq(i).css( 'transform', 'translateY('+i*0+'px)').css('transition-delay', i*0+'ms'); } $('.options').children('li').eq(1).css('transform', 'translateY('+2+'px)'); $('.options').children('li').eq(2).css('transform', 'translateY('+4+'px)'); } // click event functions $('.selectLabel').click(function () { $(this).toggleClass('active'); if( $(this).hasClass('active') ) { openList(); focusItems(); } else { closeList(); } }); $(".options li").on('keypress click', function(e) { e.preventDefault(); $('.options li').siblings().removeClass(); closeList(); $('.selectLabel').removeClass('active'); $('.selectLabel').text($(this).text()); defaultselectbox.val($(this).text()); $('.selected-item p span').text($('.selectLabel').text()); }); }); function focusItems() { $('.options').on('focus', 'li', function() { $this = $(this); $this.addClass('active').siblings().removeClass(); }).on('keydown', 'li', function(e) { $this = $(this); if (e.keyCode == 40) { $this.next().focus(); return false; } else if (e.keyCode == 38) { $this.prev().focus(); return false; } }).find('li').first().focus(); } // features tab $('.feaTabBtn small').click(function() { $('.feaTabCon').toggleClass('active'); if($('.feaTabCon').hasClass('active')) { $(this).text('hide'); } else { $(this).text('show'); } }); $('.readmoreBlock span').click(function() { $('.readContent').toggleClass('active'); if($('.readContent').hasClass('active')) { $(this).text('lessmore...'); } else { $(this).text('readmore...'); } }); // form validation function validateRegister() { var emaidid, atpos, dotpos, password, username, country; username = $('#name').val(); password = $('#password').val(); emailid = $('#email').val(); atpos = emailid.indexOf('@'); dotpos = emailid.lastIndexOf('.'); if (username == null || username == '') { $('#regisErr').addClass('error').text('* Name do not empty !'); $('#name').focus(); return false; } if(username.length < 6 ) { $('#regisErr').addClass('error').text('* Name should be min 6 char !'); $('#name').focus(); return false; } if(password == null || password == '') { $('#regisErr').addClass('error').text('* Password do not empty!'); $('#password').focus(); return false; } if(password.length < 6 ) { $('#regisErr').addClass('error').text('* Password do not short!'); $('#password').focus(); return false; } if(emailid == null || emailid == '') { $('#regisErr').addClass('error').text('* Email id do not empty!'); $('#email').focus(); return false; } if(atpos < 1 || dotpos < atpos+2 || dotpos+2 >= emailid.length) { $('#regisErr').addClass('error').text('It is not a valid email address!'); $('#email').focus(); return false; } country = $('.selectLabel').text(); if (country == 'select') { $('#regisErr').removeClass('success').addClass('error').text('* Select country !'); $('.selectLabel').focus(); return false; } else if(country != 'select') { $('#regisErr').addClass('success').text('Your country is: '+country); return false; } } |
That’s It. Now you have successfully created jQuery Form Validation With Custom Select Box, Validate Form Program. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.