PDA

View Full Version : Resolved javascript validation alert problem



vineet
11-22-2008, 07:54 AM
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


<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>

jscheuer1
11-22-2008, 09:45 AM
That's because regardless of whether or not either value is empty, the alert still fires, try:


<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>

vineet
11-22-2008, 09:50 AM
That's because regardless of whether or not either value is empty, the alert still fires, try:


<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