on my php i use preg_match
to validate input texts.
if(preg_match('/^[a-zA-Z0-9]+$/', $firstname)) { }
But this only allows alphanumeric and does not allow spaces. I want to allow spaces, alpha and numeric. and period(.) and dash(-)
Please help me out here? thanks in advance.
The preg_match() function returns whether a match was found in a string.
The preg_match() function will tell you whether a string contains matches of a pattern.
A ctype_alpha() function in PHP used to check all characters of a given string are alphabetic or not. If all characters are alphabetic then return True otherwise return False.
preg_match() returns 1 if the pattern matches given subject , 0 if it does not, or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .
Use
preg_match('/^[a-z0-9 .\-]+$/i', $firstname)
If you not only want to allow ASCII, then use Unicode properties:
preg_match('/^[\p{L}\p{N} .-]+$/', $firstname)
\p{L}
is any letter in any language, matches also Chinese, Hebrew, Arabic, ... characters.
\p{N}
any kind of numeric character (means also e.g. roman numerals)
if you want to limit to digits, then use \p{Nd}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With