How do i validate form with PHP and JS?

Hi friend, i want to validate my forms with php and javascript. This is possible?

how can i make this?

thanks

Perhaps if you start by showing us your HTML…

Thanks. I am validate with HTML + PHP or with HTML + JS. No with HTML + PHP + JS. Do you have some tip?

Thanks.

Welcome to SitePoint.
You can use both.
PHP can only validate after the form has been submitted, when the targetted page loads. JS can validate either as the user types or when the user does something such as removing focus from that input.
It is common to use both methods.
Folk will be able to offer more advice if you show your HTML.

You must validate data on the server before you use it. Data can be submitted to your page from anywhere, can be anything, and cannot be trusted.

Client-side validation is a nicety for legitimate visitors only.

3 Likes

As it’s quite simple, I suggest you start by looking at HTML5 client-side validation as described here and elsewhere:
https://developer.mozilla.org/en-US/docs/Learn/Forms/HTML5_input_types

JavaScript validation is better if you wish to display your own error messages or if code is required to determine whether an input is valid.

At the server using PHP, it is important to sanitise the data received, especially if it is to go into a database. There was discussion in this forum thread on the use of FILTER_SANITIZE_STRING.

I would look first at HTML5 form validation, you can do a lot with minlength, maxlength, min, max, type and especially pattern

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.