0

I have written this code for Form using Server Side Validation in 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>

My problem is when I skip the $s_secemail field, I am getting this meassage "Secondary Email-ID is invalid", its not mandatory

1
  • Use var_dump($s_secemail) to see what is in $s_secemail.If necessary use trim($s_secemail); Commented May 25, 2013 at 6:43

3 Answers 3

2

use else if (!empty($s_secemail) && !filter_var($s_secemail, FILTER_VALIDATE_EMAIL)) to only check the condition if $s_secemail is provided

Sign up to request clarification or add additional context in comments.

1 Comment

that's weird. are you sure $s_secemail = $_POST['s_secemail'] ? you can also trim the data so white space will be truncated like this else if (!empty(trim($s_secemail)) && !filter_var($s_secemail, FILTER_VALIDATE_EMAIL)). Lastly, you're missing a } after the last if
1

You may try like this only check $s_secemail is valid email or not if is not empty as the below code

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>

1 Comment

@SagarPanchal i updated the code..you can check now..it is working in my system..
0

i found good source for implementing server side validation so would like to share http://www.webexpertlabs.com/server-side-form-validation-using-regular-expression/

1 Comment

The linked site is no longer active (at least at the time of this writing)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.