php field validation?

office politics

It's all just 1s and 0s
Messages
6,555
Location
in the lab
currently, im coding field validation in the frontend and backend. I'm mainly using javascript for the frontend and php for the backend. This seems redundant to me.

Do you have any tactics for making field validation easier? validations such as required fields or syntax.
 
Validating with both PHP and JS is a good way to do it because you can check for empty fields using JavaScript, and if they are, you return false to the onSubmit event to prevent the data from being sent to the server. That way you prevent unnecessary calls to the server. I'd still check for this using PHP using the function which swibit has stated just in case a user does switch their JavaScript off. Firstly you would want to use the trim() function to make sure you get rid of whitespace as they could possibly enter just spaces and pass your validation.

Something like:
PHP:
if(isset($_POST['field_name']))
{
     $_POST['field_name'] = trim($_POST['field_name']);
     //do for all fields.

     if(empty($_POST['field_name']))
     {
             //send them back to form witherror.
     }
}
Ok late for dinner :p

Hope this helps.

Kind regards,

Lab.
 
Last edited:
Back
Top Bottom