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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

formbox validation problem

Status
Not open for further replies.

abienz

Programmer
Aug 13, 2001
53
0
0
GB
Hi there I wonder if I can get any help with a simple function that I'm writing to check users input to a textbox.

Here's the story...

I've a textbox on a page called 1001 (name="1001")
it has an onBlur event tied to it that calls a function...
onBlur=checkseats('1001');

the function looks like this...

function checkseats(myBox) {
if (document.form1[myBox].value>'5'){alert('You can only order up to 5 tickets per event');}
}

This seems pretty simple but it's having a problem and I think it's because I'm using server-side scripting to generate the names of the text box, even though when I view source for that page it all looks fine!?

This is what I have written on my page for the text box

<input type=&quot;text&quot; name=&quot;<%=Rs.Fields.Item(&quot;eventID&quot;).Value%>&quot; size=&quot;1&quot; value=&quot;<%=Rs.Fields.Item(&quot;Quantity&quot;).Value%>&quot; maxlength=&quot;1&quot; onBlur=checkseats('<%=Rs.Fields.Item(&quot;eventID&quot;).Value%>');>

Thanks in advance!


 
I think the problem is here:
You compare numeric value of [myBox].value with a string because of the quotes: '5'.

You should do it like this:
... [myBox].value > 5 )

You can also add parseInt to be sure that you deal with numeric value of the text field:

if ( parseInt(document.form1[myBox].value) > 5 )
{...

 
I've tried that but now I get an error on the line that my text box is, saying Object Expected.

Why do you think that might be? It is ok to use Server-side scripting in Client-Side scripting like I have done isn't it?

Again when I look at the source code everything looks fine.

Is it ok to use an number as a name for a textbox?
 
Ah I think I may have cracked it, I put the word box infront of my name for the text box, so when the page was run I got the name box1001

So my page eneded up like this somewhat...

function checkseats(myBox) {
if (document.form1[myBox].value>5){alert('You can only order up to 5 tickets per event');}
}

and the box looked like this...

<input type=&quot;text&quot; name=&quot;box<%=Rs.Fields.Item(&quot;eventID&quot;).Value%>&quot; size=&quot;1&quot; value=&quot;<%=Rs.Fields.Item(&quot;Quantity&quot;).Value%>&quot; maxlength=&quot;1&quot; onBlur=checkseats('box<%=Rs.Fields.Item(&quot;eventID&quot;).Value%>');>

This worked lovely, so it would appear that Javascript doesn't allow the use of integers for names of objects.

A valuable lesson learnt there!

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top