1

Okay, so I'm working on this contact form here, and everything seems to be working well. It checks if the email address has an "@" sign with the following code: (It's a function, just not shown here for ease).

JS:

var at = "@";

if (email.indexOf(at) == -1 || email.indexOf(at) == 0) {
   success = false;
   document.getElementById("email-error").innerHTML = "That's not an email!";
   document.getElementById("email-good").innerHTML = "";
 }

HTML:

<input onfocus="return validation()" type="text" name="email" id="email"><span id="email-error"></span><span id="email-good"></span>

I want to check if there is a "." (dot) in the email value with the following code, but it doesn't work!

  var at = "@";
  var dot = ".";
if (email.indexOf(at) == -1 || email.indexOf(at) == 0 || email.indexOf(dot) == -1 ||    email.indexOf(dot) == 0) {
success = false;
document.getElementById("email-error").innerHTML = "That's not an email!";
document.getElementById("email-good").innerHTML = "";
}

Also, if possible, is there a way to check if there are more than one "@" or "." ? I tried > and != 1 already.

Matthew
  • 2,158
  • 7
  • 30
  • 52

3 Answers3

2

is there a way to check if there are more than one "@" or "." ?

Yes, you can do String.prototype.split and check the length.

var email = 'a@b.c';

if (email.indexOf('@') < 1 || // @ index -1 or 0
    email.split('@').length > 2 || // more than one @
    email.indexOf('.') < 1) { // . index -1 or 0
    success = false;
    // etc
}

Remember that some email addresses can have multiple .s, e.g. someone@ukogbani.co.uk and that you can't validate an address exists unless you send something to it and get an expected response.

A really simple RegExp check is /^[^ @]+@[^ @]+$/, because unless it is a really special email address, it won't contain spaces or more than one @ sign, but could be owned by, e.g. tld com, such as tldadmin@com

Paul S.
  • 64,864
  • 9
  • 122
  • 138
1

If you are trying to check for a valid email address using javascript I would strongly recommend using a Regular Expression check.

Please see the following post for more information: Validate email address in JavaScript?

Community
  • 1
  • 1
JanR
  • 6,052
  • 3
  • 23
  • 30
  • Using a regex for email validation is overkill - just make sure there's a `@` and, if you want to get REALLY ambitious, a `.`. – Patashu Jun 14 '13 at 01:28
  • if you only check for an @ sign, users can still enter example@user and it would be considered a valid email. – JanR Jun 14 '13 at 01:30
  • @JanR I could enter asdjodfgjdsjjopj@hotmail.com as my email - it's a valid email - but it doesn't exist. No amount of regexing can prove your email is valid. – Patashu Jun 14 '13 at 01:30
  • I agree to that extend, and there is nothing you can do about that. But you can guide the user to at least put in an email in the correct format. Say I am in a rush and forget the .com or my copy/paste didn't work correctly, the website would alert me. Also it's up to your application design to ensure people enter an existing email address, hence a lot of websites go with some sort of email activation procedure – JanR Jun 14 '13 at 01:32
  • The proper way to verify email addresses is to send an email to that address and see if the recipient replies back or uses whatever link you sent to them. Not using ungodly regexes. A **small** simple regex for sanity checking is fine to use, however. the performance of such a regex check will **never** be worth a second thought. – Steven Lu Jun 16 '13 at 02:18
-1

if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

   $email = $_POST['email'];
  } 
  else {
  exit("The Email Address you have entered is not valid.");
  }
Zowes
  • 59
  • 1
  • 7