javascript - validate input for jquery -
i haven`t form, input html:
<script src="index.js"></script> </head> <body> <p>name</p> <input type="text" size="15" id="title" placeholder="enter title" onclick="check(this)"> <p>description</p> <textarea rows="7" cols="20" id="description" placeholder="enter description"> </textarea> <p>price</p> <input type ="number" value="00.0" min="0" step="0.1" id="price"><br> <button id="add_new_product" >add new product</button><br> <a href="catalog.html">all product</a> </body>
i must validate input on empty. js file:
$(function(){ $('#add_new_product').click(function(){ var title = $("#title").val(); var description = $("#description").val(); var price = $("#price").val(); $.ajax({url:'adres/add_new_product',type:'get', data:{description:description,title:title, price:price},success:function(result){ alert('product add!)'); } }); }); }); $(document).ready(function(){ $.ajax({url:'adres/all_products', type: 'get', datatype: 'json', success:function(result){ for(var = 0; < result.length; i++){ $('#list_of_products').append('<b>' + result[i].name + '</b><br>' + result[i].description + '</br><br>'+result[i].price+'<br><br>');} }}); });
simple, can call javascript function while new product button clicked.
replace
<button id="add_new_product" >add new product</button><br>
with
<button id="add_new_product" onclick="javascript: validatefield()" >add new product</button><br>
in validatefield() function can validate field.
refer , http://jsfiddle.net/nwwl4/19/
Comments
Post a Comment