Using javascript for form validation in a PHP file -
this first time trying javascript , having trouble getting work. have php file text box , submit button. upon clicking submit button, want javascript preform validation make sure text box not empty. please see code below. upon running, nothing happens when click submit button.
<html><head><script type="text/javascript"> function formvalidator() { var client_name = document.getelementbyid('client_name'); if(notempty(client_name, "client name blank")){ return true; } return false; } function notempty(elem, helpermsg) { if(elem.length == 0) { alert(helpermsg); elem.focus(); return false; } return true; } </script></head> <?php echo '<body><form onsumbit="return formvalidator()" method="post"> <table border="1" cellspacing="0" cellpadding="0"><tr><td colspan="2"> <h1>add account</h1></td></tr> <tr> <td>client name: <font size="1">(required)</font></td> <td><input type="text" id="client_name" name="client_name" size="35"/></td></tr> </table> <input type="submit" name="submit_account" value="add account"/> </form></body></html>'; ?>
- javascript case sensitive.
true
,false
not defined (you meantrue
,false
) so, on encountering them, javascript engine throw exception , normal form submission resume. - you testing
length
property ofhtmlinputelement
(unless define otherwise)undefined
. want test length of value.elem.value.length
.
Comments
Post a Comment