Validating contact form

Hello,
I want to validate html contact form using PHP.
Please guide me.

Can you show us what you’ve done so far and what you’re specifically having problems with? That will allow us to give you more constructive replies rather than generic input validation methods.

Well it’s best to do a google search 'cos there are tone of good tutorials out there which can teach you a lot if you know the basics of PHP it want be much of challenge to learn. And yes try it your self first my friend and if you run in to an issue then ask at that point you’ve learned something also we’ll be willing to help you sort it out.

Well basic contact form consist of three main things.

  1. Name
  2. Email
  3. Message

To make every field mandatory use required key word. for example,

input name=“name” id=“name” type=“text” required>

will make your name filed mandatory. You can do this to every other fields too.

To validate Email address (use have to use this code in class where your submission method is been located )

$email = filter_var( $_POST[‘email’], FILTER_VALIDATE_EMAIL);

To validate name as a string

$name= filter_var($name, FILTER_SANITIZE_STRING);

Message filed validation defend on your requirement. either you can validate as normal string (this is same as name filed validation) but i strongly recommend below method as it check the special characters too (which can be use to harm your site)

$message = filter_var($_POST[‘message’],FILTER_SANITIZE_FULL_SPECIAL_CHARS)

Hope this will helpful
Happy codding :slight_smile: