-1

hi i font know if this is the right place to ask this question but i have a problem with my code that i cannot figure out. i have tried many different algorithms and none work. i am trying to validate email from a form. here is the code (form is in html)

function isValidString(str) {

  var quot = "\"";
  if (str.indexOf(quot) != -1)
    return false;

  var badStr = "$%^&*()_+[]{}<>?אבגדהוזחטיכךלמםנןסעפצקרשת";
  var i = 0,
    p;
  while (i < str.length) {
    p = badStr.indexOf(str.charAt(i));
    if (p != -1)
      return false;
    i++;
  }


  return true;
}

function isValidEmail()

{

  var str = document.getElementById("email").value;

  document.write("email from isValidEmail(str) = " + email);
  if (isEmpty(str) || str.length < 5) {
    alert("isEmpty(str) || str.length < 5 = false");
    return false;
  }

  if (!isValidString(str)) {
    alert("!isValidString(str) = false");
    return false;
  }

  var atSign = str.indexOf('@');
  if (atSign == -1 || str.lastIndexOf('@') || atSign === 0 || atSign == str.length - 1) {
    alert("atSign == -1 || str.lastIndexOf('@') || atSign == 0 || atSign == str.length - 1 = false");
    return false;
  }

  var dotSign = str.indexOf('.', atSign);
  if (dotSign == -1 || dotSign === 0 || dotSign == str.length - 1 || dotSign - atSign < 2) {
    alert("dotSign == -1 || dotSign == 0 || dotSign == str.length - 1 || dotSign - atSign < 2 = false");
    return false;
  }
  return true;
  
  

no matter what i input it always comes back valid. here is the part where i apply it:

var email = document.getElementById("email").value;
if (emailcheck(email)) {
  alert("invalid email");
  return false;
}

return true;

thanks in advance

nisai
  • 1
  • Regex based email addresss validation might be a bad idea: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – Terry Jun 17 '15 at 09:40
  • The only real validation is to send a confirmation mail. But if you want to check that they match a pattern then a parser is a better idea, something like this library https://github.com/FogCreek/email-addresses – Xotic750 Jun 17 '15 at 09:46

3 Answers3

0

An example of using the parser library mentioned in my comment.

var eAddr = document.getElementById('eAddr'),
    check = document.getElementById('check'),
    pre = document.getElementById('out');

check.addEventListener('click', function (evt) {
    pre.textContent = !!emailAddresses.parseOneAddress(eAddr.value.trim());
}, false);
<script src="https://rawgit.com/FogCreek/email-addresses/master/lib/email-addresses.js"></script>
<input id="eAddr"></input>
<button id="check">Test pattern</button>
<pre id="out"></pre>

Note: this will accept Goodhertz Inc <support@goodhertz.com> as it stands and you would need to further check the object returned by parseOneAddress to filter these out.

Xotic750
  • 22,914
  • 8
  • 57
  • 79
0

You don't call the rigth function i. e. call

var email = document.getElementById("email").value;
if (isValidString(email)) {
  alert("invalid email");
  return false;
}

return true;

instead of

var email = document.getElementById("email").value;
if (emailcheck(email)) {
  alert("invalid email");
  return false;
}

return true;
AdminXVII
  • 1,319
  • 11
  • 22
-1

Using Regular expression is the best method for validating input elements. Below function can validate email perfectly.

function regExValidate_Email(id) {
    var email = document.getElementById(id).value;
    if (email != '') {
        var regExforEmail = /^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        if (regExforEmail.test(email)) {
            $("#" + id).css("background-color", "#ffffff");
            return true;
        }
        else {
            alert('Please enter a valid email id. \nex: yourname@example.com');
            document.getElementById(id).style.backgroundColor = '#feffea';
            document.getElementById(id).value = '';
            Ctrlid = id;
            setTimeout("document.getElementById(Ctrlid).focus()", 1);
            return false;
        }
    }
    else { document.getElementById(id).style.backgroundColor = 'white'; }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Email: <input type="email" onblur="return regExValidate_Email(this.id)" id="txtEmail" />
Srinivas
  • 133
  • 6
  • This is a valid email address `email@123.123.123.123` and this `email@[123.123.123.123]` and this `“email”@example.com`. This is an invalid email address `.email@example.com` and this `email.@example.com` and so on ... – Xotic750 Jun 17 '15 at 10:22