music_fan01 Posted September 11, 2011 Share Posted September 11, 2011 I am really new to using php validation, I think I may be on to what I am looking for but not very sure. I am trying to validate my form fields just incase someone forgets (name, subject, message, and email). Here is what I have so far. I was looking at an example on how to validate a phone number. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Contact</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <?php $first_name=$_POST['name']; $email_address=$_POST['email']; $subject=$_POST['subject']; $message=$_POST['text']; if(isset($_GET['submit'])) { if(preg_match("/^\(([ $_GET['email']) != ) { echo "The email field was invalid<BR>"; } else if(isset($_GET['submit'])) { if(preg_match("/^\(([ $_GET['name']) != "") { echo "The name field was invalid<BR>"; } else if(isset($_GET['submit'])) { if(preg_match("/^\(([ $_GET['subject']) != "") { echo "The subject field was invalid<BR>"; } else { mail("[email protected]","Subject: $subject", $message, "From: $first_name <$email_address>"); echo "Thank you for using our mail form.<br/>"; echo "Your email has been sent."; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/246878-validation/ Share on other sites More sharing options...
music_fan01 Posted September 11, 2011 Author Share Posted September 11, 2011 I found this is another example, this is something similar that I would like to add to my php. return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email); Quote Link to comment https://forums.phpfreaks.com/topic/246878-validation/#findComment-1267883 Share on other sites More sharing options...
music_fan01 Posted September 11, 2011 Author Share Posted September 11, 2011 This is what I gathered from a few examples that I found, but when I run it, no error messages show up. <?php $first_name=$_POST['name']; $email_address=$_POST['email']; $subject=$_POST['subject']; $message=$_POST['text']; //Determine if the sumbit button has been clicked. If so, begin validating form data. if ($_POST['submit'] == "Submit") { //Determine if a Name was entered $valid_form = true; if ($_POST['name'] == "") { echo "Enter your name"; $valid_form = false; } else { $first_name = $_POST['email']; } if ($_POST['email'] == "") { echo "Enter a valid email address"; $valid_form = false; } else { $email_address = $_POST['email']; } if ($_POST['subject'] == "") { echo "Enter a subject"; $valid_form = false; } else { $subject = $_POST['subject']; } if ($_POST['text'] == "") { echo "Enter a message"; $valid_form = false; } elseif (strlen($_POST['text']) < 4) { echo "Enter a message"; $valid_form = false; } else { $message = $_POST['text']; } //if all form fields were submitted properly, begin processing if($valid_form == true) { mail("[email protected]","Subject: $subject", $message, "From: $first_name <$email_address>"); echo "Thank you for using our mail form.<br/>"; echo "Your email has been sent."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246878-validation/#findComment-1267888 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.