-4

Possible Duplicate:
Validate Email in php

how to validate email IF user did fill up form field ( do not validate if field is not modified ) ?

Example:

if ($formemail ="" ) /* user DIDN'T enter email/not filled up email field  */   
{
/* do not validate and do not =mail($,$,$,$) */
}

but if field is modified THEN validate if its ok then =mail($,$,$,$)

Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64
Ivan
  • 131
  • 7

3 Answers3

0

You can use HTML5 attributes to get that done.

<input type="text" class="email" email="email" required />

The required attribute makes sure the field has some value,and the email attribute checks if the value is an email. :)

wadie
  • 496
  • 1
  • 10
  • 24
  • 1
    Yes, but this html5 input type is not 100% supported across all browsers – Pjottur Dec 02 '12 at 16:47
  • 3
    Server-side validation should always take place. Doing it in the browser should strictly be seen as a UI improvement in response to the user. – Jared Farrish Dec 02 '12 at 16:48
  • True,server-side validation should always take place. sorry about that! I usually check if the field is empty,however I leave it to HTML5 to check if an email was entered. – wadie Dec 02 '12 at 16:50
0

Just do this:

if($formemail != ""){
    //validate and send
}

You don't need an else statement here.

Pjottur
  • 632
  • 9
  • 22
0

Try this (from http://www.linuxjournal.com/article/9585):

function checkEmail($email) {
  if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email)) {
    return true;
  }

  return false;
}

if (!empty($formemail) && checkEmail($formemail)) {
  //Mail it!
}
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195