25

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?

Community
  • 1
  • 1
ajsie
  • 77,632
  • 106
  • 276
  • 381
  • Out of the question, the most advanced, and regularly updated validation tool is https://github.com/google/libphonenumber and it's npm port: https://github.com/ruimarinho/google-libphonenumber The problem is that it takes >500Kb to bundle libphonenumber in your frontend app. (https://bundlephobia.com/package/google-libphonenumber@3.2.22) Worth reading, Falsehoods Programmers Believe About Phone Numbers: https://github.com/google/libphonenumber/blob/master/FALSEHOODS.md Given the complexity I would also consider to use specialized online services, eg: https://validatephonenumber.com/ – dsdenes Aug 20 '21 at 09:41

8 Answers8

47

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:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • so the best practice is to implement form validation for both javascript and php side? – OMGPOP Aug 26 '12 at 04:03
  • 4
    Absolutely, client-side is merely a functional courtesy to your users, it should never be considered 'secure.' All user-submitted data should be validated and sanity-checked on the server-side before storing. – David Thomas Aug 26 '12 at 12:50
7

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.

mip
  • 8,355
  • 6
  • 53
  • 72
  • Should I just validate it in JavaScript, and then when it's valid, submit and also validate in PHP just to make sure? And that would require me to check for the exact same thing in both JS and PHP to prevent any inconveniences? – Chris Jul 29 '18 at 22:21
3

... as courtesy to the users pretty much. Makes life easier for the ordinary users that simply commit human things from time to time.

vector
  • 7,334
  • 8
  • 52
  • 80
  • 1
    Seconded. Especially on slow connections, Javascript warnings informing about mistakes are a great enhancement in convenience. – Pekka Nov 13 '09 at 01:55
3

I recommend you using unified server-side and client-side validation using a framework, since it may avoid confronting the user to data valid on client side but rejected by the server, or the opposite (client side too restrictive). Following list of framework give information about server/client side validation: http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks

snowflake
  • 1,750
  • 2
  • 17
  • 40
1

It's a matter of whether you want your form (and website as a whole) to be interactive-cum-user-friendly or not. You can just let the server-side do the validations and throw the error back to the users but that would be less interactive and less user-friendly than warning the users before they submit the form (although you still need to validate the inputs on server-side no matter what). Just my 2 cents :P

Lukman
  • 18,462
  • 6
  • 56
  • 66
1

Yes, it is best practice to validate the user input values from both sides client and server side ,

some cases client was disabled javascript or mobile browser that doesn't javascript, remember there is spammers also.

Matt
  • 74,352
  • 26
  • 153
  • 180
mahesh
  • 19
  • 1
1

I recomend to use Javascript for client side and Php for server side This will make interaction or user friendly site nad reduce reloading page many times in case user submit wrong data

0

To my mind, only client-side-checking of form input does not work because of security. Imagine you want to check a user password(if "yourpwd" == userinput), with js the user will see the password because it is in the browser-sourcecode .With php, it is not visible because php is for some reason hidden.

Timo
  • 2,922
  • 3
  • 29
  • 28