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.
Alphanumeric, also referred to as alphameric, is a term that encompasses all of the letters and numerals in a given language set. In layouts designed for English language users, alphanumeric characters are those comprised of the combined set of the 26 alphabetic characters, A to Z, and the 10 Arabic numerals, 0 to 9.
An alphanumeric string is a string that contains only alphabets from a-z, A-Z and some numbers from 0-9. Explanation: This string contains all the alphabets from a-z, A-Z, and the number from 0-9. Therefore, it is an alphanumeric string.
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
}
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