This is a discussion on "Form validation help!" within the JavaScript Forum section. This forum, and the thread "Form validation help! are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Form validation help!
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
Form validation help!
I need some help with a form that has 2 text box entries, I tried making the validation from each into separate functions then calling both functions from the one that is called upon on the onsubmit function, but i can't seem to get it to work as it just seems to go ahead with the submit as usual with no validating going on.
|
|
|
|
#2
|
||||
|
||||
|
Re: Form validation help!
use "alert" to debug and find out what the real output of the validation function is:
onsubmit="alert(Validate(this)); return false;" It needs to be false if you want to interrupt form submission.
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#3
|
|||
|
|||
|
Re: Form validation help!
I just tried that but for some reason it just completely ignored the Javascript and even the alert to just submit anyway. I do not have javascript disabled on my browser as my other form (with 1 text box entry) works just fine, but the main problem was trying to get it to work with multiple form entries and functions.
Heres the code atm
|
|
#4
|
|||
|
|||
|
Re: Form validation help!
You should really use the form object and not 'gtElementById'
The following would go on the form: onsubmit="validateMe(this);" and the function example:
__________________
Imagn Design: Los Angeles Web Design Last edited by c010depunkk; Feb 11th, 2008 at 10:02. Reason: please use [HTML] when posting HTML |
|
#5
|
|||
|
|||
|
Re: Form validation help!
The thing is I don't want it alerting (pop-ups) and just have a clear message on the form above the value that is incorrect. I have made a few modifications based on my previous form with javascript validation and my previous version of this form using server-side validation so that both are involved in case Javascript is disabled.
What happens now is that the javascript error messages appear, then the form submits regardless and my php validation kicks in to stop it so replaces the error messages with it's own. So at least it's working partially.
|
|
#6
|
|||
|
|||
|
Re: Form validation help!
If the form is still submitting you need to add:
return false; If an error is found, or return true; If no error is found.
__________________
Imagn Design: Los Angeles Web Design |
|
#7
|
|||
|
|||
|
Re: Form validation help!
I've done this:
But it's still doing the same thing of submitting and showing both messages. |
|
#8
|
||||
|
||||
|
Re: Form validation help!
Quote:
Quote:
What browser are you using? Are you sure it's showing error messages? In IE.: Tools > Options > Advanced > tick: Show notification about every script error Firefox: Get Firebug Instead of... onsubmit="Validate(this);" ... a better implementation would be: onsubmit=" try{ if(Validate(this)) this.submit(); }catch(e){} return false; " This way the form will only submit if the script has no error AND it passes the validation. And if an error occurs, at least it will stay on the page so you can work out what the problem is...
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#9
|
||||
|
||||
|
Re: Form validation help!
this line...
f['Campus'].value.length ... will can cause an error in some browsers if the Campus field is empty. The value property doesn't always return a string (sometimes it's null) so you can;t call the 'length' property. A better approach would be... (f['Campus'].value+'').length ...which makes sure we have a string before we call the length property. Use firebug or alerts to report the progress of the script, step by step, and you will eventually work out where it breaks.
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#10
|
||||
|
||||
|
Re: Form validation help!
OK, ignore both of my previous posts.
I've just realised you've got it working... the problem is that you have this: onsubmit="validateMe(this);" ...when you should have... onsubmit="return validateMe(this);" ...case closed.
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#11
|
|||
|
|||
|
Re: Form validation help!
Unfortunately I tried the method in post #10 already but it ignored the javascript altogether, the closest I got to getting it complete was using your script in #8 which did all the javascript validation and stopped the form submitting...even when all fields are filled out and didn't even let me retry the javascript when I pressed the submit again unless i refreshed the page.
Last edited by psycho wolvesbane; Feb 11th, 2008 at 10:15. |
|
#12
|
||||
|
||||
|
Re: Form validation help!
Quote:
Quote:
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#13
|
|||
|
|||
|
Re: Form validation help!
I turned on the debugger in IE and it comes up with nothing. Also I installed Firebug but I have no idea how to debug with it, either that or it isn't coming up with anything either thus displays nothing useful.
Here's the current code for this annoying form:
Last edited by psycho wolvesbane; Feb 11th, 2008 at 18:46. |
|
#14
|
||||
|
||||
|
Re: Form validation help!
Can you upload this somewhere?
The code reads fine, I'm not sure about the way you're referring to the form elements though:f['CampusEmail'].value may not work in some browsers...
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#15
|
|||
|
|||
|
Re: Form validation help!
|