Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function is not being called

Status
Not open for further replies.

cmn2

Programmer
Mar 6, 2003
73
US
Hello,

I am trying to get this simple validation script to work but can't. Does anyone see the reason why?
I really appreciate and would like to thank anyone who takes the time to look at this.


<html>
<head>
<script type="text/javascript">
function validate()
{
x=document.myForm

seats=x.txtNumberOfSeats.value
firstname=x.txtFirstName.value
lastname=x.txtLastName.value
phonenumber=x.txtPhoneNumber.value
submitOK="True"

if (seats<1 ||(seats>20)
{
alert("The number of seats must be between 1 and 20")
submitOK="False"
}

if (firstname.length=0)
{
alert("Your must enter your first name.")
submitOK="False"
}

if (lastname.length=0)
{
alert("Your must enter your last name.")
submitOK="False"
}

if (phonenumber.length=0)
{
alert("Your must enter your phone number.")
submitOK="False"
}

if (submitOK=="False")
{
return false
}
}
</script>
</head>
<body>

<form name="myForm" action="javascripttest.html" method="get" onsubmit="return validate()">
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="txtFirstName" size="20"></td>
</tr>

<tr>
<td>Last Name:</td>
<td><input type="text" name="txtLastName" size="20"></td>
</tr>

<tr>
<td>Phone Number:</td>
<td><input type="text" name="txtPhoneNumber" size="20"></td>
</tr>

<tr>
<td>Date:</td>
<td><input type="text" name="txtDate" size="20"></td>
</tr>

<tr>
<td>Flight Number:</td>
<td>
<select name="lstFlights">
<option value="101">101
<option value="201">201
<option value="301">301
</select>
</td>
</tr>

<tr>
<td>Number of Seats:</td>
<td><input type="text" name="txtNumberOfSeats" size="20"></td>
</tr>

<tr>

<td>
Smoking:<input type="radio" name="optSmokingPref" value="smoking">
</td>


<td>
Non-Smoking:<input type="radio" checked name="optSmokingPref" value="non-smoking">
</td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
<td>Thank You</td>

</tr>
</table>
</form>

</body>
</html>
 
first thing I would change would be this, try it and see if it works afterwards:
Code:
if (seats<1 ||(seats>20)[red][b])[/b][/red]

-kaht

banghead.gif
 

I would change this:

Code:
x=document.myForm

to this:

Code:
x = document.forms['myForm'].elements;

A lot of your equality tests, for example:

Code:
(firstname.length=0)

have only single "=", where they should have double "==".

I would also question why you are assigning strings to submitOK instead of using the boolean values true and false... although your method is by no means incorrect.

Personally, I would add some semicolons, but that's up to you.

Hope this helps,
Dan

 
Thanks to the both of you! I put in all of the changes you pointed out and am now back in business. I appreciate your time in helping me out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top