Jump to content

Form validation problem


rickphp

Recommended Posts

I need it so that if the email field isn't blank then the validation be ran against it, but the form is sending even when the message is left empty even though the validation in place is correct, I think the way the below is coded is causing the issue, can it be improved?

 

Here is the current code I've got:

 

  else if ($email != "") {

  if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {

  $error = "The format of your email address is not valid.";

       } 
       }

 

Sorry if thats not clear enough but hopefully you get what I mean!  :confused:

 

Thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/187463-form-validation-problem/
Share on other sites

Without looking at the regexp extensively, where is $email being assigned?  Maybe the issue lies there rather than at this particular bit of code.

 

I'm not fantastic with regexp, but this looks passable as email validation code (though I do have this feeling that something isn't quite right, but I can't put my finger on it).

$email is inputted via a form, I think its this bit of coding thats causing the problem as when I remove this bit of code the problem goes away, but I need to ensure proper emails are entered. Any ideas?

 

The form consists of name, businessname, email, phonenumber and message, the problem arrises when the person fills out their email, they can leave the message field empty and the form still sends, if they dont enter an email and only enter a phone number (which is acceptable) they then need to enter a message before the form sends (which again is correct) but its only when entering an email the rest of the validation following the code provided stops working.

 

I think the way I've coded it is halting any following validation.. but only when an email is inputted

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.