php preg_match

Associate
Joined
19 Jul 2006
Posts
1,847
I have the following code that checks to see that the email is correct.

How do i alter the phone one to check that it contains at least 9 numbers?

PHP:
//Check to make sure sure that a valid email address is submitted
		if(trim($_POST['email']) === '')  {
			$hasError = true;
		} else if (!preg_match('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$^', trim($_POST['email']))) {
			$hasError = true;
			$errorMessage = "Please enter a valid email address!";
		} else {
			$email = trim($_POST['email']);
		}

		//phone
		if(isset($_POST['phone'])) $phone = trim($_POST['phone']);

TIA
 
PHP:
	if (trim($_POST['phone'] === ''))
	{
		$hasError = true;
    }
	else if (!preg_match('(\d{9})', trim($_POST['phone'])))
			{
				$haserror = true;
				$errorMessage = "Your phone number must have 9 digits";
			}
			else
			{
				$phone = trim($_POST['phone']);
			}


	print $errorMessage.'<br>';  // Dont need this line

I just tried this out and it worked for me. Hope that helps.
 
Just checked that site which is good but im now unsure of the {} on the site is says
{9} means exactly 9

Now in the code above {9} seams to work with 9 or above?
 
Just checked that site which is good but im now unsure of the {} on the site is says
{9} means exactly 9

Now in the code above {9} seams to work with 9 or above?

Hi Hargi,

Yes, you are right, the {9} picks up with 9 or above. :eek:

So I replaced it with (\A\d{9}\Z) That seems to work. :)
 
Back
Top Bottom