CodeCanyon

Need help with regular expressions

293 posts
  • Author had a Free File of the Month
  • Sold between 10 000 and 50 000 dollars
  • Indonesia
  • Referred between 200 and 499 users
  • Bought between 10 and 49 items
  • Has been a member for 2-3 years
  • Exclusive Author
amatyr4n says


Isn’t there a space character in between the name? if so then it should be allowed as well I guess.
/^[a-z]+[a-z\s\.\'-]*[a-z\.]+$/i
When I put a number is still pass.

That’s weird, I’ve tried using number and and surely it won’t pass the regex since there’s no number matches in the pattern

<?php
$name = "A.J. D'avilla 1234567890";
$expression = "/^[a-z]+[a-z\s\.\'-]*[a-z\.]+$/i";

if(!preg_match($expression, $name1)){
    echo "The string contain illegal characters";
} else {
    echo "The string passed";
}
?>
see the output:
The string contain illegal characters
182 posts
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 100 and 199 users
  • Sold between 5 000 and 10 000 dollars
  • United States
SuperFlash says



Isn’t there a space character in between the name? if so then it should be allowed as well I guess.
/^[a-z]+[a-z\s\.\'-]*[a-z\.]+$/i
When I put a number is still pass.

That’s weird, I’ve tried using number and and surely it won’t pass the regex since there’s no number matches in the pattern

<?php
$name = "A.J. D'avilla 1234567890";
$expression = "/^[a-z]+[a-z\s\.\'-]*[a-z\.]+$/i";

if(!preg_match($expression, $name1)){
    echo "The string contain illegal characters";
} else {
    echo "The string passed";
}
?>
see the output:
The string contain illegal characters

Try $name = “George”;

It’s print out The string contain illegal characters instead of the string passed.

42 posts
  • Exclusive Author
  • Has been a member for 1-2 years
marketslk says
Pal don`t waste time trying several.Please use this is 100% assured .Experiment this.what you need is achieved and test
<?php
$name = "A.J. D' avilla";
$expression = "/[0-9!@#$%^&*_~`<>,?:;\"|()=+{}\[\]€\/]/";

if(preg_match($expression, $name)) { // i reversed if logic to make the problem easier 
    echo "invalid characters";
    }else{
   echo $name;// string fit to output 
}
?>

42 posts
  • Exclusive Author
  • Has been a member for 1-2 years
marketslk says
This is further developed .please try these place your bad characters anywhere in the name .I`m sure that this will be the final solution
<?php
$name = "A.J. D' avilla";
$expression = "/[0-9!@#$%^&*_~`<>,?:;\"|()=+{}\[\]€\/\\\]/";

if(preg_match($expression, $name)) { // i reversed if logic to make the problem easier 
    echo "invalid characters";
    }else{
   echo $name;// string fit to output 
}
?>
42 posts
  • Exclusive Author
  • Has been a member for 1-2 years
marketslk says
The only way is reverse logic the ‘if’ statement.
<?php
$name = "A.J. D' avilla";
$expression = "/[0-9!@#$%^&*_~`<>,?:;\"|()=+{}\[\]€\/\\\]/";

if(preg_match($expression, $name)) { // i reversed if logic to make the problem easier 
    echo "invalid characters";
    }else{
   echo $name;// string fit to output 
}
?>

I have tried your example but numbers are still accepted. Something is incorrect. Also * and / are passing. They should not.
293 posts
  • Author had a Free File of the Month
  • Sold between 10 000 and 50 000 dollars
  • Indonesia
  • Referred between 200 and 499 users
  • Bought between 10 and 49 items
  • Has been a member for 2-3 years
  • Exclusive Author
amatyr4n says




Isn’t there a space character in between the name? if so then it should be allowed as well I guess.
/^[a-z]+[a-z\s\.\'-]*[a-z\.]+$/i
When I put a number is still pass.

That’s weird, I’ve tried using number and and surely it won’t pass the regex since there’s no number matches in the pattern

<?php
$name = "A.J. D'avilla 1234567890";
$expression = "/^[a-z]+[a-z\s\.\'-]*[a-z\.]+$/i";

if(!preg_match($expression, $name1)){
    echo "The string contain illegal characters";
} else {
    echo "The string passed";
}
?>
see the output:
The string contain illegal characters

Try $name = “George”;

It’s print out The string contain illegal characters instead of the string passed.

Sorry my bad it’s typo, $name1 in preg_replace() should be $name. “George” will pass of course because if you see the pattern it will match any words more than 1 chars

<?php

$name = "George";
$expression = "/^[a-z]+[a-z\s\.\'-]*[a-z\.]+$/i";

if(!preg_match($expression, $name)){
    echo 'The string contain illegal characters';
} else {
    echo 'The string passed';
}

?>

Pal don`t waste time trying several.Please use this is 100% assured .Experiment this.what you need is achieved and test

I believe whitelist approach is much simpler to implement and easier to read and to be maintained later than blacklist approach here, because with blacklist way you must include ALL the possibility of illegal characters in your pattern instead of what only allowed chars

42 posts
  • Exclusive Author
  • Has been a member for 1-2 years
marketslk says
Does n`t allow $name = “A.J. D’ avilla-”;





Isn’t there a space character in between the name? if so then it should be allowed as well I guess.
/^[a-z]+[a-z\s\.\'-]*[a-z\.]+$/i
When I put a number is still pass.

That’s weird, I’ve tried using number and and surely it won’t pass the regex since there’s no number matches in the pattern

<?php
$name = "A.J. D'avilla 1234567890";
$expression = "/^[a-z]+[a-z\s\.\'-]*[a-z\.]+$/i";

if(!preg_match($expression, $name1)){
    echo "The string contain illegal characters";
} else {
    echo "The string passed";
}
?>
see the output:
The string contain illegal characters

Try $name = “George”;

It’s print out The string contain illegal characters instead of the string passed.

Sorry my bad it’s typo, $name1 in preg_replace() should be $name. “George” will pass of course because if you see the pattern it will match any words more than 1 chars

<?php

$name = "George";
$expression = "/^[a-z]+[a-z\s\.\'-]*[a-z\.]+$/i";

if(!preg_match($expression, $name)){
    echo 'The string contain illegal characters';
} else {
    echo 'The string passed';
}

?>

Pal don`t waste time trying several.Please use this is 100% assured .Experiment this.what you need is achieved and test
I believe whitelist approach is much simpler to implement and easier to read and to be maintained later than blacklist approach here, because with blacklist way you must include ALL the possibility of illegal characters in your pattern instead of what only allowed chars
293 posts
  • Author had a Free File of the Month
  • Sold between 10 000 and 50 000 dollars
  • Indonesia
  • Referred between 200 and 499 users
  • Bought between 10 and 49 items
  • Has been a member for 2-3 years
  • Exclusive Author
amatyr4n says

Does n`t allow $name = “A.J. D’ avilla-”;

obviously ;) because the pattern will only match a string which:
- first char is a-z
- in-between can be a-z, whitespace, singlequote, dash, dot
- end char can be a-z or dot

My assumption is that there’s no person who has a dash character in his very last char of his last name. If you found this is wrong, I apologize.

18 posts
  • Bought between 1 and 9 items
  • Europe
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
webarto says

Don’t forget to preg_quote() your $name.

by
by
by
by
by