Jump to content

Regex for Middle Initial


doubledee

Recommended Posts

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.';
        }
    }
?>

Link to comment
Share on other sites

Hi, "The Letter E"!!  :D

 

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

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.