I want to validate alphanumeric string in form text box in php. It can contain numbers and special characters like '.' and '-' but the string should not contain only numbers and special characters. Please help with the code.
Use ctype_alnum
like below:
if(ctype_alnum($string)){
echo "Yes, It's an alphanumeric string/text";
}
else{
echo "No, It's not an alphanumeric string/text";
}
Read function specification on php.net
Try this
// Validate alphanumeric
if (preg_match('/^[a-zA-Z]+[a-zA-Z0-9._]+$/', $input)) {
// Valid
} else {
// Invalid
}
Code:
if(preg_match('/[^a-z_\\-0-9]/i', $string)) { echo "not valid string"; }
Explanation:
The 'i' modifier at the end of the regex is for 'case-insensitive' if you don't put that you will need to add the upper case characters in the code before by doing AZ
我是regex的新手,但是我可以这样:
preg_match('/^[\w.-]+$/', input)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.