Results 1 to 3 of 3

Thread: javascript validation alert problem

  1. #1
    Join Date
    Jun 2008
    Posts
    121
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default javascript validation alert problem

    hi all

    i have a form with two fields that alert combine validations if both fields are empty. But the problem is i m getting a blank alert with nothing written when both fields are filled and submit button is clicked.

    this is the code
    Code:
    <script language="javascript">
    function validate()
    {
    var msg="";
    if(document.advertise.uname.value=='')
    	{
    		msg="Must Enter UserName\n";
    		document.advertise.uname.focus();
    	}
    if(document.advertise.ucompany.value=='')
    	{
    		msg= msg + "Must Enter Company Info\n";
    		document.advertise.ucompany.focus();
    	}
    		alert(msg);
    		return false;
    }
    </script>
    <body>
    <table width="501" border="0" align="center">
      <form name="advertise" onSubmit="return validate()">
          <tr>
          <td width="213" height="30" valign="top" bgcolor="#EFEFEF" class="bodytext"><div align="right">Name</div></td>
          <td height="30" valign="top" bgcolor="#EFEFEF" class="bodytext">&nbsp;</td>
          <td height="30" valign="top" bgcolor="#EFEFEF" class="bodytext"><input name="uname" type="text" class="bodytext" id="uname" size="30" /></td>
        </tr>
        <tr>
          <td height="30" valign="top" bgcolor="#EFEFEF" class="bodytext"><div align="right">Company Name</div></td>
          <td width="11" height="30" valign="top" bgcolor="#EFEFEF" class="bodytext">&nbsp;</td>
          <td width="263" height="30" valign="top" bgcolor="#EFEFEF" class="bodytext"><input name="ucompany" type="text" class="bodytext" id="ucompany" size="30" /></td>
        </tr>
         <tr>
          <td height="50" class="bodytext"><div align="right">
            <input name="submit" type="submit" class="style45" id="submit" value="Submit" />
          </div></td>
          <td class="bodytext">&nbsp;</td>
          <td class="bodytext"></td>
        </tr>
          </form>
    </table>
    </body>
    Last edited by vineet; 11-22-2008 at 09:53 AM. Reason: resolved

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That's because regardless of whether or not either value is empty, the alert still fires, try:

    Code:
    <script language="javascript">
    function validate()
    {
    var msg="";
    if(document.advertise.uname.value=='')
    	{
    		msg="Must Enter UserName\n";
    		document.advertise.uname.focus();
    	}
    if(document.advertise.ucompany.value=='')
    	{
    		msg= msg + "Must Enter Company Info\n";
    		document.advertise.ucompany.focus();
    	}
    		if(msg != ''){
    				alert(msg);
    				return false;
    		} return true;
    }
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2008
    Posts
    121
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default javascript validation

    Quote Originally Posted by jscheuer1 View Post
    That's because regardless of whether or not either value is empty, the alert still fires, try:

    Code:
    <script language="javascript">
    function validate()
    {
    var msg="";
    if(document.advertise.uname.value=='')
    	{
    		msg="Must Enter UserName\n";
    		document.advertise.uname.focus();
    	}
    if(document.advertise.ucompany.value=='')
    	{
    		msg= msg + "Must Enter Company Info\n";
    		document.advertise.ucompany.focus();
    	}
    		if(msg != ''){
    				alert(msg);
    				return false;
    		} return true;
    }
    </script>
    hi john

    thanks for the help. your code worked perfect. everything fine

    vineet

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •