By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,306 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

Need help in validating the period in an email address (new JavaScript student)

P: n/a
Sue
<html>
Is there someone here that can help me validate the period as the
fourth from the last character in an email address. There is other
information and validation on the form I have to do but the period in
the email address is the only part I am having problems with. I have
posted part of my code below. Any help would be greatly appreciated.

THANKS !

<head>
<title>JavaScript Project</title>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--Hide from old browsers
function Validate(myform){

// validate the e-mail
var RegeMail=document.Register.eMail.value
var atSign = RegeMail.indexOf("@")
if (RegeMail == " " || atSign == -1) {
alert("Please enter your e-mail address")
document.Register.eMail.value = " "
document.Register.eMail.focus()
}
}
//-->
</script>
</head>

<body>

<FORM Name="Register">
<table border="0" width="60%">

<tr>
<td width="50%"><p align="right">Enter Your Email Address : </td>
<td width="50%"><Input Type="text" Name="eMail" value=" "></td>
</tr>

<tr>
<td width="50%"><p align="right"><Input Type="button" Value="Send"
onClick="Validate(Register)"> </td>
<td width="50%"><Input Type="Reset"></td>
</tr>
</table>
</center></div>
</body>
</html>
Jul 20 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
Sue hu kiteb:
<html>
Is there someone here that can help me validate the period as the
fourth from the last character in an email address. There is other
information and validation on the form I have to do but the period in
the email address is the only part I am having problems with. I have
posted part of my code below. Any help would be greatly appreciated.


I think you are approaching this wrongly. You seem to be assuming that a
"." will be teh 4th from last character in an email address. This is a
bad assumption, and quite wrong. Instead, I'd check for the presence of
a dot in teh same way you heck for the presence of an at sign.

Why is it bad to look for a dot in the 4th from last position?

ta****@company.co.jp
me*****@downingstreet.gov.uk

That's why.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #2

P: n/a
Sue

I am not assuming that the period is the fourth from the last
character. Some domains have four letter extention making the period
the fifth from the last. However, the instruction in doing this
project says to check for the period being the fourth from the last.

On Tue, 25 Nov 2003 17:16:50 +0900, "Fabian" <la****@hotmail.com>
wrote:
Sue hu kiteb:
<html>
Is there someone here that can help me validate the period as the
fourth from the last character in an email address. There is other
information and validation on the form I have to do but the period in
the email address is the only part I am having problems with. I have
posted part of my code below. Any help would be greatly appreciated.


I think you are approaching this wrongly. You seem to be assuming that a
"." will be teh 4th from last character in an email address. This is a
bad assumption, and quite wrong. Instead, I'd check for the presence of
a dot in teh same way you heck for the presence of an at sign.

Why is it bad to look for a dot in the 4th from last position?

ta****@company.co.jp
me*****@downingstreet.gov.uk

That's why.
--


Jul 20 '05 #3

P: n/a
Sue <Su***@comcast.net > writes:

Please don't top post.
I am not assuming that the period is the fourth from the last
character. Some domains have four letter extention making the period
the fifth from the last. However, the instruction in doing this
project says to check for the period being the fourth from the last.


So it is an school project assignment? Always say so, people react
differently when they know (and bad when they find out later).

