我使用Server Side Validation
CORE PHP
为Form编写了此代码
if(empty($s_email)){
$er = "Please enter E-mail";
}else if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i", $s_email)){
$er="Email-ID is invalid!<br>";
}else if(!filter_var($s_secemail, FILTER_VALIDATE_EMAIL)){
$er="Secondary Email-ID is invalid!<br>";
<p>
<label class="required" for="s_email">Email Address:</label><br/>
<input type="text" id="s_email" class="half" value="" name="s_email"/>
</p>
<p>
<label for="s_secemail">Secondary Email Address:</label><br/>
<input type="text" id="s_secemail" class="half" value="" name="s_secemail"/>
</p>
我的问题是,当我跳过$s_secemail
字段时,我得到了这个消息
“辅助电子邮件ID无效”,非强制性
答案 0 :(得分:2)
使用else if (!empty($s_secemail) && !filter_var($s_secemail, FILTER_VALIDATE_EMAIL))
仅检查条件是否提供了$ s_secemail
答案 1 :(得分:2)
您可以尝试这样做,只检查$s_secemail
是valid email
还是not
,如果not empty
为以下代码
if(empty($s_email)){
$er = "Please enter E-mail";
}else if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i", $s_email)){
$er="Email-ID is invalid!<br>";
}else if(!empty($s_secemail) && !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i", $s_secemail)){
$er="Secondary Email-ID is invalid!<br>";
}
<p>
<label class="required" for="s_email">Email Address:</label><br/>
<input type="text" id="s_email" class="half" value="" name="s_email"/>
</p>
<p>
<label for="s_secemail">Secondary Email Address:</label><br/>
<input type="text" id="s_secemail" class="half" value="" name="s_secemail"/>
</p>
答案 2 :(得分:1)
我找到了实现服务器端验证的良好来源,因此我想分享 http://www.webexpertlabs.com/server-side-form-validation-using-regular-expression/