In the previous two posts, we saw what are the input elements and how to create a form.
In this post, we will see form validation, Different validation methods.
Before applying validation to form elements, let discussing on what validation is and why it is necessary.
What is validation?
When you go to any website, you can see the forms like register, login, and contact forms. If you enter a wrong value or remain blank in the input field then a popup box or alert message will display to give you a warning about wrong data. This is called the validation of input fields.
For example,
If you enter a text value in a number field, you get a message like “Please enter valid input”.
Why Validation is necessary?
Validation is necessary for the authenticity of a user. Validation restricts the false or blank input. Validation is mainly needed for privacy. The form design is incomplete without validation.
What validation includes?
Validation includes checking an empty field, and the correctness and accuracy of input data.
Types of validation
(1) Client-side validation:-
(2) Server-side validation:-
Client-side validation
(1) Validation using HTML5 constraints
• HTML5 attributes
(1) required attribute
HTML5 required attribute |
(2) type attribute
HTML 5 type attribute |
(3) max and min attributes
HTML5 min and max attribute |
(4) maxlength and minlength attributes
(5) pattern attribute
HTML5 pattern attribute |
(6) disabled attribute
HTML5 disabled attribute |
• CSS pseudo selectors
(1) :required pseudo selector
:required pseudo selector selects those input fields that have the required attribute specified.
(2) :valid pseudo selector
(3) :invalid pseudo selector
(4) :disabled pseudo selector
(5) :optional pseudo selector
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 | <!Doctype html> <html> <head> <title>Validation</title> <link rel="stylesheet" href="/storage/emulated/0/Blog/selcetorvalid.css"> <style> form{ background-color:#333; color:#fff; margin:300px; padding:10px; display:inline-block; border:5px solid #0cc;} label,input,textarea{ margin:10px;} </style> </head> <body> <form name="frm" action="#" method="post"> <h2>User registration form</h2> <label for="name">Name:</label><br> <input type="text" name="uname" required><br> <label for="dob">Birthdate:</label><br> <input type="date" name="dob"><br> <label for="age">Age:</label><br> <input type="number" name="age" min="18" max="60"><br> <label for="phno">Phone:</label><br> <input type="tel" name="phno" pattern="[0-9]{4}-[0-9]{2}-[0-9]{4}"><br> <label for="addrs">Address:</label><br> <textarea disabled></textarea><br> <label for="eml">Email id:</label><br> <input type="email" name="eml"><br> <label for="pass">Password:</label><br> <input type="password" name="pass" minlength="8"><br> <label for="repass">Re-type password:</label><br> <input type="password" name="repass" minlength="8"><br> <input type="submit" > </form> </body> </html> |
CSS:-
1 2 3 4 5 6 7 8 9 10 11 | input[type="text"]:required{ border:2px solid #00f; background-color:#fcc;} input[type="number"],[type="tel"],[type="email"],[type="password"]:valid{ border:2px solid #0f0;} input:invalid{ border:2px solid #f00;} input[type="date"]:optional{ border:2px solid #fff;} textarea:disabled{ background-color:#ccf;} |
Output:-
CSS pseudo selectors validation |
• DOM properties and methods
✏ DOM methods
• checkValidity()
• setCustomValidity()
✏ DOM properties
• validity
(1) customError
(2) patternMismatch
(3) stepMismatch
(4) typeMismatch
(5) rangeOverflow
(6) RangeUnderflow
(7) tooLong
(8) valid
(9) ValueMissing
• validationMessage
• willValidate
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 | <!Doctype html> <html> <head> <title>Validation using DOM</title> <style> form{ margin:300px; background-color:#fc9; padding:30px;} </style> </head> <body> <form> <label for="nm">Enter your name:-</label><br><br> <input type="text" id="nam" name="nm" pattern="[A-Z,a-z]"><br><br> <input type="submit" name="sub" onclick="namecheck()"> <h2 id="i1"></h2> </form> <script> function namecheck(){ const n=document.getElementById("nam"); if(!n.checkValidity()){ document.getElementById("i1").innerHTML=n.validationMessage;} } </script> </body> </html> |
Output:-
checkValidity() method |
(2) JavaScript Validation
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 | <!Doctype html> <html> <head> <title>form validation</title> </head> <body bgcolor="pink"> <h2>Student Attandance Form</h2> <form name="studattand" method"post"> <label for="snmae">Stud_name:</label><br> <input type="text" name="sname"><br><br> <label for="rno">Roll_no:</label><br> <input type="number" name="rno"><br><br> <label for="attan">Attandance:</label><br> <input type="number" name="attan"><br><br> <button type="submit" onclick="autovalid()">Input</button> </form> <script> function autovalid(){ var a=document.forms["studattand"]["sname"].value; var b=document.forms["studattand"]["rno","attan"].value; if(!isNaN(a)){ alert("Please Enter text."); } else if(b==""){ alert("Please enter value..") } else{ alert("Data successfully submited"); } } </script> </body> </html> |
Output:-
JavaScript validation |
0 Comments