1

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?

3
  • FYI, this doesn't have anything to do with PHP. Commented Dec 2, 2009 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. Commented Dec 2, 2009 at 19:38
  • Please post some of the body of Validator. Otherwise how can we tell what the problem is:) Commented Dec 2, 2009 at 19:38

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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:
Form is still submitted even though listener function returns false

jQuery approaches:
How to not submit a form if validation is false

Comments

0

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

1 Comment

No, input elements don't have submit events. It has to be a form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.