I entered my flyer id as abc in input and stored it in $flyerIDs.
I want to validate that input does not contain anything except spaces, digits and comma and if it does throw error.
I have:
$error = preg_match("/[^\s,0-9]+/gi", $flyerIDs);
$error stores "". Don't understand why.
I don't think "g" is a valid modifier in PHP. Also, "i" would only be needed if matching letters.
$error = preg_match("/[^\s,0-9]+/", $ids);
You should use preg_match_all() instead of g modifier in PHP.
Related
I have this email regex, and i would like to make same thing with a name regex, so you can't write numbers in a name, how should i change it?
if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
}
I know that [a-z0-9_.-] means that a-z 0-9 and _ . - is usable.
But i want to only use a-z and this is why my brain is breaking across, as i don't understand the whole sentence, could anyone "translate" it to only use a-z and no numbers neither _ - .?
I would like it to be changed to a name regex, so people can't write numbers in their name.
I understand that i can write
if (!preg_match("/^[[:alnum:]][a-z]*#[a-z]+\.[a-z]{2,4}$/i", trim($_POST['name']))) {
$nameError = 'You entered invalid characters in your name.';
$hasError = true;
}
but for me it doesn't make any sense, how i should enter that into the regex above.
but i could also type in this? so i say if it contains 0-9 then its invalid? but i don't know what it means all these characters in the sentence.
if (preg_match("/^[[:alnum:]][0-9]*#[0-9]+\.[0-9]{2,4}$/i", trim($_POST['name']))) {
$nameError = 'You entered invalid characters in your name.';
$hasError = true;
}
I tried to research about "preg_match" but i can't find an explanation, so i can make my a regex for "preg_match" on my own
You could try the below.
/^[a-z]+#[a-z]+\.[a-z]{2,4}$/i
[[:alnum:]] Matches both alphabets and numbers but POSIX [[:alpha:]] matches only the alphabets.
[a-z]* zero or more lowercase letters. [a-z]+ one or more lowercase letters.
i modifier helps to do a case insensitive match.
If you want to understand the regex pattern try using http://regex101.com . That will let you analyze each part of the pattern.
You can try using this, it should do the job:
/^[[:alpha:]][a-z]*#[a-z]+\.[a-z]{2,4}$/i
But the simpler regex for a name check that I use and haven't had any issues with is:
/^([a-z])+$/i
Although the first one may be better (?) the second one makes the regex slightly more understandable in my opinion.
I mask the phone numbers like "(342) 004-1452" with jQuery. Now I am trying validate this input with PHP. I tried
if(!preg_match('\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}', $_POST['tel']))
{
$errors['tel']='enter a valid phone number';
}
But I think my preg_match expression is not valid. What is the right preg_match expression to validate this data?
You're all over-thinking it. All you truly need to do is verify that the given string contains the proper number of numeric characters.
$input = '(342) 004-1452';
$stripped = preg_replace('/[^0-9]/', '', $input);
if( strlen($stripped) != 10 ) {
printf('%s is not a valid phone number.', $input);
} else {
printf('%s is a valid phone number. yay.', $input);
}
//output: (342) 004-1452 is a valid phone number. yay.
You can pretty-fy the phone number back from whatever garbled input someone has fed it with:
$phone_pretty = sprintf('(%s) %s-%s',
substr($stripped,0,3),
substr($stripped,3,3),
substr($stripped,6,4)
);
Your regular expression is missing delimiters. Wrap it with /:
'/\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/'
If you've configured your environment to display warnings, you should be seeing one:
PHP Warning: preg_match(): Delimiter must not be alphanumeric or backslash
If you haven't turned on warnings, or have intentionally turned them off, you should stop developing PHP code until you turn them back on.
Your expression looks okay, but preg_match() needs you to supply delimiters for the start and end of the regular expression within the quotes.
These markers are typically slashes, but can actually be a number of other characters.
So adding slashes to your line of code gives the following:
if(!preg_match('/\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/', $_POST['tel']))
If you want the string to be only a phone number and nothing else, you may also want to limit the regex by adding ^ at the beginning and $ at the end:
if(!preg_match('/^\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/', $_POST['tel']))
Hope that helps.
(that said, I would add that the phone number format you're checking only applies for US and countries in the US dialling plan; most international countries use different formats, so if you want to accept international visitors, you'll need a much looser regex than this)
I want to place check in Full name field that full name field should accept space between first and last name using i am using strrpos() function for it but not working
You could use a regex...
if (preg_match("/(.+)( )(.+)/", $full_name))
{
// returns true if name is formed of two words with a space between
}
For even better validation, you can use \w although keep in mind that it will only match English word characters. See here for more info: Why does \w match only English words in javascript regex?
preg_match("/(\w+)( )(\w+)/", $full_name)
I'm using in Php Sanitize and Validate Filters but I have problems to add some rules, I have some basic knowledge of php so I think this question is easy for you.
if ($_POST['ccp_n'] != "") {
$ccp = filter_var($_POST['ccp_n'], FILTER_SANITIZE_NUMBER_INT);
if (!filter_var($ccp, FILTER_VALIDATE_INT)) {
$errors .= 'Insert a valid code.<br/>';
}
} else {
$errors .= 'Insert a code.<br/>';
}
I need to add a minimum and maximum number of characters (14-15) and I want to accept this characters ( - or space ) .The exact sequence is 0000-0000-0000 (the last four digits could be 5 too
Thanks
You can use preg_match and apply a regular expression.
preg_match ( string $pattern , string $TestString) See here in detail
The pattern is the problem. You need to define in detail what is allowed.
For example, the pattern:
'~^\d{4}-\d{4}-\d{4,5}$~D'
would be the whole string from start ^ to the end $. 4 digits, hyphen, 4 digits, hyphen, 4 to 5 digits.
See it here on Regexr
Update:
I added the D modifier to the end, otherwise the $ not only match to the end of the string, but also before a newline as last character in the string. See here for php modifiers in detail
Use a regular expression with preg_match(). Alternatively, you can also use sscanf() to parse the input from a string according to a format.
I am trying to validate a input field with regex with this pattern [A-Za-z]{,10}
I want it to only find a match if 10 or less chars was sent in the input field, the problem I get is that it will match all words that is less then 10 chars.
Is there a way to say if there is more then 10 chars in the input field come back as false, or is it just better to do a strlen with php?
If you need to validate that it's alphabetic only, don't use strlen(). Instead, put boundaries (^$) on your regex:
/^[A-Za-z]{,10}$/
There is an important syntax mistake being made here by the OP and all the current regex answers: When using the curly brace quantifier with PHP (PCRE), you need to specify the first number. (i.e. The expression: {,10} is NOT a valid quantifier!) Although the comma and second number are optional, the first number in a curly brace quantifier is required. Thus the expression should be specified like so:
if (preg_match('/^[A-Za-z]{0,10}$/', $text))
// Valid input
else
// Invalid input
If you just match ^[A-Za-z]{,10}$ it will check that the whole string is 10 or less.
^[A-Za-z]{,10}$
$##$^&$a36#&$^ will pass your regexp because a is substring of it and a pass [A-Za-z]{,10}
If you are only interested in the length of the string, regex is heavy-handed and you should use strlen instead.