SuperFlash said
amatyr4n saidWhen I put a number is still pass.
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
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
amatyr4n said
SuperFlash said
amatyr4n saidWhen I put a number is still pass.
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\.]+$/iThat’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.
<?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
}
?>
<?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
}
?>
<?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
}
?>
SuperFlash said
I have tried your example but numbers are still accepted. Something is incorrect. Also * and / are passing. They should not.
SuperFlash said
amatyr4n said
SuperFlash said
amatyr4n saidWhen I put a number is still pass.
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\.]+$/iThat’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 charactersTry $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';
}
?>
marketslk said
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
amatyr4n said
SuperFlash said
amatyr4n said
SuperFlash said
amatyr4n saidWhen I put a number is still pass.
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\.]+$/iThat’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 charactersTry $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'; } ?>marketslk saidI 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
Pal don`t waste time trying several.Please use this is 100% assured .Experiment this.what you need is achieved and test
marketslk said
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.
Don’t forget to preg_quote() your $name.
