| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
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. |
|
|
|
|
|
#4 (permalink) |
|
shiro
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. |
|
|
|
#6 (permalink) |
|
shiro
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. |
|
|
|
#7 (permalink) |
|
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. |
|
|
|
#8 (permalink) |
|
shiro
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. |
|
|
|
#12 (permalink) |
|
shiro
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! |
|
![]() |