vote up 0 vote down star

If I am defining a function in JavaScript at the top of my page that validates if the string entered in a form is (A-Z,a-z, or 0-9). And then I call that function when they submit it saying :

onsubmit="return Validator(this);"

If the function name is :

function Validator(form)

Why isn't it actually validating the string submitted when we actually click on submit, and is there a better way of validating forms?

flag

80% accept rate
FYI, this doesn't have anything to do with PHP. – Tom Ritter Dec 2 at 19:35
Check out this link yuilibrary.com/gallery/show/formvalidator Its a contribution to YUI, examples are here murdog05.github.com/yui3-gallery/… I think you'll find it useful. – Zoidberg Dec 2 at 19:38
Please post some of the body of Validator. Otherwise how can we tell what the problem is:) – Byron Whitlock Dec 2 at 19:38

4 Answers

vote up 0 vote down check

You aren't submitting a string to Validator, you are submitting an input element. So you would do if(form.element.value != "") ... or something... And yes there is a better way

link|flag
No, input elements don't have submit events. It has to be a form. – David Dorward 2 days ago
Absolutely right. Brainfart ;) – Byron Whitlock 2 days ago
vote up 2 vote down

Almost certainly because the Validator function is not written correctly… but since you haven't shown us what it looks like, we can't say specifically why.

Perhaps it is expecting a String and you are giving it an HTMLFormElement.

link|flag
vote up 1 vote down

Lots of options for form validation. Depending on your framework, you may want to use Jquery's validation. If you go with yui, I have submitted a form validator that will be useful

Here is the gallery submission, from thee you can look at examples

link|flag
vote up 1 vote down

An onsubmit handler function needs to return false to cancel the event (form submission) if you want to not submit and show a validation error message instead.

Plain Javascript approaches:
http://stackoverflow.com/questions/967483/form-is-still-submitted-even-though-listener-function-returns-false

jQuery approaches:
http://stackoverflow.com/questions/665470/how-to-not-submit-a-form-if-validation-is-false

link|flag

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.