0

Is there a good technique to ensure that the validation performed by PHP is identical to that performed my Javascript?

2
  • 1
    Thorough testing, or use of the same regexes. Commented Oct 5, 2011 at 20:32
  • 2
    You can have similar code only for simple cases. How would you check for email uniqueness from Javascript? Commented Oct 5, 2011 at 20:33

1 Answer 1

2

There used to be a time where we would validate using javascript only and then people saw that you could bypass javascript validation so they started validating with PHP/ASP too. And then, people got bored, but sudenly, AJAX apperead :)

My recommendation that i use about 50% of the time is the following:

Just POST your form via ajax using your library of choice (i use jQuery) to a validation script made in php, expect a JSON return giving your errors or return some HTML to display to your user with the error. Using this technique, it looks like javascript validation because the page doesn't change and the validation is done on PHP side only.

Thats what i tend to implement when i can unless i'm forced to use file uploads (You can't use file uploads correctly with ajax posting) or unless my boss wants me to absolutely do real Javascript validation.

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

Comments

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.