Jump to content

[SOLVED] ereg() help


Dragen

Recommended Posts

Hi, I'm trying to use ereg() to check if an input from a form contains certain characters. It's an english phone number so I want to check that it only contains numbers (0-9) and it may then have a space followed by more numbers..

I'm not quite sure how to work this as I don't understand the expressions very well.. What I've got at the moment is:

if(ereg('[^0-9]', $value)){

do this code etc, but i need to check for the space, then more numbers that may or may not be there.

I've tried various things like:

if(ereg('^[0-9].[0-9]', $value)){

 

can't get it to work...

thanks

Link to comment
https://forums.phpfreaks.com/topic/51834-solved-ereg-help/
Share on other sites

Do you have any specific formats in mind? Below is some code I posted a while back:

 

<pre>
<?php
$phones = array(
	'(123) 456-7890',
	'456-7890',
	'(123) 456-7890 x1234',
	'456-7890 x1234'
);

foreach($phones as $num) {
	preg_match('/^(?:\(([0-9]{3})\)\s+)?([0-9]{3})-([0-9]{4})(?:\s+x([0-9]{4}))?$/', $num, $matches);
	echo "matches on: $num";
	print_r($matches);
}
?>
</pre>

Link to comment
https://forums.phpfreaks.com/topic/51834-solved-ereg-help/#findComment-255473
Share on other sites

Thanks for the reply. Basically I want it to be able to accept an input which is all numbers, and also an input which is all numbers, but has a single blank space somewhere in it.. for example:

01769 569251
01769569251

should both be acceptable

..but:

01769a569251
01769  569251 //more than one space
01769abcd569251

should not be acceptable

Link to comment
https://forums.phpfreaks.com/topic/51834-solved-ereg-help/#findComment-255742
Share on other sites

hmm.. I guess not. It's more for the display of it, but I guess I could add in the space after so many characters when I display the code, and force the phone numbers to be entered as one strip of continuous numbers.

so I guess this would force it to be just numbers:

if(ereg('[^0-9+]', $value)){

If I want it to be only a certain amount of numbers would it be something like this:

if(ereg('[^0-9{10}]', $value)){

where 10 is the amount of numbers?

Link to comment
https://forums.phpfreaks.com/topic/51834-solved-ereg-help/#findComment-255807
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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