doubledee Posted April 1, 2011 Share Posted April 1, 2011 Is this the correct syntax to check for an optional Middle Initial... if (preg_match('/^?[A-Z]$/i', $_POST['middleInitial'])){ Thanks, Debbie Quote Link to comment Share on other sites More sharing options...
The Letter E Posted April 1, 2011 Share Posted April 1, 2011 Is this the correct syntax to check for an optional Middle Initial... if (preg_match('/^?[A-Z]$/i', $_POST['middleInitial'])){ Thanks, Debbie Is that post variable a field that only takes the middle name input? If so, all you need to do is: <?php if(!empty($_POST['middleInitial'])){ $middleInitial = $_POST['middleInitial']; //You don't really need the question mark unless the expression is matching a full name and middle is optional if(preg_match('/^[A-Z]$/i', $middleInitial)){ echo 'Middle initial matched and is case insensitive.'; } } ?> Quote Link to comment Share on other sites More sharing options...
doubledee Posted April 1, 2011 Author Share Posted April 1, 2011 Hi, "The Letter E"!! Is that post variable a field that only takes the middle name input? If so, all you need to do is: <?php if(!empty($_POST['middleInitial'])){ $middleInitial = $_POST['middleInitial']; //You don't really need the question mark unless the expression is matching a full name and middle is optional if(preg_match('/^[A-Z]$/i', $middleInitial)){ echo 'Middle initial matched and is case insensitive.'; } } ?> I seem to be really struggling with this concept... Here is my loopy coding style... <?php // Initialize variables. $firstName = $middleInitial = $lastName = $name = ''; $address1 = $address2 = $address = ''; $errors = array(); if (isset($_POST['submit'])){ // Handle Form. // Trim all incoming data. $trimmed = array_map('trim', $_POST); // Validate Form Data. // Check First Name. if (preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['firstName'])){ $firstName = $_POST['firstName']; }else{ $errors['firstName'] = 'Enter a valid First Name. (e.g. 2-20 letters, ".", "-")'; } // Check Middle Initial. if (preg_match('/^[A-Z]?$/i', $_POST['middleInitial'])){ $middleInitial = $_POST['middleInitial']; }else{ $errors['middleInitial'] = 'Enter a valid Middle Initial. (e.g. A-Z)'; } and so on... I guess I am doing it the wrong way with initializing all of my variables, huh? Debbie Quote Link to comment Share on other sites More sharing options...
doubledee Posted April 1, 2011 Author Share Posted April 1, 2011 The Letter E, This is how I was doing my data validation before... <?php // Initialize variables. $firstName = $middleInitial = $lastName = $name = ''; $address1 = $address2 = $address = ''; $errors = array(); if (isset($_POST['submit'])){ // Handle Form. // Trim all incoming data. $trimmed = array_map('trim', $_POST); // Validate Form Data. // Check Middle Initial. if (preg_match('/^[A-Z]?$/i', $_POST['middleInitial'])){ $middleInitial = $_POST['middleInitial']; }else{ $errors['middleInitial'] = 'Enter a valid Middle Initial. (e.g. A-Z)'; } } ?> However I tried to use your code and came up with this... <?php // Check Middle Initial. (NEW & IMPROVED!!!) if (!empty($_POST['middleInitial'])){ if (preg_match('/^[A-Z]?$/i', $_POST['middleInitial'])){ $middleInitial = $_POST['middleInitial']; }else{ $errors['middleInitial'] = 'Enter a valid Middle Initial. (e.g. A-Z)'; } } ?> Does that look better as far as coding style?? Thanks, Debbie Quote Link to comment Share on other sites More sharing options...
The Letter E Posted April 1, 2011 Share Posted April 1, 2011 I hear you. Try something like this: <?php // Initialize variables - this is assuming they are posted from a form //firstName set var $firstName = $_POST['firstName']; //middleInitial - set var $middleInitial = $_POST['middleInitial']; //lastName - set var $lastName = $_POST['lastName']; //Concatonate names together, separating by spaces $fullName = $firstName.' '.$middleInitial.' '.$lastName; //Full name PCREgex - this will check the entire name - case insensitive //^[A-Z][A-Za-z-]*\s Checks for firstname syntax followed by a space \s - and is at the beginning of the string ^ //([A-Z][A-Za-z-]\s)? Checks for optional middle initial followed by a space \s //[A-Z][A-Za-z-]*$ Checks for last name syntax - and is at the end of the string $ //The entire expression is enclosed in / / and the i at the end sets case insensitive mode $fullNamePattern = '/^[A-Z][A-Za-z-]*\s([A-Za-z]\s)?[A-Z][A-Za-z-]*$/i'; if(preg_match($fullNamePattern, $fullName)){ echo 'Your name was successfully matched!'; }else{ //error catching... } ?> I didn't test this, but it's a nudge in the right direction. Also, check out: http://www.regular-expressions.info/reference.html http://www.phpf1.com/tutorial/php-regular-expression.html <------- this one is really easy to follow http://weblogtoolscollection.com/regex/regex.php/ Keep trying, keep posting and most importantly, stay positive! Google is your friend if you use it right Quote Link to comment Share on other sites More sharing options...
The Letter E Posted April 1, 2011 Share Posted April 1, 2011 The Letter E, This is how I was doing my data validation before... <?php // Initialize variables. $firstName = $middleInitial = $lastName = $name = ''; $address1 = $address2 = $address = ''; $errors = array(); if (isset($_POST['submit'])){ // Handle Form. // Trim all incoming data. $trimmed = array_map('trim', $_POST); // Validate Form Data. // Check Middle Initial. if (preg_match('/^[A-Z]?$/i', $_POST['middleInitial'])){ $middleInitial = $_POST['middleInitial']; }else{ $errors['middleInitial'] = 'Enter a valid Middle Initial. (e.g. A-Z)'; } } ?> However I tried to use your code and came up with this... <?php // Check Middle Initial. (NEW & IMPROVED!!!) if (!empty($_POST['middleInitial'])){ if (preg_match('/^[A-Z]?$/i', $_POST['middleInitial'])){ $middleInitial = $_POST['middleInitial']; }else{ $errors['middleInitial'] = 'Enter a valid Middle Initial. (e.g. A-Z)'; } } ?> Does that look better as far as coding style?? Thanks, Debbie Don't worry too much about style at this point. Once you have a comfortable grasp on the basic php functions and syntax, your style will develop. It's more important that you understand how to effectively use the vast array of tools php provides. Make sure to study up on php.net as well. Quote Link to comment Share on other sites More sharing options...
The Letter E Posted April 1, 2011 Share Posted April 1, 2011 This is a cool site: http://www.regextester.com/ 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.