5 Replies - 10343 Views - Last Post: 23 December 2005 - 10:30 PM Rate Topic: -----

#1 theRemix  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 440
  • Joined: 19-October 05

eregi for email or phone number

Posted 23 December 2005 - 03:08 PM

i want a single form to have to be a valid email address or a valid phone number.

if(@eregi("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $_POST['contact']) || @eregi("^([0-1]([\s-./\\])?)?(\(?[2-9]\d{2}\)?|[2-9]\d{3})([\s-./\\])?(\d{3}([\s-./\\])?\d{4}|[a-zA-Z0-9]{7})$", $_POST['contact']) ){
  $success = true;
	}else{
  $success = false;
	}


i took the phone number regex from regexlib.com, this should work, but it doesn't.

:crazy:

Is This A Good Question/Topic? 0
  • +

Replies To: eregi for email or phone number

#2 Wizzy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 408
  • Joined: 20-November 05

Re: eregi for email or phone number

Posted 23 December 2005 - 05:49 PM

When you remove the error hider, you'll get Warning: REG_ERANGE, which means there is a mistake in the regular expression, i'll go over it but this looks wrong to me: [-0-9a-zA-Z]
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: eregi for email or phone number

Posted 23 December 2005 - 06:59 PM

Email is easy enough...for the phone validation, what, if any, limits do you wish to set? 10 digit dialing? 7? international? Possibility of all of them?

You may be better off using a couple of variables for each pattern (one for phone, one for email)...then performing both tests...if both come back as a fail, you know it is neither a phone number or an email address.

$myPattern = "[0-9]{3}-[0-9]{3}-[0-9]{4}";


can work for the standard 10 digit dialing (you can modify based on what patterns you'll accept). Do another for email
$myPatternEmail = "^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$";


, test'em both, check the return status, and you're in business! (in theory) :)

Regex are your friends...

Also, I'm sure you're aware, but preg_match() is also available, and often faster (also, it supports perl regex syntax, and perl is the king of regex).
Was This Post Helpful? 0
  • +
  • -

#4 cyberscribe  Icon User is offline

  • humble.genius
  • member icon

Reputation: 10
  • View blog
  • Posts: 1,062
  • Joined: 05-May 02

Re: eregi for email or phone number

Posted 23 December 2005 - 10:00 PM

Yep ... defintely recommend PCRE over POSIX regex.
Was This Post Helpful? 0
  • +
  • -

#5 theRemix  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 440
  • Joined: 19-October 05

Re: eregi for email or phone number

Posted 23 December 2005 - 10:01 PM

hey thanks guys, amadeus that worked great!!!

i tweaked it a bit, hows this?

if(@eregi("\(?[0-9]{3}\)?-?[0-9]{3}-?[0-9]{4}", $_POST['contact']) || @eregi("[0-9]{3}-?[0-9]{4}", $_POST['contact']) )


so that (808)999-5555 || 808-999-5555 || 99955555 all match :)
Was This Post Helpful? 0
  • +
  • -

#6 Wizzy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 408
  • Joined: 20-November 05

Re: eregi for email or phone number

Posted 23 December 2005 - 10:30 PM

I guess your email test is seperate from that function
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1