You can use the below to files,
form.html
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 |
<!DOCTYPE html> <html> <head> <title>Simple Validations:mydoubts.in</title> <script type="text/javascript" src="validate.js"></script> </head> <body> <table id="mytable" border="1" style="border-top: 1px solid Gainsboro;width:53%; border-bottom:1px solid Gainsboro;border-left:1px solid Gainsboro; border-right:1px solid Gainsboro;"> <tr> <p> <td width="1%"><input type="checkbox" required="required" name="chk[]" id="chk[]" /></td> <td width="8%"> <label>Country</label> <select name="country[]" id="[]"> <option value="select">Please Select</option> <option value="1">Africa</option> <option value="2">America</option> <option value="3">Australia</option> <option value="4">China</option> <option value="5">England</option> <option value="6">Japan</option> <option value="7">India</option> <option value="8">Indonesia</option> </select> </td> <td width="7%"> <label >Population Percentage</label> <input type="text" name="population[]" id="population[]"> </td> </p> </tr> <p> <td width="1%"><input type="checkbox" required="required" name="chk[]" id="chk[]" /></td> <td width="8%"> <label>Country</label> <select name="country[]" id="[]"> <option value="select">Please Select</option> <option value="1">Africa</option> <option value="2">America</option> <option value="3">Australia</option> <option value="4">China</option> <option value="5">England</option> <option value="6">Japan</option> <option value="7">India</option> <option value="8">Indonesia</option> </select> </td> <td width="7%"> <label >Population Percentage</label> <input type="text" name="population[]" id="population[]"> </td> </p> </tr> </table> <input type="button" name="save" id="save" value="Save" Onclick="checkvalidaion();"> </body> </html> |
validate.js
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 |
function checkvalidaion() { var table = document.getElementById('mytable'); var rowCount = table.rows.length; var chks = document.getElementsByName('chk[]'); var country=document.getElementsByName("country[]"); var population = document.getElementsByName('population[]'); //validations var testnumbers = /^(10|[0-9])$/; for(var j=0; j<rowCount; j++) { if (chks[j].checked==false) { alert("please select rows using checkbox"); chks[j].focus(); return false; } if(country[j].value=='select') { alert("please select the country"); country[j].focus(); return false; } if(population[j].value=='') { alert("please Enter the populations"); population[j].focus(); return false; } if (!testnumbers.test(population[j].value)) { alert("please Enter Numeric value between0-10"); population[j].focus(); return false; } //validating the same skill levels for(var k=j+1; k<rowCount; k++) { // alert(country[j].value);alert(country[k].value); if((country[j].value)==(country[k].value)&& (country[j].value !='select') && (country[k].value !='select')) { alert("There is no permission to add same country two times "); country[k].focus(); return false; } } } } |