Dragen Posted May 17, 2007 Share Posted May 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 17, 2007 Author Share Posted May 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted May 17, 2007 Share Posted May 17, 2007 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> Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 17, 2007 Author Share Posted May 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted May 17, 2007 Share Posted May 17, 2007 That could be a little tricky. Is the whitespace even important? It might be better to strip all of the whitespace and see how many numbers you have left. Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 17, 2007 Author Share Posted May 17, 2007 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? Quote Link to comment Share on other sites More sharing options...
effigy Posted May 17, 2007 Share Posted May 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 18, 2007 Author Share Posted May 18, 2007 okay so would this work? if(ereg('^[0-9]{10}$', $value)){ Quote Link to comment Share on other sites More sharing options...
effigy Posted May 18, 2007 Share Posted May 18, 2007 Yes; that looks for exactly 10 numbers. Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 18, 2007 Author Share Posted May 18, 2007 Thanks.. I've spiced it up a bit so it's now: if(!ereg('^([0-9]{11}|[0-9]{5}[[:space:]][0-9]{6})$', $value)) which works great! I found out that [[:space:]] is used for white space. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.