Old 28-01-2008, 07:51   #1 (permalink)
Witchfinder
Registered User
 
Join Date: Jan 2008
Posts: 13
Javascript form validation

Hello,
I'm having some trouble with form validation on a page. The script is mean't to make sure the user has selected at least two fields on the form. If they don't select at least two they get an error message telling them so. The script uses if/else if statements. The problem only the first two fields work if there selected. If its anything from style, gear, frame or price they arent accepted as having there values selected.

Any help would be appriciated.

Last edited by Witchfinder : 17-02-2008 at 10:21.
  Reply With Quote
Old 28-01-2008, 09:57   #2 (permalink)
pgo
i'm done, son
 
Join Date: Jan 2005
Posts: 12,262
JavaScript form validation is essentially pointless bloat as you'll have to validate the data again once it gets to the server.
  Reply With Quote
Old 28-01-2008, 11:16   #3 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,081
Nice to have both though.
  Reply With Quote
Old 28-01-2008, 11:24   #4 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,081
Change each 'else if' to 'if'

With 'else if', the script will only enter the first function it comes for which it matches the conditions. That means that if, for example, form.brand.options[brand1].value == "N/A", it will add 1 to count, and then skip over the rest of the checks going right on to check if count is less than or equal to five.

also instead of "count = count + 1", you can write that in short hand as "count +=1". It wont change your function, but its common to write it that way.
  Reply With Quote
Old 28-01-2008, 11:57   #5 (permalink)
Witchfinder
Registered User
 
Join Date: Jan 2008
Posts: 13
I've changed it to this however its still not working. Think I tried removing the else's last night. I haven't a clue. Cheers for any help though!

Last edited by Witchfinder : 17-02-2008 at 10:21.
  Reply With Quote
Old 28-01-2008, 12:09   #6 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,081
I think the problem may be 'count <= 5'.

It looks like you are adding 1 to count for each empty field. That means if they don't choose anything, they will have a count of 6, and the search will proceed, even though thats not what you want. You want the search to proceed if they have less than 5, because the only way they will have less than five is if they have at least two search options selected.

edit: this is assuming your syntax is right on everything. Try using alert(form.brand.options[brand1].value) etc to see what the value for the options are to make sure you have the right syntax. If you get any "undefined", you have some syntax errors. It looks alright to me now, but I'm notorious for screwups like that, so I always output and check everything when my scripts arent working.
  Reply With Quote
Old 28-01-2008, 12:26   #7 (permalink)
Witchfinder
Registered User
 
Join Date: Jan 2008
Posts: 13
Yeah I just noticed that count error a few moments ago. Its been changed to "count >= 5" now.

I've put those messages in along with tags to show whichs messages are showing and although all fields aren't changed only the first two show up been brand and type. Even though style, gear, frame and price aren't changed it seems like they aren't been added to the count value hence to error messages. I can't see why though.
  Reply With Quote
Old 28-01-2008, 12:30   #8 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,081
Try alerting all the values before they get tested in the 'if' statements to see what they are. Once you know what the values are, you will know why they are (or arent) being caught by the if statements.

Good luck, bedtime for me now.
  Reply With Quote
Old 28-01-2008, 12:39   #9 (permalink)
Witchfinder
Registered User
 
Join Date: Jan 2008
Posts: 13
Just brand and type. I'll look into it later.


In a bit.
  Reply With Quote
Old 28-01-2008, 13:14   #10 (permalink)
Witchfinder
Registered User
 
Join Date: Jan 2008
Posts: 13
Theres the form code which uses dynamic drop down menus.

Last edited by Witchfinder : 17-02-2008 at 10:21.
  Reply With Quote
Old 28-01-2008, 18:25   #11 (permalink)
Witchfinder
Registered User
 
Join Date: Jan 2008
Posts: 13
Sorted it. Had a capital g on gear on the javascript.

Opps...
  Reply With Quote
Old 28-01-2008, 18:41   #12 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,081
Spelling errors will kill a guy!

That's why its good to output data at different points into your script - you can see exactly where the data isn't what it should be, then work backwards and find out why not. I think I don't actually spend that long writing code myself, I just spend most of my time figuring out where I had a spelling or syntax error!
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8