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.
Related
I'm trying to find a good preg_match expression that allow all letters (included accented letters), the space , and these symbols: - '
I thought this could be a good solution '/^\p{L}+$/ui', but I can't find a successful way to add the symbols I need, if that is possible...
Otherwise I guess I should use something like this:
'#^[a-zA-Z0-9àòäöüÄÖÜ \.\]]+$#'
but there are so many accents that I was hoping to find a better solution.
Try placing the \p{L} and allowed symbols inside brackets:
$name = "B-jör n'Bòrg";
if (preg_match("/^[- '\p{L}]+$/u", $name)) {
echo "$name is a valid name!"; // It is
}
You may also want to add some additional checks, e.g. to make sure that names starts and ends with a letter and not a symbol.
Edit
This will make sure that names starts/ends with a letter and does not contain consecutive symbols:
$name = "-Björ n''Bòrg-";
if (preg_match("/^\p{L}([- ']\p{L}|\p{L})*$/u", $name)) {
echo "$name is a valid name!"; // It's not
}
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.
I have this code which makes sure that the username is only letters and numbers. But the way my code is set up, I need it to check if the result of preg_match is false.
Right now it says "if secure echo this". I need its logic to say "if not secure say this". How can I do it?
if (preg_match('/[A-Z]+[a-z]+[0-9]+/', $username))
{
echo 'Secure enough';
}
You can negate the condition like this:
if (!preg_match('/^[A-Za-z0-9]+$/', $username))
{
echo 'Not secure enough';
}
Also, your regex needs to be [A-Za-z0-9]+ if you mean "alphanumeric" (only letters and numbers) as a whole.
The regex in your code would match if the username 1) starts with a capital letter (or more than one) 2) is followed by one or more lower-case letter and 3) ends with one or more number(s).
I'm really not sure if this is what you want. You can do, basically:
if (preg_match('/^[A-Za-z0-9]+$/', $username)) {
echo 'Is only letters and numbers';
} else {
echo 'Contains some other characters';
}
A secure password would be one with special characters and while you probably don't want to enforce this (depending on your target audience), you'd usually want your system to support special characters in passwords.
That's what the ! operator is for, so just say
if (!preg_match.....)
Or this is what the else clause is for. Either way, this is rudimentary programming.
Im creating a search function. The users are only allowed to use a-z A-Z and 0-9. How do i check if $var only contains text or numbers, and not special characters ?
I have tried something like this:
if (!preg_match('/[^a-z]/i', $search) {
$error = "error...";
}
If anyone have a smarter solution, please let me know. It could also be something checking for special characters.
You're pretty much there. Just add numbers 0-9 to your regular expression, like this:
if(preg_match('/[^a-z0-9]/i', $search))
{
$error = "Error...";
}
The /i flag tells the expression to ignore case, so A-Z is not needed inside the letter list.
In your original code, you were looking for anything that wasn't a letter or number, while also checking to see if preg_match() hadn't matched anything - you created a double negative. The code above executes the if() if anything that isn't a letter or number is found. Full credit to #brain in the comments.
To allow other characters, simply add them to the characters inside the braces:
if(preg_match('/[^a-z0-9 \.]/i', $search))
{
$error = "Error...";
}
This example allows spaces and . (dots).
Something like this:
if(!preg_match('/^[a-zA-Z0-9]+$/', $search)) {
// error
}
I want a regular expression to validate a nickname: 6 to 36 characters, it should contain at least one letter. Other allowed characters: 0-9 and underscores.
This is what I have now:
if(!preg_match('/^.*(?=\d{0,})(?=[a-zA-Z]{1,})(?=[a-zA-Z0-9_]{6,36}).*$/i', $value)){
echo 'bad';
}
else{
echo 'good';
}
This seems to work, but when a validate this strings for example:
11111111111a > is not valid, but it should
aaaaaaa!aaaa > is valid, but it shouldn't
Any ideas to make this regexp better?
I would actually split your task into two regex:
to find out whether it's a valid word: /^\w{6,36}$/i
to find out whether it contains a letter /[a-z]/i
I think it's much simpler this way.
Try this:
'/^(?=.*[a-z])\w{6,36}$/i'
Here are some of the problems with your original regex:
/^.*(?=\d{0,})(?=[a-zA-Z]{1,})(?=[a-zA-Z0-9_]{6,36}).*$/i
(?=\d{0,}): What is this for??? This is always true and doesn't do anything!
(?=[a-zA-Z]{1,}): You don't need the {1,} part, you just need to find one letter, and i flag also allows you to omit A-Z
/^.*: You're matching these outside of the lookaround; it should be inside
(?=[a-zA-Z0-9_]{6,36}).*$: this means that as long as there are between 6-36 \w characters, everything else in the rest of the string matches! The string can be 100 characters long mostly containing illegal characters and it will still match!
You can do it easily using two calls to preg_match as:
if( preg_match('/^[a-z0-9_]{6,36}$/i',$input) && preg_match('/[a-z]/i',$input)) {
// good
} else {
// bad
}