javascript - HTML form returns NULL fields -
i have basic form several fields want validate before submitting code. when pass form data js says fields null. please? im new @ js please patient me. relevant form. theres button toggle forms display dont think has effect on this.
<div id = "new_bk" style="display: none;"> <form id = "new_book" onsubmit = "javscript: return(formvalidate());"> book name:<br> <input type="text" name="book_name"> <br> author name:<br> <input type="text" name="author_name"> <br> isbn:<br> <input type="text" name="isbn"> <br> release date:<br> <input type="text" name="release_date"> <br> price:<br> <input type="text" name="price"> <br> stock:<br> <input type="text" name="stock"> <br> sold:<br> <input type="text" name="sold"> <br> restock amount:<br> <input type="text" name="restock"> <br> rating:<br> <input type="text" name="rating"> <br> reviews:<br> <input type="text" name="reviews"> <br> <input type="submit" value="submit"> </form> </div>
and js
function formvalidate(){ // if(document.getelementbyid("book_name").value.length>0){ // alert("ok"); // }else{ // alert("not ok"); // } alert("name is: " + document.getelementbyid('book_name').value); if(document.new_book.book_name.value == ""){ alert("sdkg"); return false; } return true; }
however not trigger alert box. , if switch
alert(document.getelementbyid('book_name'));
it gives me null.
any , appreciated. oh , in case matters. js in external file referenced @ end of body tag of html file.
update
the form sends data correctly now. odd happens.
alert("1"); if(document.new_book.book_name.value == ''){ alert("input book name please"); return false; } alert("2");
this code segment trigger alert("1") end. neither fail scenario alert nor alert("2") triggered. ideas why?
for code:
alert(document.getelementbyid('book_name'));
notice textbox has no id attribute trying traget.
<input type="text" name="book_name">
change add id attribute:
<input type="text" id="book_name" name="book_name">
and work.
Comments
Post a Comment