0

我已经使用Server Side Validationin为 Form 编写了这段代码CORE PHP

                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 无效”,它不是强制性的

4

3 回答 3

2

用于else if (!empty($s_secemail) && !filter_var($s_secemail, FILTER_VALIDATE_EMAIL))仅在提供 $s_secemail 时检查条件

于 2013-05-25T06:36:32.763 回答
2

您可以尝试像这样只检查$s_secemailvalid emailnot如果是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>
于 2013-05-25T06:44:59.377 回答
0

我找到了实现服务器端验证的好来源,所以想分享 http://www.webexpertlabs.com/server-side-form-validation-using-regular-expression/

于 2013-11-12T20:22:28.847 回答