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>'; ?> 

  1. javascript case sensitive. true , false not defined (you mean true , false) so, on encountering them, javascript engine throw exception , normal form submission resume.
  2. you testing length property of htmlinputelement (unless define otherwise) undefined. want test length of value. elem.value.length.

Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -