Here we will see now how to set the validation for all form fields without any validation code means Validation without validation code. Yes it is called as a built in validation, It is available in html5. Now we will see one by one with live example. Below i list out some of the fields with validation please check out those examples. Mostly here we will use the patterns to validate the fields with the specific format and conditions.
Html5: Required
required is one of the attribute used to validate the input field should not be blank(empty), It should contain some value. If you are not entering any value to the particular input required field it displays the error messages like 'Please fill out this field'.
Code:
<form onsubmit="alert('Submitted.');return false;" action="" >
Name: <input type="text" name="name" required>
<input type="submit" value="submit">
</form>
Live Example:
Html5 Name Field Validation:
Mostly the name or username condition is should be the character small letter or capital letter. So i validate this name field like to enter only the character no special character and numbers are allowed here. Some of the usernames contain number so you can modify the pattern here just add 0-9 in pattern. Almost this is the Html5 name validation.
Name validation code:
<form onsubmit="alert('Submitted.');return false;">
<input type="text" required="" pattern="[a-zA-Z]+" value="" >
<input type="submit" value="submit">
</form>
Live Demo
Html5 Email Field Validation:
Html5 Email validation is one of the important validation it is pre defined validation you just set the type="email" so the email validation done. No need to enter any email patterns here in Html5. check out the below live example.
Email validation code:
<form onsubmit="alert('Submitted.');return false;">
<input type="email" required="" >
<input type="submit" value="submit">
</form>
Live Demo
Html5 Mobile Number Field Validation:
Most of the Html5 Mobile validation is should be number and digit restriction. Here i will validate the mobile number should be only numeric number and minlength=10 and maxlength=12. I think this is validation is enough to get real mobile number, If you want to add some condition please update the html5 pattern. see the live demo below to validate
Mobile validation code:
<form onsubmit="alert('Submitted.');return false;">
<input type="text" minlength="10" maxlength="12" pattern="[0-9]+" name="mobile" required="">
<input type="submit" value="submit">
</form>
Live Demo
Html5 Password Field Validation:
Html5 password validation is fully customization it is not a unique some of the people don't have any restriction for this password field. This password validation is totally different from different websites. here have a restriction at least one capital letter and at least one small letter at least one number and it should be 10 digit longer.
If you want one special character and one number and caps letter and small letter AND min 8 character means you can use this pattern to validation : (?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$
Password validation code:
<form onsubmit="alert('Submitted.');return false;">
<input type="password" minlength="10" pattern="^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$" name="password" required="">
<input type="submit" value="submit">
</form>
Live Demo
Html5 Date Field Validation:
Date validation in html5 is one of simple thing but it haves the different format Here i will used this format DD.MM.YYYY . If you want different format means you can use the different html5 pattern. check the live example demo below for date field.
For example i given some pattern here
YYYY-MM-DD : [0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])
MM/DD/YYYY : (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d
Date validation code:
<form onsubmit="alert('Submitted.');return false;">
<input type="text" required="" pattern="(0[1-9]|1[0-9]|2[0-9]|3[01]).(0[1-9]|1[012]).[0-9]{4}" placeholder="Format: DD.MM.YYYY">
<input type="submit" value="submit">
</form>
Live Demo
Html5 Time Field Validation:
Here is the time validation using html5 inbuilt validation. You can use this time pattern for time validation. checkout the live example demo below to check the how the time validation working. If you want different time format please update the pattern .
Time validation code:
<form onsubmit="alert('Submitted.');return false;">
<input type="text" required="" pattern="(0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9]){2}" placeholder="Format: HH:MM:SS">
<input type="submit" value="submit">
</form>
Live Demo
Html5 URL Field Validation:
This is url field html5 validation. This is predefined one, No need any of the validation pattern for this field only set the type="url" so it automatically check the validation for url. Check the below live example how the validation works for the url.
url validation code:
<form onsubmit="alert('Submitted.');return false;">
<input type="url" id="url" name="url" required>
<input type="submit" value="submit">
</form>
Live Demo
Html5 Credit card Number Validation:
Credit card number validation in html5 with the help of pattern. This credit card number should be 13 to 16 digit numeric number to validate this using this html5 pattern [0-9]{13,16}. Check out the below example for credit card number validation.
credit card validation code:
<form onsubmit="alert('Submitted.');return false;">
<input type="text" required="" pattern="[0-9]{13,16}" value="" placeholder="Credit card No.">
<input type="submit" value="submit">
</form>
Live Demo
Html5 Indian Postal Code (pin code) Validation:
This is one of the validation to validate the postal code or pin code validation. The Indian postal code contains the 6 digit number and not start with 0. Here i validate the pin code should be 6 digits number and it should not be start with 0. If you want any updation in the postal code validation you can change the html5 pattern according to your requirement. Please refer the demo below
Postal code validation code:
<form onsubmit="alert('Submitted.');return false;">
<input type="text" required="" pattern="^[1-9][0-9]{5}$" value="" placeholder="Postal Code">
<input type="submit" value="submit">
</form>
Live Demo
Html5 Number Validation:
You can also validate numbers. Here is the validation is to accept numbers both decimal and without decimal numbers for example 5.8, 1500, 15,000 like that. check the below live example. you can change the html5 patterns for validation according to your condition.
Numbers with or without decimals:
<form onsubmit="alert('Submitted.');return false;">
<input type="text" required="" pattern="[-+]?[0-9]*[.,]?[0-9]+" value="" placeholder="Format: 7 or 7.9 or 7,9">
<input type="submit" value="submit">
</form>
Live Demo
Html5 Color Code Validation:
You also validate the color code, check the below html5 pattern to validate color code and check the below live example demo. I hope this will be very useful.
Color code validation code:
<form onsubmit="alert('Submitted.');return false;">
<input type="text" required="" pattern="^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$" value="" placeholder="Format is #CCC or #CCCCCC">
<input type="submit" value="submit">
</form>
Live Demo
Html5 Price format Validation:
You can validate price also. Here the html5 price validation should be allowed the price two digits after the decimal point. If you are entering more than 2 digits it won't accept the value. You will modify the patterns according to your validation.
Price validation code:
<form onsubmit="alert('Submitted.');return false;">
<input type="text" required="" pattern="\d+(\.\d{2})?" value="" placeholder="Price (Format: 5.00)">
<input type="submit" value="submit">
</form>
Live Demo
Html5 Domain format Validation:
Here you can validate the domain name it should be a string with dot after the dot 2digit minimum condition set in the html5 pattern. check out he live example demo below
Domain validation code:
Domain validation code:
<form onsubmit="alert('Submitted.');return false;">
<input type="text" required="" pattern="^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$" value="" placeholder="san.com">
<input type="submit" value="submit">
</form>
Live Demo
If you want to customize the error code means you can use the javascript to customize the form field validation error codes.
1 Comments
good tricks i am also a blogger kinda struggling
ReplyDelete