一些较多使用的正则验证

正则验证:
检查 name 字段是否包含字母和空格
$name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "只允许字母和空格!"; }
注释:preg_match() 函数检索字符串的模式,如果模式存在则返回 true,否则返回 false。
 
检查 e-mail 地址语法是否有效
$email = test_input($_POST["email"]); if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = "无效的 email 格式!"; }
检查 URL 地址语法是否有效(这条正则表达式同时允许 URL 中的斜杠)
$website = test_input($_POST["website"]); if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/% =~_|]/i",$website)) { $websiteErr = "无效的 URL"; }
posted @ 2017-09-04 16:04  小耳朵李文文  阅读(95)  评论(0编辑  收藏  举报