mostafatalebi Posted March 3, 2013 Share Posted March 3, 2013 Hello everybody I need mobile number validation. Number "09" is fixed at the beginning. And it is followed by other numbers from 0 to 9. When I use this reg exp: "/^([09]+[0-9])$/" It fails But when I use this one: "/^([09]+[0-9])/" it works What's the problem? Is it okay to use it without "$"? Quote Link to comment https://forums.phpfreaks.com/topic/275194-why-this-reg-exp-for-mobile-number-does-not-work/ Share on other sites More sharing options...
requinix Posted March 4, 2013 Share Posted March 4, 2013 (edited) You need the $. Your expression says "at least one of [09] and then only one [0-9]". Does that sound right? [edit] And when you say "09 is fixed" do you mean literally 09 or either 0 or 9? Because your expression says the latter. Edited March 4, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/275194-why-this-reg-exp-for-mobile-number-does-not-work/#findComment-1416317 Share on other sites More sharing options...
mostafatalebi Posted March 4, 2013 Author Share Posted March 4, 2013 No. I mean the former. 09 and not 0 or 9. I have also another problem. How can I add the length limitation on it? There MUST be ONLY AND ONLY 11 characters of number and not more or less. this is true true format. Quote Link to comment https://forums.phpfreaks.com/topic/275194-why-this-reg-exp-for-mobile-number-does-not-work/#findComment-1416322 Share on other sites More sharing options...
AyKay47 Posted March 4, 2013 Share Posted March 4, 2013 A simple regex should be fine: <?php $pattern = "~^09\d{9}$~"; ?> While you can't guarantee that all numbers that run through this regex will be mobile numbers and not land lines, you can ensure that they conform to the pattern that you specify. Quote Link to comment https://forums.phpfreaks.com/topic/275194-why-this-reg-exp-for-mobile-number-does-not-work/#findComment-1416336 Share on other sites More sharing options...
Christian F. Posted March 4, 2013 Share Posted March 4, 2013 You might also want to read up on regular expressions, so that you know what each character and each element of that RegExp means. Should help you avoid mistakes like this in the future. Also, for PHP I generally recommend using \z instead of $ as the "end of string" match, seeing as the dollar sign really means "end of string, or the last newline before the end of the string". An alternative to using \z is using the D modifier with the dollar sign. Quote Link to comment https://forums.phpfreaks.com/topic/275194-why-this-reg-exp-for-mobile-number-does-not-work/#findComment-1416506 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.