Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

E-mail validation

Status
Not open for further replies.

WannaLearn

Programmer
Jul 10, 2001
210
0
0
US
Hi. I need some help with a email validation.
I know how to do a simple validation that checks to make the sure the field is not blank, has a '@' sign and a ".", but how can I make it to check that at least 2 characters are entered before the @, 2 characters after the . and an acceptable domain name (like: com, net, edu, uk, etc), prefreably in an array so I can add more domain names later.
I found a script online that did all that, but I don't want to use it. I'd rather write the code myself. Any help? Any ideas?
Thanks.
 
>> I found a script online that did all that, but I don't want to use it. I'd rather write the code myself.

ok...start writing, and show us where you need help!


=========================================================
while (!succeed) try();
-jeff
 
Hi-

Do something like this:

function length()
if (document.form.name.length!="2"){
alert("Type more then 2 characters")
document.form.name.focus()
}

<input name=&quot;name&quot; type=&quot;text&quot; value=&quot;&quot; onBlur=&quot;length()&quot;>@<input name=&quot;domain&quot; type=&quot;text&quot; value=&quot;&quot;>.<input name=&quot;domainType&quot; type=&quot;text&quot; maxlength=&quot;3&quot; size=&quot;3&quot; onBlur=&quot;compare_value_against_array()&quot;>

For the domain type create an Array containing all the legal domain extension and compare user value against it...


Ciao
Placido

P.S.
Knowledge gives you the mean to see the door...
Wisdom gives you the key to open it...
 
Hi, this is what I came up with. If the field is blank and if the field is missing the '@' and '.' validation works, but not the domain validation. I'm checking to see if the domain (after the '.') is either a com, edu, or net (like the array says). How can I also check to see if at least 2 char. are enterted before the '@', also?
What am I doing wrong?
Thanks.

Code:
<html>
<head>

<script>
function checkemail() {
var check = document.form
var one = new Array(&quot;com&quot;, &quot;edu&quot;, &quot;net&quot;)

  if(check.email.value == &quot;&quot;) {
    alert(&quot;Please enter the email address&quot;);
    return false;
  }
  if(check.email.value.indexOf(&quot;@&quot;) == -1 || check.email.value.lastIndexOf(&quot;.&quot;) == -1)  {
	alert(&quot;Email must contain the @ and a .&quot;);
	return false;
  }
  for(i=0; i<=one.length; i++) {
    if(one[i] == -1) {
	  alert(&quot;Plase enter a valid domain name&quot;);
	  return false;
	}
  }
}
</script>
</head>

<body>
<form name=&quot;form&quot; method=&quot;post&quot; onSubmit=&quot;return checkemail();&quot;>
    <input type=&quot;text&quot; name=&quot;email&quot;><br><br>
    <input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
</form>
</body>
</html>
 
looks good...your domain validation can be something like this:
Code:
  var bOK = false;
  for(i=0; i<=one.length; i++) {
    if(check.email.value.indexOf(&quot;.&quot; + one[i]) > -1) {
      bOK = true;
    }
  }
  if (!bOK) {
    alert(&quot;Plase enter a valid domain name&quot;);
    return false;
  }

=========================================================
while (!succeed) try();
-jeff
 
USe Regular expressions....

<html>
<script>
function checkEmail(txtemail)
{
var emapat = /^[a-z][a-z_0-9\.]+@[a-z_0-9\.]+\.[a-z]{2,3}$/i;
if(!emapat.test(txtemail))
{alert('You have entered an incorrect email address');
return false;}
return true;
}
</script>
<body>
<form name=&quot;myform&quot; Action=&quot;&quot;>
<input type=&quot;text&quot; value=&quot;&quot; name=&quot;email&quot; onblur=&quot;javascript:checkEmail(this.value);&quot;>
</form>

</body>
</html>


good luck.
grtfercho çB^]\..
 
The problem with a domain validation is that you'lll have to consider not only the .com. net .org and others but top level domains like .jp .ca .co .bo .pe etc..... having an array for alllll of them will be hard work and if you actually come with something like that please post it here so we don't have to write all of them.... :) :)





good luck
grtfercho çB^]\..
 
jemminger, hi. The domain part works good. Now i'm trying to make sure that the user entered at least 2 char before the '@' sign. This is what I have, and it keeps throwing the error, why?
Thanks.

Code:
<html>
<head>

