sphinx Posted October 6, 2011 Share Posted October 6, 2011 Hello, I'm currently using this code to check for blank fields: if(isset($_POST['submit'])) { $emailconfirm = $_POST['emailconfirm']; $user_message = $_POST['message']; $name = $_POST['name']; $visitor_email = $_POST['email']; if(empty($name)|| empty($visitor_email)|| empty($user_message)) { $errors .= "<div id='contact_form_errorloc' class='err1'>Some of the above fields have not been filled in.</div><br>"; } This however, does not check for valid emails, I've found this code: function is_valid_email($email) { $result = TRUE; if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) { $result = FALSE; } return $result; } My question was if the above is possible aswell as incorporating the blank field checks. Many thanks! Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 6, 2011 Share Posted October 6, 2011 Well, you could incorporate it into your current "global" validation - but the message would be problematic. Also, you need to trim() the post data - otherwise a field with only spaces would pass your validation. Plus, that function is using outdated functions. I prefer to individually check each field as appropriate and display error messages for all the errors. I assume the two email fields are supposed to match - so you need to validate that as well. function is_email($email) { $formatTest = '/^[\w!#$%&\'*+\-\/=?^`{|}~]+(\.[\w!#$%&\'*+\-\/=?^`{|}~]+)*@[a-z\d]([a-z\d-]{0,62}[a-z\d])?(\.[a-z\d]([a-z\d-]{0,62}[a-z\d])?)*\.[a-z]{2,6}$/i'; $lengthTest = '/^(.{1,64})@(.{4,255})$/'; return (preg_match($formatTest, $email) && preg_match($lengthTest, $email)); } if(isset($_POST['submit'])) { //Create array to track errors $errors = array(); //Pre-process post data $name = trim($_POST['name']); $visitor_email = trim($_POST['email']); $emailconfirm = trim($_POST['emailconfirm']); $user_message = trim($_POST['message']); //Validate post data if(empty($name)) { $errors[] = "Name is required"; } if(empty($user_message)) { $errors[] = "Message is required"; } if(empty($visitor_email) || empty($emailconfirm)) { $errors[] = "Email and confirmation are required"; } elseif($emailconfirm!=$emailconfirm) { $errors[] = "Email and confirmation do not match"; } elseif(!is_email($visitor_email)) { $errors[] = "Email is not valid"; } if(count($errors)) { //Create error message $errors .= "<div id='contact_form_errorloc' class='err1'>"; $errors .= "The following errors occured:\n"; $errors .= "<ul>\n"; foreach($errors as $error) { $errors .= "<li>{$error}</li>\n"; } $errors .= "<ul>\n"; $errors .= "</div><br>"; } else { //No errors occured } } Here is the full documentation of the email validation function I provided: // NOTES: // // Format test // - Username: // - Can contain the following characters: // - Uppercase and lowercase English letters (a-z, A-Z) // - Digits 0 to 9 // - Characters _ ! # $ % & ' * + - / = ? ^ ` { | } ~ // - May contain '.' (periods), but cannot begin or end with a period // and they may not appear in succession (i.e. 2 or more in a row) // - Must be between 1 and 64 characters // - Domain: // - Can contain the following characters: 'a-z', 'A-Z', '0-9', '-' (hyphen), and '.' (period). // - There may be subdomains, separated by a period (.), but the combined domain may not // begin with a period and they not appear in succession (i.e. 2 or more in a row) // - Domain/Subdomain name parts may not begin or end with a hyphen // - Domain/Subdomain name parts must be between 1-64 characters // - TLD accepts: 'a-z' & 'A-Z' (2 to 6 characters) // // Note: the domain and tld parts must be between 4 and 255 characters total // // Length test // - Username: 1 to 64 characters // - Domain: 4 to 255 character //===================================================== // Function: is_email ( string $email ) // // Description: Finds whether the given string variable // is a properly formatted email. // // Parameters: $email the string being evaluated // // Return Values: Returns TRUE if $email is valid email // format, FALSE otherwise. //===================================================== Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 6, 2011 Share Posted October 6, 2011 Why re-invent the wheel, if you're using PHP 5, if ( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == false ) { //email didn't validate } Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 7, 2011 Share Posted October 7, 2011 Why re-invent the wheel, if you're using PHP 5, True, but unfortunately there are still plenty of hosts that have not upgraded to PHP5. Quote Link to comment Share on other sites More sharing options...
sphinx Posted October 7, 2011 Author Share Posted October 7, 2011 Thanks, but geez that's more complicated than I thought :-\ Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 7, 2011 Share Posted October 7, 2011 Thanks, but geez that's more complicated than I thought :-\ No, it is not complicated - it is just structured. Trying to dump a bunch of validation into a single line is problematic. You stated you wanted to add the email validation into the one you already have. But, the error message states Some of the above fields have not been filled in. So, how would that message be appropriate if all the fields are filled in and the email validation fails? Plus, there are also the other problems with your code that I pointed out (e.g. not trimming the POST values). But, you are free to cut corners as you wish. if(empty($name)|| empty($visitor_email)|| empty($user_message) || !is_valid_email($visitor_email)) { $errors .= "<div id='contact_form_errorloc' class='err1'>Some of the above fields have not been filled in or the email is not valid.</div><br>"; } Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 7, 2011 Share Posted October 7, 2011 You can always do: try { if ( isset($_POST['submit']) ) { unset($_POST['submit']); $_POST = array_walk($_POST, 'trim'); if ( !empty(array_filter($_POST)) ) throw new Exception('You failed to fill out the '. str_replace('_', ' ', implode(', ', array_diff(array_filter($_POST), $_POST))) .' fields.'); if ( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == false ) throw new Exception('The email you have entered is not valid.'); } } catch ( Exception $e ) { $error = '<div id="contact_form_errorloc" class="err1">' . $e->getMessage() . '</div>'; } ?> <html> <!-- etc //--> <?php echo isset($error) ? $error : ''; ?> </html> Quote Link to comment Share on other sites More sharing options...
sphinx Posted October 7, 2011 Author Share Posted October 7, 2011 Cheers guys, although I'm still getting issues with the following: if(empty($name)|| empty($visitor_email)|| empty($user_message) || !is_valid_email($visitor_email)) { $errors .= "<div id='contact_form_errorloc' class='err1'>Some of the above fields have not been filled in or the email is not valid.</div><br />"; } function is_email($email) { $formatTest = '/^[\w!#$%&\'*+\-\/=?^`{|}~]+(\.[\w!#$%&\'*+\-\/=?^`{|}~]+)*@[a-z\d]([a-z\d-]{0,62}[a-z\d])?(\.[a-z\d]([a-z\d-]{0,62}[a-z\d])?)*\.[a-z]{2,6}$/i'; $lengthTest = '/^(.{1,64})@(.{4,255})$/'; return (preg_match($formatTest, $email) && preg_match($lengthTest, $email)); } Fatal error: Call to undefined function is_valid_email() in /home2/gold4fun/public_html/site/contents/index.php on line 23 Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 7, 2011 Share Posted October 7, 2011 Your test is trying to call the function is_valid_email(), but you used the function is_email(). Quote Link to comment Share on other sites More sharing options...
sphinx Posted October 7, 2011 Author Share Posted October 7, 2011 OMG, MY WHOLE SITE HAS BEEN OVERWRITTEN! STUPID CPANEL! I DIDNT KNOW THAT IF YOU MOVE A FILE OVER TO A FOLDER THAT HAS THAT FILE OF THE SAME NAME, ITS OVERWRITES IT WITHOUT WARNING! Quote Link to comment 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.