Connecting Tech Pros Worldwide Help | Site Map

Problem with form field validating

Shane Jones
Guest
 
Posts: n/a
#1: Jul 23 '05
on my php page i have a submit button and when I submit i want to
validate that dome fields have data in.

i have this as my submit button

<input name="Enter" type="button" onClick="frmValidate()" value="Submit">

and this is my javascript function

<script language='JavaScript' type='text/javascript'>
function frmValidate()
{
if (frm.lName.value == '')
{
alert('Some required fields are empty!');
} else {
frm.submit
}
}
</script>

but it don't work can anyone see where i have gone wrong

is it because the page is .php instead of .html / .htm

thanks

Shane Jones.................
Martin Honnen
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Problem with form field validating




Shane Jones wrote:
[color=blue]
> on my php page i have a submit button and when I submit i want to
> validate that dome fields have data in.
>
> i have this as my submit button
>
> <input name="Enter" type="button" onClick="frmValidate()" value="Submit">[/color]

How about
<input type="submit"
onclick="return frmValidate(this.form);"
name="Enter"
value="Submit">
and
function frmValidate (form) {
if (form.lName.value == '') {
alert(...);
return false;
}
else {
return true;
}
}
That way you have a submit button that works even if script is not
supported or is disabled.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Shane Jones
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Problem with form field validating


Martin, thanks for the quick reply

bit it is still not working

does it matter that i am using firefox
Martin Honnen
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Problem with form field validating




Shane Jones wrote:
[color=blue]
> bit it is still not working
>
> does it matter that i am using firefox[/color]

I posted code that hopefully works with Firefox, IE and other browsers.

What exactly goes wrong, what does the Firefox JavaScript console say?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Shane Jones
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Problem with form field validating


nothing at all appears in the console
Lee
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Problem with form field validating


Shane Jones said:[color=blue]
>
>Martin, thanks for the quick reply
>
>bit it is still not working
>
>does it matter that i am using firefox[/color]

No. Post what you're trying now.
What does "still not working" mean?

Shane Jones
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Problem with form field validating


Hi Lee here is my page code now

This is for an editing page for database information, it just needs to
verify that the lName, lAddress1, lTown and lPhoneNo are not empty

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Inserting New Location</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language='JavaScript' type='text/javascript'>
function frmValidate (form) {
if (form.lName.value == '' || form.lAddress1.value == '' ||
form.lTown.value == '' || form.lPhone.value == '') {
alert('One or more of the required fields are empty.');
return false;
}
else {
return true;
}
}
</script>

</head>
<link href="redifinedTags.css" rel="stylesheet" type="text/css">
<body background="images/background.jpg">
<?
$No = $_POST['lNo'];
mysql_connect('localhost','','');
mysql_select_db('expense') or die('Error selecting database.');
$locQuery = "SELECT * FROM locations WHERE lNo = $No";
?>
<?
$locResult = mysql_query($locQuery);
while ($lRow = mysql_fetch_assoc($locResult))
{
extract($lRow);
?>

<h4>Editing <? echo $lName ?></h4>
<p>Use this below form to edit details about this item.</p>
<hr>
<form action="php_scripts/replace_location_script.php" method="post">
<table width="70%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="29%" height="30"><p>Name</p></td>
<td width="70%">
<input name="rNo" type="hidden" size="20" maxlength="30"
value="<? echo $lNo ?>">
<input name="rName" type="text" size="45" maxlength="30"
value="<? echo $lName ?>"><font size="-3" face="Verdana"
color="#000099">req.</font>
</td>
</tr>
<tr>
<td height="30"><p>Address</p></td>
<td>
<input name="rAddress1" type="text" size="45" maxlength="30"
value="<? echo $lAddress1 ?>"><font size="-3" face="Verdana"
color="#000099">req.</font>
</td>
</tr>
<tr>
<td height="30"></td>
<td>
<input name="rAddress2" type="text" size="45" maxlength="30"
value="<? echo $lAddress2 ?>">
</td>
</tr>
<tr>
<td height="30"><p>Town</p></td>
<td>
<input name="rTown" type="text" size="20" maxlength="15"
value="<? echo $lTown ?>"><font size="-3" face="Verdana"
color="#000099">req.</font>
</td><td width="1%">
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td height="30"><p>Phone</p></td>
<td>
<input name="rPhoneNo" type="text" size="20" maxlength="15"
value="<? echo $lPhoneNo ?>"><font size="-3" face="Verdana"
color="#000099">req.</font>
</td>
</tr>
</table>
<?
}
mysql_free_result($locResult);
?>
<br>
<input name="Enter" onClick="return frmValidate(this.form);"
type="submit" value="Update">
<input name="Reset" type="reset" value="Reset">
</form>
</body>
</html>
--
Shane Jones.................
shanemjo82@yahoo.co.uk <mailto:shanemjo82@yahoo.co.uk>
Martin Honnen
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Problem with form field validating




Shane Jones wrote:

[color=blue]
> function frmValidate (form) {
> if (form.lName.value == '' || form.lAddress1.value == '' ||[/color]
^^^^^^
Here you are looking for controls with the name lName, lAddress and so
forth but below in the form you use the names

[color=blue]
> <input name="rName" type="text" size="45" maxlength="30"[/color]
^^^^^
rName and rAddress and so forth. So perhaps it is only a naming issue.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Lee
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Problem with form field validating


Shane Jones said:[color=blue]
>
>Hi Lee here is my page code now
>
>This is for an editing page for database information, it just needs to
>verify that the lName, lAddress1, lTown and lPhoneNo are not empty
>
><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
><html>
><head>
><title>Inserting New Location</title>
><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
><script language='JavaScript' type='text/javascript'>
>function frmValidate (form) {
> if (form.lName.value == '' || form.lAddress1.value == '' ||
>form.lTown.value == '' || form.lPhone.value == '') {
> alert('One or more of the required fields are empty.');
> return false;
> }
> else {
> return true;
> }
> }
></script>[/color]


[color=blue]
><form action="php_scripts/replace_location_script.php" method="post">[/color]
[color=blue]
> <input name="Enter" onClick="return frmValidate(this.form);"
>type="submit" value="Update">[/color]

1. You don't have form elements named lName, lAddress1, etc.
2. This sort of form validation function should be invoked from
the onSubmit handler of the form element, never from the onClick
handler of the submit button:

<form action="php_scripts/replace_location_script.php"
method="post"
onsubmit="return frmValidate(this)">

Closed Thread