preg_match: number-alphabets and commas only
You want:
/^[a-zA-Z0-9,]+$/
You need the start ^
and end $
of string anchors. Without them the regex engine will look for any of those characters in the string and if it finds one, it will call it a day and say there's a match. With the anchors, it forces the engine to look at the whole string. Basically:
/[a-zA-Z0-9,]+/
matches if any of the characters are alphanumeric + comma./^[a-zA-Z0-9,]+$/
matches if all of the characters are alphanumeric + comma.
if(preg_match('/^[0-9a-z,]+$/i', $cst_value)) {
// valid input..contains only alphabet,number and comma.
}else{
// invalid
}
We pass the following to preg_match : /^[0-9a-z,]+$/i
Explanation:
/
: regex delimiters.^
: start anchor[..]
: Char class0-9
: any digita-z
: any alphabet,
: a comma. comma is not a regex metachar, so you need not escape it+
: quantifier for one or more. If an empty input is considered valid, change+
to*
$
: end anchori
: to make the matching case insensitive.