Jump to content

Javascript vs PHP for Form Validation?


php_guy

Recommended Posts

Let's say I have a form with 5 fields to fill out

 

Is it better practice to use Javascript to ensure that all fields are non-empty, a certain length, contain certain characters, etc. Or should the back end PHP side of things handle this, then return an error code to be printed out, etc

 

Thanks!

Link to comment
Share on other sites

You should always use PHP regardless.  Why?  Because JavaScript can be turned off in the browser.  That said, you can use JavaScript on top of PHP, as an extra layer to add more responsiveness for the user.  The typical way to do it is to run JavaScript validation first.  If things check out, then the PHP script will take a hold of the form data and do its own validation on it.  If the JavaScript validation finds something wrong, it can alert the user immediately without sending the data on to the back end.

Link to comment
Share on other sites

You should always use PHP regardless.  Why?  Because JavaScript can be turned off in the browser.  That said, you can use JavaScript on top of PHP, as an extra layer to add more responsiveness for the user.  The typical way to do it is to run JavaScript validation first.  If things check out, then the PHP script will take a hold of the form data and do its own validation on it.  If the JavaScript validation finds something wrong, it can alert the user immediately without sending the data on to the back end.

Ahhhh that makes sense, and sounds very thorough.

 

So basically program your PHP as if there is no Javascript -- but optionally include the javascript anyway, as a means to reduce server hits for something as simple as a missing field (but don't rely solely on this)

 

Is that sort of the rationale?

 

Thanks

 

Link to comment
Share on other sites

So basically program your PHP as if there is no Javascript -- but optionally include the javascript anyway, as a means to reduce server hits for something as simple as a missing field (but don't rely solely on this)

 

Is that sort of the rationale?

 

Not exactly. The point is primarily avoiding bogus data. It is very easy to turn off javascript and enter anything (or nothing) and submit a form. spam bots don't even use javascript, so they will send you as much email as you can handle without thinking about any browser-based validation.

Link to comment
Share on other sites

  • 2 months later...

Javascript validation allows your user to be abreast of any errors above-mentioned to their appointment the anatomy to the server. This saves acid page-reloads back on abide the JS catches the accident and validates the form, preventing form-submission if errors are begin and minimises the affairs of their accepting to re-enter advice again, or abrogation above-mentioned to commutual the anatomy properly. JS validation is not a acting for server-side validation (since the user can see the JS, and, by extenuative the page and alteration the JS do whatever they want); but it's a accessibility for them.

Link to comment
Share on other sites

Javascript validation allows your user to be abreast of any errors above-mentioned to their appointment the anatomy to the server. This saves acid page-reloads back on abide the JS catches the accident and validates the form, preventing form-submission if errors are begin and minimises the affairs of their accepting to re-enter advice again, or abrogation above-mentioned to commutual the anatomy properly. JS validation is not a acting for server-side validation (since the user can see the JS, and, by extenuative the page and alteration the JS do whatever they want); but it's a accessibility for them.

:wtf:

 

 

Nightslyr is right on the money. Javascript can't be considered validation at all. It isn't anything more than a convenience to the user (when properly done).

I'm thinking hotmail as a good example of when it's not :D

Link to comment
Share on other sites

Javascript validation allows your user to be abreast of any errors above-mentioned to their appointment the anatomy to the server. This saves acid page-reloads back on abide the JS catches the accident and validates the form, preventing form-submission if errors are begin and minimises the affairs of their accepting to re-enter advice again, or abrogation above-mentioned to commutual the anatomy properly. JS validation is not a acting for server-side validation (since the user can see the JS, and, by extenuative the page and alteration the JS do whatever they want); but it's a accessibility for them.

 

Don't resurrect dead threads.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.