I’ve wrote a simple JavaScript function in the <head> tag of the page to validate a form in that page, and it worked well. Then I wrote another function in the same <head> to validate another form, the second function worked but the first stopped working! I deleted the second function but the first stopped working permanently and I don’t know why. The code is listed below
The first function
[CODE]function validate_addproduct_form()
{
c = /^d{1,}$/;
p = /^d{1,}.{0,1}d{0,2}/;
string = ”;
if (!c.test(addproduct.code.value))
{
string += ‘n* The code you entered is not a valid code.’;
}
if (!p.test(addproduct.price.value))
{
string += ‘n* The price you entered is not a valid price.’;
}
if (string != ”)
{
alert (‘The following error(s) occured:n’+string+’nnPlease correct the errors and try again.’);
event.returnValue = false;
}
else if (addproduct.description.value == ”)
{
action = confirm(“You haven’t inserted description. Do you want to continue?”);
if (action == false)
{
event.returnValue = false;
}
}
}
Which validates this form
[CODE]<form name=”addproduct” enctype=”multipart/form-data” method=”post” action=”index.php” onsubmit=”validate_addproduct_form();”>
<table align=”center” width=”80%” border=”0″ cellpadding=”5″ cellspacing=”0″ style=”border-collapse: collapse” id=”AutoNumber1″>
<tr>
<td align=”right”><label for=”code”>Code:</label></td>
<td><input type=”text” name=”code” value=”” size=”3″ maxlength=”3″ id=”code” /></td>
</tr>
<tr>
<td align=”right”><label for=”price”>Price:</label></td>
<td><input type=”text” name=”price” value=”” size=”6″ maxlength=”6″ id=”price” /> LE</td>
</tr>
<tr>
<td align=”right”>Dimensions (in mm):</td>
<td>
<label for=”l”>Length</label>
<input type=”text” name=”l” value=”” size=”6″ maxlength=”5″ id=”l” />
<label for=”w”> Width</label>
<input type=”text” name=”w” value=”” size=”6″ maxlength=”5″ id=”w” />
<label for=”h”> Hight</label>
<input type=”text” name=”h” value=”” size=”6″ maxlength=”5″ id=”h” />
</td>
</tr>
<tr>
<td align=”right” valign=”middle”>Description:</td>
<td><TEXTAREA name=”description” rows=”10″ cols=”50″ id=”description” /></TEXTAREA></td>
</tr>
<tr><input type=”hidden” name=”MAX_FILE_SIZE” value=”5000000″ />
<td align=”right”><label for=”file”>Upload image:</label></td>
<td><input type=”file” name=”userfile” accept=”image/gif” id=”file” /></td>
</tr>
<tr>
<td align=”left”><input type=”hidden” name=”do” value=”product”>
<input type=”hidden” name=”action” value=”add”></td>
<td align=”center”><BR>
<input type=”submit” value=”Add Product” name=”submit”></td>
</tr>
</table>
</form>
The second function
[CODE]function validate_login_form()
{
if (login.user_name.value == ” || login.password.value == ”)
{
alert(‘Please insert the missing fields’);
event.returnValue = false;
}
}
Which validaes this form
[CODE]<form name=”login” method=”post” action=”index.php” onsubmit=”validate_login_form();”>
<table border=”0″ cellpadding=”10″ cellspacing=”0″ style=”border-collapse: collapse” id=”AutoNumber1″>
<tr>
<td align=”right”><label for=”user_name”>user name:</label></td>
<td><input type=”text” name=”user_name” size=”20″ maxlength=”30″ id=”user_name” /></td>
</tr>
<tr>
<td align=”right”><label for=”password”>password:</label></td>
<td><input type=”password” name=”password” size=”20″ maxlength=”20″ id=”password” /></td>
</tr>
<tr>
<td align=”left”><input type=”hidden” name=”do” value=”login”></td>
<td align=”center”>
<input type=”submit” value=”login” name=”submit”></td>
</tr>
</table>
</form>