What primitives are you allowed to use?
Regular expression? (the power tool when it comes to checking strings)
indexOf, lastIndexOf? (that's a hint!)

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a
JRS: In article <h8********************************@4ax.com>, seen in
news:comp.lang.javascript, Sue <Su***@comcast.net> posted at Tue, 25 Nov
2003 04:34:06 :-
Is there someone here that can help me validate the period as the
fourth from the last character in an email address.


So you are not expecting to write to me, or to *.firm, etc., or even to
*.us??

Your request, as stated, would allow Fr**@here.there.x.y which may
be technically permissible but is not likely to be valid. You mean the
*last* period as the preantepenultimate character; look up lastIndexOf
as a Method of String.

Text fields are best validated with RegExps.

See <URL:http://www.merlyn.demon.co.uk/js-other.htm#VEmA>
<URL:http://www.merlyn.demon.co.uk/jt.htm>
In jt.htm, seek, in PrepObj, '"em"', and replace the last '+' on the
line with '{3}', I think, for your purpose.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5

P: n/a
Sue
The indexOf() method is what I need to use to validate the fourth from
the last character. This is my first JavaScript class so I do not need
the advance validations of email addresses. The last four characters
of the address can be all periods. All I have to do is validate that
the fourth from the last is a period.



On Tue, 25 Nov 2003 13:01:16 +0000, Dr John Stockton
<sp**@merlyn.demon.co.uk> wrote:
JRS: In article <h8********************************@4ax.com>, seen in
news:comp.lang.javascript, Sue <Su***@comcast.net> posted at Tue, 25 Nov
2003 04:34:06 :-
Is there someone here that can help me validate the period as the
fourth from the last character in an email address.


So you are not expecting to write to me, or to *.firm, etc., or even to
*.us??

Your request, as stated, would allow Fr**@here.there.x.y which may
be technically permissible but is not likely to be valid. You mean the
*last* period as the preantepenultimate character; look up lastIndexOf
as a Method of String.

Text fields are best validated with RegExps.

See <URL:http://www.merlyn.demon.co.uk/js-other.htm#VEmA>
<URL:http://www.merlyn.demon.co.uk/jt.htm>
In jt.htm, seek, in PrepObj, '"em"', and replace the last '+' on the
line with '{3}', I think, for your purpose.


Jul 20 '05 #6

P: n/a
Lee
Sue said:

The indexOf() method is what I need to use to validate the fourth from
the last character. This is my first JavaScript class so I do not need
the advance validations of email addresses. The last four characters
of the address can be all periods. All I have to do is validate that
the fourth from the last is a period.


If you want to validate that the fourth from last character is
a period, then indexOf() is not at all what you want to use.

That will only tell you where the first period is.
For example,

"alpha.beta.com".indexOf(".")

would report that it finds a period at a position other
than the 4th from the end.

You need to look through the methods of String to find
one that will tell you which character is found at a
specified position in the string.

Jul 20 '05 #7

P: n/a
Sue <Su***@comcast.net > writes:

(Please don't top post).
The indexOf() method is what I need to use to validate the fourth
from the last character.
That sounds unlikely. I can think of many different ways to find
the fourth last character of a string, but indexOf is almost useless.

You know the index of the character: string.length - 4
Maybe you can use the subtring/substr methods to pick out the
character. Or the charAt method.
This is my first JavaScript class so I do not need the advance
validations of email addresses. The last four characters of the
address can be all periods. All I have to do is validate that the
fourth from the last is a period.


If that's the specification, that's what they'll get :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8

P: n/a
Sue
The instructor said take the total of the string using the indexOf()
method and subtract four to varify the period. The following code will
tell me what position the period is from the front but I have been
unable to determine how to write the formula for subtracting 4 from
the total of the string and then read that character.

// validate the e-mail
var RegeMail=document.Register.eMail.value
var atSign = RegeMail.indexOf("@")

var Period=document.Register.eMail.value
var PPeriod = Period.indexOf('.');
alert('The Period is at position '+PPeriod+'.');

if (RegeMail == " " || atSign == -1) {
alert("Please enter your e-mail address")
document.Register.eMail.value = " "
document.Register.eMail.focus()
}
else {

On Wed, 26 Nov 2003 04:09:09 +0100, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
Sue <Su***@comcast.net > writes:

(Please don't top post).
The indexOf() method is what I need to use to validate the fourth
from the last character.


That sounds unlikely. I can think of many different ways to find
the fourth last character of a string, but indexOf is almost useless.

You know the index of the character: string.length - 4
Maybe you can use the subtring/substr methods to pick out the
character. Or the charAt method.
This is my first JavaScript class so I do not need the advance
validations of email addresses. The last four characters of the
address can be all periods. All I have to do is validate that the
fourth from the last is a period.


If that's the specification, that's what they'll get :)

/L


Jul 20 '05 #9

P: n/a
Sue <Su***@comcast.net > wrote in
news:pn********************************@4ax.com:
The instructor said take the total of the string using the indexOf()
What does "the total of the string" mean? I can understand what "the total
of a list of numbers" means, but that concept doesn't really extend to
strings. Are you talking about the *length* of a string? If so, every
single Javascript string has a rather obviously-named method for finding
that, but it has nothing to do with indexOf, which gives the location of
the *first* (leftmost, in Western languages) occurrence of a substring.
method and subtract four to varify the period. The following code will
tell me what position the period is from the front but I have been
unable to determine how to write the formula for subtracting 4 from
the total of the string and then read that character.

// validate the e-mail
var RegeMail=document.Register.eMail.value
var atSign = RegeMail.indexOf("@")
var Period=document.Register.eMail.value
var PPeriod = Period.indexOf('.');
As above, that gives you the position of the *first* occurrence of a
period.
alert('The Period is at position '+PPeriod+'.');

if (RegeMail == " " || atSign == -1) {
That first test asks if the value of RegeMail consists of a single space.
I don't think that's what you want to check for.
alert("Please enter your e-mail address")
document.Register.eMail.value = " "
That sets the value to a single space.
document.Register.eMail.focus()
}
else {

Jul 20 '05 #10

P: n/a
Sue
I am sorry but as the subject line says, I am a new JavaScript
Student. I thought I might be able to get some help here but so far my
posting have proven fruitless.

The instructor told me to use the indexOf() method to determine if the
period is the fourth from the last character in a string. He showed me
a web page that used this method. Regardless of the other
possibilities this is what I have to check for. As a new JavaScript
student the other possibilities that can be checked for are currently
beyond my grasp.

If anyone can write the code using the indexOf() to verify that there
is a period in the fourth from the last position, I would appreciate
you posting it. Other than checking for the @ in an email address and
that the period is the forth from the last character in an email
address, nothing else matters to the instructor and at this point me
either.

I understand the logic of using the indexOf() , however, I just can
not put it into the syntax the JavaScript requires.



On 26 Nov 2003 05:40:34 GMT, Eric Bohlman <eb******@earthlink.net>
wrote:
Sue <Su***@comcast.net > wrote in
news:pn********************************@4ax.com :
The instructor said take the total of the string using the indexOf()


What does "the total of the string" mean? I can understand what "the total
of a list of numbers" means, but that concept doesn't really extend to
strings. Are you talking about the *length* of a string? If so, every
single Javascript string has a rather obviously-named method for finding
that, but it has nothing to do with indexOf, which gives the location of
the *first* (leftmost, in Western languages) occurrence of a substring.
method and subtract four to varify the period. The following code will
tell me what position the period is from the front but I have been
unable to determine how to write the formula for subtracting 4 from
the total of the string and then read that character.

// validate the e-mail
var RegeMail=document.Register.eMail.value
var atSign = RegeMail.indexOf("@")
var Period=document.Register.eMail.value
var PPeriod = Period.indexOf('.');


As above, that gives you the position of the *first* occurrence of a
period.
alert('The Period is at position '+PPeriod+'.');

if (RegeMail == " " || atSign == -1) {


That first test asks if the value of RegeMail consists of a single space.
I don't think that's what you want to check for.
alert("Please enter your e-mail address")
document.Register.eMail.value = " "


That sets the value to a single space.
document.Register.eMail.focus()
}
else {


Jul 20 '05 #11

P: n/a
JRS: In article <i1********************************@4ax.com>, seen in
news:comp.lang.javascript, Sue <Su***@comcast.net> posted at Wed, 26 Nov
2003 00:02:34 :-
The indexOf() method is what I need to use to validate the fourth from
the last character. This is my first JavaScript class so I do not need
the advance validations of email addresses. The last four characters
of the address can be all periods. All I have to do is validate that
the fourth from the last is a period.

...

On Tue, 25 Nov 2003 13:01:16 +0000, Dr John Stockton
<sp**@merlyn.demon.co.uk> wrote:
JRS: In article <h8********************************@4ax.com>, seen in
news:comp.lang.javascript, Sue <Su***@comcast.net> posted at Tue, 25 Nov
2003 04:34:06 :-
Is there someone
...


You should take a copy of the FAQ of this newsgroup, and read through
it. Among other things, it will tell you that, in a response, you
should quote only that which needs to be quoted, and you should respond
to what is quoted underneath it. Adherence will help others to maintain
an interest in the case.

It will also pre-empt many possible questions.

<FAQENTRY> The FAQ should also say that, when asking about classwork, a
poster needs to state that it is classwork (and to present what he/she
has done so far, which you did). </FAQENTRY>

You have been told a proper way of locating the last period in a string;
now that you have indicated that you [think that you] must use indexOf,
one can tell you that you can do that either by reading the whole string
forwards and noting every period, or by reading it backwards until you
find one. Ensure that, if there is no period, failure to find one is
benign.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #12

P: n/a
Lee
Sue said:

I am sorry but as the subject line says, I am a new JavaScript
Student. I thought I might be able to get some help here but so far my
posting have proven fruitless.

The instructor told me to use the indexOf() method to determine if the
period is the fourth from the last character in a string. He showed me
a web page that used this method.


Here are two functions. One uses indexOf() to determine if there is
a period at the fourth from last position. The other uses charAt()
to determine if there is a period at the fourth from last position.
You can see for yourself which is easier to write and to understand.
Note that both functions assume that there are some minimum number
of characters in the string, so neither is really useful in the real
world.

function validateUsingIndexOf(){
var RegeMail=document.Register.eMail.value;
var fourFromEnd=RegeMail.length-4;
var i=0;
while(i>=0 && i!=fourFromEnd){
i=RegeMail.indexOf(".",i+1);
}
if(i<0){
alert("invalid Email");
}else{
alert("Email is ok");
}
}

function validateUsingCharAt(){
var RegeMail=document.Register.eMail.value;
if(RegeMail.charAt(RegeMail.length-4)!="."){
alert("invalid Email");
}else{
alert("Email is ok")
}
}

Jul 20 '05 #13

P: n/a
Sue

I just wanted to let you guys know I figured out how to validate the
period as the fourth from the last character in an email address.

Once I figured it out, it looks so simple. However, since I am taking
this class over the Internet with no one to explain anything to me it
is extremely difficult to understand. Thanks
// validate the e-mail
var RegeMail=document.Register.eMail.value
var atSign = RegeMail.indexOf("@")

var Period=document.Register.eMail.value
var PPeriod = Period.indexOf('.');
var LPeriod = Period.length - 4

if (RegeMail == " " || atSign == -1 || LPeriod !=
PPeriod) {
alert("Please enter a valid e-mail address")
document.Register.eMail.value = " "
document.Register.eMail.focus()
}
else {





On 26 Nov 2003 10:30:54 -0800, Lee <RE**************@cox.net> wrote:
Sue said:

I am sorry but as the subject line says, I am a new JavaScript
Student. I thought I might be able to get some help here but so far my
posting have proven fruitless.

The instructor told me to use the indexOf() method to determine if the
period is the fourth from the last character in a string. He showed me
a web page that used this method.


Here are two functions. One uses indexOf() to determine if there is
a period at the fourth from last position. The other uses charAt()
to determine if there is a period at the fourth from last position.
You can see for yourself which is easier to write and to understand.
Note that both functions assume that there are some minimum number
of characters in the string, so neither is really useful in the real
world.

function validateUsingIndexOf(){
var RegeMail=document.Register.eMail.value;
var fourFromEnd=RegeMail.length-4;
var i=0;
while(i>=0 && i!=fourFromEnd){
i=RegeMail.indexOf(".",i+1);
}
if(i<0){
alert("invalid Email");
}else{
alert("Email is ok");
}
}

function validateUsingCharAt(){
var RegeMail=document.Register.eMail.value;
if(RegeMail.charAt(RegeMail.length-4)!="."){
alert("invalid Email");
}else{
alert("Email is ok")
}
}


Jul 20 '05 #14

P: n/a
Sue <Su***@comcast.net > wrote in
news:n8********************************@4ax.com:

I just wanted to let you guys know I figured out how to validate the
period as the fourth from the last character in an email address.
Unfortunately, you didn't.
// validate the e-mail
var RegeMail=document.Register.eMail.value
var atSign = RegeMail.indexOf("@")

var Period=document.Register.eMail.value
var PPeriod = Period.indexOf('.');
var LPeriod = Period.length - 4

if (RegeMail == " " || atSign == -1 || LPeriod !=
PPeriod) {
alert("Please enter a valid e-mail address")
document.Register.eMail.value = " "
document.Register.eMail.focus()
}
else {


Try this on the address "a.b.com"; it has a period in the fourth-from-last
position, but your code will consider it invalid.
Jul 20 '05 #15

P: n/a
Lee
Sue said:


I just wanted to let you guys know I figured out how to validate the
period as the fourth from the last character in an email address.

Once I figured it out, it looks so simple. However, since I am taking
this class over the Internet with no one to explain anything to me it
is extremely difficult to understand. Thanks
// validate the e-mail
var RegeMail=document.Register.eMail.value
var atSign = RegeMail.indexOf("@")

var Period=document.Register.eMail.value
var PPeriod = Period.indexOf('.');
var LPeriod = Period.length - 4

if (RegeMail == " " || atSign == -1 || LPeriod !=
PPeriod) {
alert("Please enter a valid e-mail address")
document.Register.eMail.value = " "
document.Register.eMail.focus()
}


That may be the solution that your instructor expects.
If he told you to use indexOf(), he probably doesn't know any better.

Jul 20 '05 #16

P: n/a
JRS: In article <3n********************************@4ax.com>, seen in
news:comp.lang.javascript, Sue <Su***@comcast.net> posted at Wed, 26 Nov
2003 17:30:10 :-

The instructor told me to use the indexOf() method to determine if the
period is the fourth from the last character in a string. He showed me
a web page that used this method. Regardless of the other
possibilities this is what I have to check for. As a new JavaScript
student the other possibilities that can be checked for are currently
beyond my grasp.

If anyone can write the code using the indexOf() to verify that there
is a period in the fourth from the last position, I would appreciate
you posting it. Other than checking for the @ in an email address and
that the period is the forth from the last character in an email
address, nothing else matters to the instructor and at this point me
either.


Please learn, as you have been asked, to quote correctly.

You have been told about lastIndexOf(), which is what the instructor
ought to have been saying.

Consider -

S = "abcde.123"
Q = S.length
T = S.substring(Q-4)
OK = !T.indexOf(".")

which can be compacted. I believe that it is adequate but not
foolproof. Function charAt could be used.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #17

P: n/a
JRS: In article <n8********************************@4ax.com>, seen in
news:comp.lang.javascript, Sue <Su***@comcast.net> posted at Wed, 26 Nov
2003 19:56:06 :-

I just wanted to let you guys know I figured out how to validate the
period as the fourth from the last character in an email address.
You still need, if you wish favourable consideration to continue, to
learn how to quote correctly in Usenet News.

Evidently you have not yet learned, like so many others, that a tester
needs to be both designed and checked for giving a positive result both
when and only when it should.
// validate the e-mail
var RegeMail=document.Register.eMail.value
var atSign = RegeMail.indexOf("@")

var Period=document.Register.eMail.value
var PPeriod = Period.indexOf('.');
var LPeriod = Period.length - 4

if (RegeMail == " " || atSign == -1 || LPeriod !=
PPeriod) {
alert("Please enter a valid e-mail address")
document.Register.eMail.value = " "
document.Register.eMail.focus()
}
else {


Your code fails the requirement on a@b.c.def & Su****@comcast.net
Your code accepts abc.d@f too
also @@@.@@@

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 20 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.