Jump to content
samporras

Problems With Preg_match()

Recommended Posts

Hey guys.

 

I'm delving a little more into php and am creating a web form. While trying to create some simple validation, i came across the preg_match() function and am getting an error while implementing it.

 

below are the 2 function i created:

 

>	
	function CheckName($name)
{
  if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\]+$/s",$name))
	return TRUE;
  else
	return FALSE;
}

function CheckPhone($areaCode, $phonePrefix, $phoneSufix, $phone)
{
  if(!preg_match("/[^0-9\-\]+$/",$phone))
	return TRUE;
  elseif(strlen($areaCode) == 3)
	return TRUE;
  elseif(strlen($phonePrefix) == 3)
	return TRUE;
  elseif(strlen($phoneSuffix) == 4)
	return TRUE;
  else
	return FALSE;
}

 

i'm getting the following errors respectively when executing the code:

 

Warning: preg_match() [function.preg-match]: Compilation failed: missing terminating ] for character class at offset 31

 

Warning: preg_match() [function.preg-match]: Compilation failed: missing terminating ] for character class at offset 11

 

any help would be greatly appreciated :blush:

 

Sam

Share this post


Link to post
Share on other sites

Try

 

 

>		function CheckName($name)
{
  if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\]+$/s",$name))
   { return TRUE; }
  else
   { return FALSE; }
}

function CheckPhone($areaCode, $phonePrefix, $phoneSufix, $phone)
{
  if(!preg_match("/[^0-9\-\]+$/",$phone))
   { return TRUE; }
  elseif(strlen($areaCode) == 3)
   { return TRUE; }
  elseif(strlen($phonePrefix) == 3)
   { return TRUE; }
  elseif(strlen($phoneSuffix) == 4)
   { return TRUE; }
  else
   { return FALSE; }
}

Share this post


Link to post
Share on other sites

The backslashes before the closing ']' brackets in your expressions are causing them to be treated as literal ']' characters, rather than closing brackets. So

 

>if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\]+$/s",$name))

... should be ...

>if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü]+$/s",$name))

 

Same with the second preg_match()

Edited by click

Share this post


Link to post
Share on other sites

thanks click. worked like a charm!

 

Sam

Share this post


Link to post
Share on other sites

one more question...

 

I found the below function to validate an email address.

 

 

>function CheckEmail($email_field)
{
	if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9- ]+)*(\.[a-z]{2,3})", $email))
	{
		return TRUE;
	}
	else
		return FALSE;
}

 

Whenever the code is hit, i get the following error:

 

Warning: eregi() [function.eregi]: REG_ERANGE in /home/xxxx/xxxx/xxxx/contact.php on line 20

 

i tried escaping the quotation marks thinking it might be it, but i get a parse error when hitting that code.

Share this post


Link to post
Share on other sites

In '[_a-z0-9-]', the last '-' is being seen as defining a range rather than a literal dash. Move it to the beginning to prevent this. So it would be: [-_a-z0-9]

Share this post


Link to post
Share on other sites
In '[_a-z0-9-]', the last '-' is being seen as defining a range rather than a literal dash. Move it to the beginning to prevent this. So it would be: [-_a-z0-9]

 

I attempted to do this and it didn't work. After looking at the expression a little bit more, i noticed I had an extra space after the '-' so it looked like this:

 

[_a-z0-9- ]

 

Was the extra space the reason it was interpreted as defining a range?

 

The code seems to be functioning properly now after removing the space.

 

Thanks again!:thumbup1: I was stuck for hours!!

Share this post


Link to post
Share on other sites
I attempted to do this and it didn't work. After looking at the expression a little bit more, i noticed I had an extra space after the '-' so it looked like this:

Was the extra space the reason it was interpreted as defining a range?

 

The code seems to be functioning properly now after removing the space.

 

Thanks again!:thumbup1: I was stuck for hours!!

Could have been, I'm not really sure. I just knew it worked with it at the beginning. :)

Share this post


Link to post
Share on other sites

At present I am using this bit of code I found to check email addresses.

 

>   function check_emailaddress($email)
  {
  $addr_spec = '([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
		'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d'.
		'\\x22\\x5c\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x22)'.
		'(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e'.
		'\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|'.
		'\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c\\x00'.
		'-\\x7f)*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28'.
		'\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d'.
		'\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff'.
		']|\\x5c[\\x00-\\x7f])*\\x5d)(\\x2e([^\\x00-\\x20'.
		'\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40'.
		'\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-'.
		'\\x5d\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x5d))*';
		
  if (preg_match("!^$addr_spec$!i", $email))
  {
	   return true;
  }
  else
  {
	 return false;
  }
  }

 

thing is I swear I found a better bit of code and had put it in Lazarus but it's not there now. I'm always cocking up like that.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

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