form validation not working in both IE and Firefox.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tharini
    New Member
    • Aug 2010
    • 9

    form validation not working in both IE and Firefox.

    I have a simple form with four fields. The validation code for these fields are not working properly in both IE and firefox.I have given the code below.Please tell me whats the problem.


    Code:
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--
    .content {
    overflow: auto;
    width: 400px;
    height:200px;
    }
    -->
    </style>
    <script language="javascript" type="text/javascript" >
    function checkvals(){
    	if (document.myFRM.name.value=="NULL"){
    			alert("Please Enter the Name field.");
    			document.myFRM.name.focus();
    			return false;
    	} else if (document.myFRM.mobile.value=="NULL"){
    			alert("Please Enter a valid Mobile Number.");
    			document.myFRM.mobile.focus();
    			return false;
    	} else if (document.myFRM.email.value=="NULL") { 
          alert("Please enter a valid email address.");
    	  document.myFRM.email.focus();
    			return false;
    	} else if (document.myFRM.message.value=="NULL") {
    			alert("Please Enter the Message Field.");
    			document.myFRM.message.focus();
    			return false;
    	} else {
    		return true;
    	}
    }
    </script>
    </head>
    
    <body>
    <table width="720" height="585" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <th scope="col"><form  method="post" name="myFRM" id="myFRM"  onSubmit="return checkvals()" action="">
          <table width="553" height="478" border="0" cellpadding="0" cellspacing="0">
            <tr>
              <th height="52" colspan="4" scope="col">&nbsp;</th>
              </tr>
            <tr>
              <th width="85" height="41" scope="row">&nbsp;</th>
              <td width="136"><div align="right">Name:</div></td>
              <td width="10">&nbsp;</td>
              <td width="322"><input name="name" type="text" id="name"></td>
            </tr>
            <tr>
              <th height="37" scope="row">&nbsp;</th>
              <td><div align="right">Mobile No.:</div></td>
              <td>&nbsp;</td>
              <td><input name="mobile" type="text" id="mobile"></td>
            </tr>
            <tr>
              <th height="37" scope="row">&nbsp;</th>
              <td><div align="right">E-Mail:</div></td>
              <td>&nbsp;</td>
              <td><input name="email" type="text" id="email"></td>
            </tr>
            <tr>
              <th height="221" scope="row">&nbsp;</th>
              <td><div align="right" class="style1">Message:</div></td>
              <td>&nbsp;</td>
              <td><textarea name="message" class="content" id="message"></textarea></td>
            </tr>
            <tr>
              <th scope="row">&nbsp;</th>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td><input type="submit" name="Submit" value="Submit"></td>
            </tr>
          </table>
        </form></th>
      </tr>
    </table>
    </body>
    </html>
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    the value of an empty textfield isn't "NULL" ... it is an empty string "" ... so if you want to check for empty values you might either test for the shown empty string or > 0.

    you might even trim the value to check for only whitepaces or use regular expressions to check for whatever characters shouldn't/should be valid in the fields.

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      By ">0", I assume he means the .length property of the string.

      Comment

      Working...