Manikandan
Manikandan

Reputation: 699

how to check whether string contains '@ 'and '.'?

search_field : ________

In these field we can enter either name or email. Considering whether if the string contains @ and .,it would be considered as email else name.

The code i tried is:

if(preg_match('[@|.]', $search_field )) {
    echo "email present";
} 
else{
  echo name present";
}

The formats wht i entered is:

search_field : masn@ .

it gave me output as email present but i want the output in the other way.It should show email presence one and only if it contains both @ and .

Upvotes: 0

Views: 43

Answers (2)

Sandeep Nambiar
Sandeep Nambiar

Reputation: 1676

Try This Code

if(preg_match('!@+!', $search_field )) {
    echo "email present";
} 
else{
  echo "name present";
}

Upvotes: 0

Harshit
Harshit

Reputation: 5157

If you only want to validate the emails, then try this

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Invalid email format"; 
}

If you want to validate name then use

if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  $nameErr = "Only letters and white space allowed"; 
}

Upvotes: 1

Related Questions