Forum Moderators: coopster

Message Too Old, No Replies

ereg for phone number format #*$!-xxx-xxx not working

         

stidj

3:33 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



Hi guys

I have tried using "[0-9]{3}-[0-9]{3}-[0-9]{4}" but this does not work as intended and seems to allow nearly anything to match it for some reason.

Does anyone know how to match it for only numbers in format #*$!-xxx-xxxx?

Thanks

vincevincevince

3:39 pm on Aug 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the - between needs escaping to \-

[0-9]\-[0-9] etc

stidj

5:02 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



I'll give that a try later on (all the examples I've seen don't escape but then again all of the example code doesn't work as the tutorials claim) :)

Thanks vince.

dreamcatcher

6:34 pm on Aug 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this any good?

if (ereg("^[0-9)( - ]{7,20}(([xX] ¦ (ext) ¦ (ex))?[ -]?[0-9]{1,7})?$", $phone))

{

//valid phone no

}
else
{

//not valid

}

dc

stidj

12:48 am on Aug 5, 2005 (gmt 0)

10+ Year Member



Unfortunately both ideas don't work for some reason.

I'm using PHP 4.3.11 might there an issue with it?

coopster

1:00 am on Aug 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



stidj,

It may help if you show us what the string looks like that you are searching as well as your ereg line of code. For example, is your string just the phone number or is it embedded in a longer string of data?

stidj

2:24 am on Aug 5, 2005 (gmt 0)

10+ Year Member



Here it is. I have tried other combinations such as a "^" at the beginning and a "$" at the end

thx again all

if ( ereg("[0-9]{3}-[0-9]{3}-[09]{4}",$number) )
{
return 1;
}
else
{
return 0;
}

stidj

2:25 am on Aug 5, 2005 (gmt 0)

10+ Year Member



Sorry and I am also submitting "905-913-8911" as the string

stidj

2:50 am on Aug 5, 2005 (gmt 0)

10+ Year Member



Ok, guys it was my mistake. The error all along was the way I used the variable returned in a function ......

if ( ereg("^[0-9]{3}-[0-9]{3}-[0-9]{4}$",$number) )

That above works just as expected and only allows numbers in the format #*$!-xxx-xxxx

ergophobe

3:15 pm on Aug 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I was going to say, there's no reason that shouldn't work.


I think the - between needs escaping to \-

[0-9]\-[0-9] etc

The "-" only has a meaning inside a character class like [0-9]. Outside a character class it shouldn't need escaping.