I need to check that a string
contains only digits and
the first digit is 0, and
the whole string has at least 8 digits and maximum 25
I have tried this, but it doesn't work:
if (!preg_match('/^[0][0-9]\d{8-25}$/', $ut_tel))
Try this regex:
/^0\d{7,24}$/
It seemed to work here.
Use this regex pattern
^(0[0-9]{7,24})$
Demo
If I was you, I'd do each check separately. This way you won't have issues in the future, should you decide to remove one of them or add additional checks.
(Note to all potential downvoters: I realize the question asked for a regex way, but since that was already provided in other answers - I think it is good to have a different approach as an answer as well.)
function validateNumber($number){
if(!is_numeric($number))
return false;
if(strlen($number)>25 || strlen($number)<8)
return false;
if(substr($number, 0, 1) != 0)
return false;
return true;
}
var_dump(validateNumber('0234567')); // shorter than 8
var_dump(validateNumber('02345678')); // valid
var_dump(validateNumber('12345678')); // doesn't start with 0
var_dump(validateNumber('02345678901234567890123456')); // longer than 25
Related
I have referred to several SO webpages for the answer to my question, but I keep reading that regex should not be used for validating numbers which are less than or greater than a certain range. I want to ensure that a user enters numbers within the following ranges: 11--20 and 65-100. Anything less than 11 will not be allowed, anything between 21 and 64 will not be allowed and anything from 101 above will not be allowed. I realize I can write something like
if ($num <=10 and $num >= 21 and $num <=64 and $num >=101) {
$num = "";
$numErr = "Number must be within specified ranges";
}
But what I really want is to use regex to preclude the range of numbers I do not want from being entered but I have not seen any satisfactory answers on SO. Can someone please help?
The regex would be less readable but like
/^(1[1-9]|20|6[5-9]|[7-9][0-9]|100)$/
Regex Demo
I'm having some trouble with Regex never really used it. However basically I'm trying to set a limit on my font-size bbcode tag.
class SizeValidator implements \JBBCode\InputValidator
{
public function validate($input)
{
return (bool) preg_match('regex', $input);
}
}
If someone can help with the regex that'll be perfect! Basically just want Size 7 to 30 max, no px, em, nothing strictly numbers max 2 numbers if anyone with regex experience would be quite helpful possibly explain how it works so I can improve and get a better understanding :)
There really is no reason to use regular expressions here.
Simply verify that what you're getting is a sequence of digits (for instance using ctype_digit, and that the value lies between 7 and 30.
class SizeValidator implements \JBBCode\InputValidator {
public function validate($input) {
return ctype_digit($input) && $input >= 7 && $input <= 30;
}
}
It's much more readable and easier to modify if need be.
You could try something like this:
return (bool)preg_match('/\[size=(([7-9])|([1-2]\d)|(30))\](.*)?\[\/size\]/', $input);
First I am matching if the number is 7-9, if so your function returns true.
([7-9])
Else if your number is with two digits starting with either 1 or 2 then the function also returns true
([1-2]\d)
Or else I check if the number is 30 and return true.
I am looking to generate a series of random numbers that have a difference of at least 2 from the previous number that was generated. I thought that a simple function that would call itself would be the way to go - see code below...
function getRandomLength($previous){
$x = rand(1,12);
if(abs($x - $previous) > 2){
return $x;
} else {
getRandomLength($previous);
}
}
For whatever reason, this is not working out the way that I had hoped it would. Any help would be appreciated.
Thanks.
And for those wondering why I want random numbers that are slightly different, I'm building a deck. I need to cut the decking boards and I don't want the joint to line up, or have any perceivable pattern to them, hence, I turn to my trusty random number generator to help out...
Two problems here:
function getRandomLength($previous){
$x = rand(1,12);
if(abs($x - $previous) > 2){
First problem is here - you use > 2 when you meant >= 2, e.g. if the difference is two or more then it's fine.
return $x;
} else {
getRandomLength($previous);
Second problem is here - you call the method again, but you do not return the result of calling it, so the result of the method will be an un-useful null.
Also, you should not code the method to be recursive, it should be iterative, as it doesn't need recursive logic.
}
}
Since you need an offset of at least 2, pick a random number starting from 0 and add 2 to it. Since it's an offset, you add it to the previous value (but I'm sure you could figure that out).
I need some help on a REGEX in php (Symfony).
I want to match values 1 to 60 or string all.
For number I've use this : ^([1-5]?[0-9]|60) but It match 0 ... And I don't now how can match "all".
Can you help me ?
Many thanks before
You should be able to divide it into possibilities as follows:
^([1-9]|[1-5][0-9]|60|all)$
This gives you four possibilities:
[1-9] the single-digit values.
[1-5][0-9]: everything from ten to fifty-nine.
60: sixty.
all: your "all" option.
But keep in mind that regular expressions are not always the answer to every question.
Sometimes they're less useful for complicated value checks (though, in this case, it's a fairly simple one). Something like the following (pseudo-code):
def isAllOrOneThruSixty(str):
if str == "all":
return OK
if str.matches ("[0-9]+"):
val = str.convertToInt()
if val >= 1 and val <= 60:
return OK
return BAD
can sometimes be, while more verbose, also more readable and maintainable.
This will match all you need
^([1-9]|[1-5]\d|60|all)$
You are making the [1-5] optional; turn it around, like so: [1-5][0-9]?. Also you need to cover the single-digit [6-9].
The problem is that you are missing some parenthesis and you should switch the 60 and the rest, because otherwise it will only match the 6 in 60:
^((60|([1-5]?[0-9]))|all)
I am trying to check, if a user has created a password that contains symbols (including +,-,= signs) OR/AND numbers.
How can I do that?
function check_password($str)
{
if (!preg_match ("/[&#<>%\*\,\^!#$%().]/i", $str))
{
$this->form_validation->set_message('check_password', 'Your password should contain a number,letter,and special characters"');
return FALSE;
}
else
{
return TRUE;
}
}
Thanks.
My suggestion for password strength checking is to break each requirement you have into separate regexs, particularly:
$regexs = array(
'/[0-9]+/', // Numbers
'/[a-z]+/', // Lower Case Letters
'/[A-Z]+/', // Upper Case Letters
'/[+-=]+/', // Your list of allowable symbols.
);
Initialize a counter to 0. For each regex, test to see if it matches. If it does, increase the counter by 1. Then return true if counter is greater than 3 (or 4 if you want them to have a really really strong password), otherwise return false.
I think this would be much more maintainable for you in the long run if your requirements ever change.
You don't have to list all those characters. Anything that is not a number or a letter is a special character (or space):
(?:[^0-9A-z]|[0-9])
I recommend looking in a different direction. If you are to impose a restriction, just require it to be longer. As has been pointed out, using symbols will most likely cause the user to forget the password and never come back to your site. For maximum security post the following comic on the registration page: http://xkcd.com/936/