...

View Full Version : Need help with simple javascripting (coursework)



kas
05-22-2008, 11:48 AM
This is supposed to be a script to validate a email address in a textbox. Can anyone help me edit this with the following:
• correct indentation
• meaningful identifiers (variable, function and control names)
• correct use of Hungarian notation for variable identifiers the textbox identifier
• suitable comments
• appropriate spacing


<script language = "javascript">

function a()
{
var v1 = B.value;
var n1;
var n2 = 0;

for (n1= 0; n1 < v1.length; n1++)
{
if (v1.charAt(n1) == "@" )
{
n2++;
}
}

if ((n2 != 1) || v1.charAt(0) != "")
{
alert( "Your e-mail address is incorrect”);
}
}

</script>

rafiki
05-22-2008, 12:09 PM
Why not use This (http://www.ilovejackdaniels.com/php/email-address-validation/) email validator

kas
05-22-2008, 12:21 PM
I have to correct the one given.

Philip M
05-22-2008, 12:35 PM
See forum rules re homework. It is not in your interests in the long run if other people do your homework for you. This is a very, very simple assignment and if you have paid attention in class you ought to be able to do it easily.

Hint - simply count the @ signs and if result is zero or more than one then the email address is not valid.


111,111,111 x 111,111,111 = 12,345,678,987,654,321

huskie
04-05-2011, 03:23 PM
I am also having the same issue and if it was as simple as paying attention in lectures we wouldn't be here asking for help. The content has been rushed through so quickly only a few with prior coding experience can resolve these queries without support.

Some assistance would really be of great help.

The best I have got to is this:

<script type="text/javascript">
function checkEmail()
{

var strEmail = txtB.value;
var intCount;
var intInput = 0;

for (intCount= 0; intInput < strEmail.length; intCount++)
{
if (strEmail.charAt(intCount) == "@" )
{
intInput++;
}
}
if ((intInput != 1) || strEmail.charAt(intCount - 1) != "7")
{
alert( "Your e-mail address is incorrect”);
}
}
</script>
<body>
<P align="left"><input name="cmdCheckEmail" id="cmdCheckEmail" onclick="checkEmail() "type="submit" value="Check email"/>
<P align="left"><strong>Type your email address here:</strong><input name="txtB" type="text" id="txtB" />
</body>
</html>

Kor
04-05-2011, 03:28 PM
Once again: we don't do the other people's homework. Don't mention that you have dug a 2 years old thread.

Philip M
04-05-2011, 03:54 PM
Look very carefully at
alert( "Your e-mail address is incorrect”);
and you will find a syntax error.

for (intCount= 0; intInput < strEmail.length; intCount++)
See anything wrong here?

Rather than your loop you could use the indexOf() and lastIndexOf() methods.

huskie
04-05-2011, 10:40 PM
Thanks for the hints, I'll take another look through.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum