-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($,$,$,$)

2
  • $formemail ="" should be $formemail =="" Commented Dec 2, 2012 at 16:44
  • where you assigning the $dormemail ? Commented Dec 2, 2012 at 16:52

3 Answers 3

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. :)

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

3 Comments

Yes, but this html5 input type is not 100% supported across all browsers
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.
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.
0

Just do this:

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

You don't need an else statement here.

Comments

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! } 

1 Comment

- Don't try this at home kids. Learn about filter_var.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.