Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form validation with javascript vs php

Tags:

Why should I bother to use JavaScript for form validation when I still have to use PHP since the user could have JavaScript support turned off.

Isn't it unnecessary?

Update:

Ok thanks for your answers. it sounds like a good idea to have it on the client side too. where can I download good JavaScript validations?

Do you know where I can download a validation script like that one in yahoo when you register an account?

like image 655
ajsie Avatar asked Nov 13 '09 01:11

ajsie


People also ask

Should I use JavaScript or PHP for validation?

You should ALWAYS validate in PHP on the SERVER SIDE and validation in JavaScript is CLIENT SIDE validation for user CONVENIENCE. Thanks to validation on client user may find errors in his form without page relodaing.

Can JavaScript handle form validations?

JavaScript FormsHTML form validation can be done by JavaScript.

Why is JavaScript form validation not sufficient?

JavaScript powered validation can be turned off in the user's browser, fail due to a scripting error, or be maliciously circumvented without much effort. Also, the whole process of form submission can be faked. Therefore, there is never a guarantee that what arrives server side, is clean and safe data.

What is the advantage of using JavaScript for validation?

Validation is a method to authenticate the user. JavaScript provides the facility to validate the form on the client-side so data processing will be faster than server-side validation. It is preferred by most of the web developers.


Video Answer


2 Answers

Javascript validation allows your user to be informed of any errors prior to their submitting the form to the server. This saves irritating page-reloads (since on submit the JS catches the event and validates the form, preventing form-submission if errors are found) and minimises the chances of their having to re-enter information again (and again and again...), or leaving prior to completing the form properly. JS validation is not a substitute for server-side validation (since the user can see the JS, and, by saving the page and amending the JS do whatever they want); but it's a convenience for them.

This is simply part of the concept of progressive enhancement, whereby JS provides a mechanism for enhancing the experience for the user, if it's there and turned on, and hopefully makes their interaction with your site pleasant, or, at least, minimally irritating.


Edited in response to OP's question regarding 'where to download a JS validation tool.'

While I can't -necessarily- recommend any one library (I tend to write my own as required, or borrow from previously self-written examples), a Google search threw these options up:

  • http://www.jsvalidate.com/
  • Stephen Walther's page, discussing Microsoft's CDN and jQuery-validation, linking to jQuery Validation plug-in:
  • jQuery.validate (hosted at MS' ajax.microsoft.com subdomain)
  • jQuery.validate.min
  • jQuery validate plug-in homepage (bassistance.de).
like image 143
David Thomas Avatar answered Oct 14 '22 16:10

David Thomas


You should ALWAYS validate in PHP on the SERVER SIDE and validation in JavaScript is CLIENT SIDE validation for user CONVENIENCE. Thanks to validation on client user may find errors in his form without page relodaing. But user may sent form data without data script validation (for example he may not have JS support in web browser), thus always validate on the server side.

like image 30
mip Avatar answered Oct 14 '22 18:10

mip