正则表达式验证信息
我尝试了以下内容:
if(preg_match("/[A-Za-z0-9]+/", $ingame_name) == TRUE){
header("Location: newitem.php?username=". $ingame_name ."&email=". $email);
} else {
$invalidusername = '<font color=red>Oops, invalid username! Username
may only contain numbers and letters.</font><br>';
}
哪个不起作用,也尝试翻转这些陈述仍然没有用......
我的最后一次尝试
if ($_REQUEST['do'] == 'submit')
{
$ingame_name= trim($_POST['ingame_name']);
$email = trim($_POST['email']);
if(eregi("/[A-Za-z0-9]+/", $ingame_name))
{
$invalidusername = '<font color=red>Oops, invalid username! Username may only contain numbers and letters.</font><br>';
$ingame_name = 'invalid';
}
if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email))
{
$invalidemail = '<font color=red>Oops, seems that you have an error with your email format.</font></br>';
$email = 'invalid';
}
if ( $ingame_name = 'invalid' || $email = 'invalid')
{
/* do nothin */
}
else
{
header("Location: item.php?username=". $ingame_name ."&email=". $email);
}
}
似乎没什么用,
答案 0 :(得分:1)
试试这个:
<?php
$valid_submit = ($_REQUEST && isset($_REQUEST['do']) && $_REQUEST['do'] == 'submit') ? true : false;
if ($valid_submit) {
$ingame_name= ($_POST && isset($_POST['ingame_name'])) ? trim($_POST['ingame_name']) : '';
$email= ($_POST && isset($_POST['email'])) ? trim($_POST['email']) : '';
$invalidusername = '';
$invalidemail = '';
if(!preg_match("/^[A-Za-z0-9]+$/", $ingame_name)) {
$invalidusername = '<font color=red>Oops, invalid username! Username may only contain numbers and letters.</font><br>';
$ingame_name = 'invalid';
}
// This is a much more efficient method to validate email addresses
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$invalidemail = '<font color=red>Oops, seems that you have an error with your email format.</font></br>';
$email = 'invalid';
}
if ( $ingame_name != 'invalid' && $email != 'invalid') {
$location = 'item.php?username=' . $ingame_name . '&email=' . $email;
header("Location: $location");
}
else {
//echo $invalidusername . $invalidemail;
}
}