1

In my LAMP application I am using Javascript for form validation but of course internet users can switch Javascript off in their browser so I also validate the form data on the server using a PHP function.

Does anyone know if there is some clever way to reuse my Javascript validation on the server so as to avoid writing a PHP version?

2
  • 1
    I can only think of 1. Node if you want to use a similar script, but you're currently using PHP 2. A framework that's smart enough to generate a javascript restriction, but I don't know of any, or 3. Use HTML5 inputs. Doesn't work in older browsers obviously, but they're really simple, and the most important thing is your server validates it and gives errors if necessesary. For example, a Zend will repopulate the old values, so it's not a big deal for the user Commented Jan 20, 2013 at 19:49
  • 1
    There's really not enough evidence here that even a server-side JavaScript engine like Node would help, since we don't know how the client-side validation was implemented. For example, if the validation depends on attributes of the HTML, you'd have to figure out ways to make that information available to the server. That's a far-from-trivial task I don't think Node can help with. Commented Jan 20, 2013 at 21:17

2 Answers 2

2

It's a little difficult to tell without knowing how you've constructed your application, but it's unlikely you'll be able to recycle your JavaScript on the server side. On the other hand, if you construct a lightweight, fast php validation tool you can use server-side validation via XHR instead of (a lot of the) JavaScript, and save yourself some redundancy the other direction.

However, that way's bound to be slower, so the best combination of user experience and security will probably come from having some redundancy in your validation.

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

Comments

1

This might be what you are looking for. It is not a javascript implementation. But this should provide all of your basic validation methods. I do not believe you would be able to find anything close to javascript unless you are using Node.js

Validator.php

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.