javascript - JQuery validations not working empty data is inserted into table rows -
i've text field in table id name . have added validation on it. not working
<td>name:</td> <td> <input type="text" id="name" class="required"/>(required) </td> $("#name").focusout(function(){ if (!$(this).val()) { alert("this field required"); $(this).focus(); } });
i have complete code on jsfiddle . please check link
just make sure put inside document ready , it'll work fine:
$(function () { $("#name").focusout(function () { if (!$(this).val()) { alert("this field required"); $(this).focus(); } }); });
edit: full page requested op:
<!doctype html> <html> <head> <title>dynamic book library</title> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } th, td { padding: 5px; text-align: left; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <script> var count = 1; var rating; function addrow() { var myname = document.getelementbyid("name"); var auther = document.getelementbyid("auther"); var publish = document.getelementbyid("publish"); var ratings = document.getelementsbyname("rating"); (var = 0; < ratings.length; i++) { if (ratings[i].checked) { rating = ratings[i]; } } var table = document.getelementbyid("mytabledata"); var rowcount = table.rows.length; var row = table.insertrow(rowcount); row.insertcell(0).innerhtml = count; row.insertcell(1).innerhtml = myname.value; row.insertcell(2).innerhtml = auther.value; row.insertcell(3).innerhtml = publish.value; row.insertcell(4).innerhtml = rating.value; count = count + 1; } function load() { console.log("page load finished"); } $(function() { $("#name").focusout(function () { console.log('bla'); if (!$(this).val()) { alert("this field required"); $(this).focus(); } }); }); </script> </head><body onload="load()"> <div id="mydata"> <b>current data in system ...</b> <table id="mytabledata" border="1" style="width:100%"> <tr id="templaterow"> <th>id</th> <th>name</th> <th>authers</th> <th>published</th> <th>ratings</th> </tr> </table> <br/> </div> <div id="myform"> <b>simple form name , age ...</b> <table style="width:100%"> <tr> <td>name:</td> <td> <input type="text" id="name"> </td> </tr> <tr> <td>auther:</td> <td> <input type="text" id="auther"> </td> </tr> <tr> <td>published:</td> <td> <input type="date" id="publish"> </td> </tr> <tr> <td>rating:</td> <td> <input type="radio" value="1" id="rating" name="rating">1 <input type="radio" value="2" id="rating2" name="rating">2 <input type="radio" value="3" id="rating3" name="rating">3 <input type="radio" value="4" id="rating4" name="rating">4 <input type="radio" value="5" id="rating5" name="rating">5</td> </tr> </table> <br> <input type="button" id="add" value="add" onclick="javascript:addrow()"> </div> </body> </html>
Comments
Post a Comment