<script>
function checkemail() {
var check = document.form
var one = new Array(&quot;com&quot;, &quot;edu&quot;, &quot;net&quot;, &quot;we&quot;)
var two = 2;

  if(check.email.value == &quot;&quot;) {
    alert(&quot;Please enter the email address&quot;);
    check.email.focus();
	return false;
  }
  if(check.email.value.indexOf(&quot;@&quot;) == -1 || check.email.value.lastIndexOf(&quot;.&quot;) == -1)  {
	alert(&quot;Email must contain the @ and a .&quot;);
	check.email.focus();
	return false;
  }
  var bOK = false;
  for(i=0; i<=one.length; i++) {
    if(check.email.value.indexOf(&quot;.&quot; + one[i]) > -1) {
      bOK = true;
    }
  }
  if (!bOK) {
    alert(&quot;Plase enter a valid domain name&quot;);
	check.email.focus();
    return false;
  }
  var before = false;
  for(ii=0; ii>=two.length; ii++) {
    if(check.email.value.indexOf(two[ii] + &quot;@&quot;) > -1) {
      before = true;
    }
  }
    if(!before) {
      alert(&quot;Please enter at least 2 characters before the '@' sign&quot;);
	  check.email.focus();
	  return false;
    }
}
</script>
</head>

<body>
<form name=&quot;form&quot; method=&quot;post&quot; onSubmit=&quot;return checkemail();&quot;>
    <input type=&quot;text&quot; name=&quot;email&quot;><br><br>
    <input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
</form>
</body>
</html>









grtfercho, I will.
 
I also tried:

Code:
  var before = false;
  var two;
  if(two >= 2) {
    if(check.email.value.indexOf(two + &quot;@&quot;) > -1) {
      before = true;
    }
  }
    if(!before) {
      alert(&quot;Please enter at least 2 characters before the '@' sign&quot;);
	  check.email.focus();
	  return false;
    }

Get the same error.
 
think about what you're trying to accomplish:

- make sure there's at least 2 chars before the @ symbol.

so, the indexOf('@') must be 2 or greater.

see if this helps.



=========================================================
while (!succeed) try();
-jeff
 
grtfercho,

yes, regexes are great, but it sounds to me like WannaLearn wants to learn the basics first.

=========================================================
while (!succeed) try();
-jeff
 
so, the indexOf('@') must be 2 or greater.

I thought that is what the var two was doing. I declared a var called 2 and gave it the value of 2, then checked to see if the two is false then throw an alert, but if it is then two is true...

Code:
  var before = false;
  var two;
  if(two >= 2) {
    if(check.email.value.indexOf(two + &quot;@&quot;) > -1) {
      before = true;
    }
  }
    if(!before) {
      alert(&quot;Please enter at least 2 characters before the '@' sign&quot;);
      check.email.focus();
      return false;
    }

So why would this keep giving me an alert even if I enter:
Code:
abcd@me.com
 
your code:
Code:
  var before = false;
  var two;
  if(two >= 2) {
    if(check.email.value.indexOf(two + &quot;@&quot;) > -1) {
      before = true;
    }
  }

is equivalent to this:
Code:
  var before = false;

  if(2 >= 2) {
    if(check.email.value.indexOf(2 + &quot;@&quot;) > -1) {
      before = true;
    }
  }


in which case only an email address like &quot;
Code:
2@
whatever.com&quot; would pass.

String1.indexOf(String2) gives you the zero-indexed position of String2 in String1.

for example:
Code:
str = &quot;foo@bar.com&quot;;
       -----------
       0123456...

str.indexOf(&quot;@&quot;) == 3

use something like this:
Code:
if(check.email.value.indexOf(&quot;@&quot;) < 2) {
	alert(&quot;Please enter at least 2 characters before the '@' sign&quot;);
	check.email.focus();
	return false;
}


=========================================================
while (!succeed) try();
-jeff
 
jemminger, thanks for showing me my mistake. You rock. LOL.
Just one more question, now I know how to validate how many char before certain indexOf() and after, but what about between? What I mean is if I wanted to make sure someone entered something after the '@' and before the &quot;.&quot;, how would I do that?
I was thinking something like taking the position of &quot;@&quot; and minusing it from the position of &quot;.&quot;, but somehow that sounds wrong.
Any ideas. I don't want you to do it for me, I want a direction to go in. :)
 
why does it sound wrong to check if the difference between the two positions is >1?
 
WannaLearn,

that should be fine, provided you don't want to pass an address like &quot;foo.bar@site.com&quot;


=========================================================
while (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top