Validate Phone Number With PHP
ฝัง
- เผยแพร่เมื่อ 1 ต.ค. 2024
- View on website:
scottsweb.dev/...
Phone numbers can be entered in many different formats, even with input type="tel" fields.
Here, I show you a PHP function that strips out standard formatting characters from the phone number and then makes sure the phone number is a valid length.
This function will work for US numbers as well as international numbers.
We use str_replace, is_numeric, and strlen functions in PHP.
Always sanitize your data and ensure that it looks like what you're expecting it to. Use good data validation habits!