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
Share on other sites

okay I've pretty much figured it out now..

I'm currently using this code:

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

 

But as I'm using the '.' it means that any single letter or number can be inserted here.. I want to make it so it can only be a single empty space.

Link to comment
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
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
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
Share on other sites

Almost. The quantifiers go behind the brackets: [...]+ or [...]{#}; and don't use ^ inside the brackets unless you want negation. You'll also want ^ and $ around your pattern to anchor it to the entire string.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

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

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.