0

I have form who validate user input, I use PHP, Javascript and AJAX. I want to check every field with regex But I'm little confuse about method to check it...

What's better method to check it Javascript or PHP?

Thanks in advance..

John Conde
  • 217,595
  • 99
  • 455
  • 496
Joko Wandiro
  • 1,957
  • 1
  • 18
  • 28
  • Possible duplicate: http://stackoverflow.com/questions/852287/server-side-client-side-or-both-user-input-validation – Damiqib Dec 31 '10 at 06:25

3 Answers3

0

Both. Use JavaScript for the convenience of the user as you can report an error before they submit the form and have to wait for it to process. But never rely on that because evading JavaScript validation is as easy as turning JavaScript off. So always validate on the server side, too. If you're only going to use one then it should always be server side validation.

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • hmmm... that's mean double check for validation.. why validation always be terrible thing.. – Joko Wandiro Dec 31 '10 at 06:53
  • Double checking is never a bad idea. As a web developer you have to assume your users are either stupid or malicious. If you don't it's only a matter of time before someone compromises your site. And fixing that is ten times harder then validating their data. – John Conde Dec 31 '10 at 16:09
  • If you take some time, you can write a set of generic rules that can be used in both places. Then you just need to write some code in JS, and PHP that both know how to interpret the rules. Or you could send an ajax request to the backend that only returns validation errors, minimizing the transfer. – Chris Jan 03 '11 at 15:31
0

You should always validate user inputs at server-side as you should not trust user-inputs! Ever that is. ;) And as a bonus to that you can add client-side validation for user convenience.

Damiqib
  • 1,067
  • 2
  • 13
  • 23
0

Doing both wouldn't hurt, but make sure PHP is done if you can only do one. Javascript is optional for web-users, so go for the sure thing.

ryebr3ad
  • 1,228
  • 3
  • 12
  • 20