Jump to content

Need help with simple javascripting (coursework)


kas

Recommended Posts

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>

Link to comment
Share on other sites

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

 

As much as I dislike doing other people's homework, the following should suffice:

<!-- Note: language attribute is deprecated.  Use type attribute instead. -->
<script type="text/javascript">
   /**
    *Function that validates an e-mail address input
    *
    *Arguments:
    *None.
    *
    *Return value:
    *None (alert message upon failure).
    */

   function validateEmail()
   {
      var emailAddr = emailInput.value; //value of the input
      var hasAt = 0; //does the value have an '@'?

      for(var i = 0; i < emailAddr.length; i++) //iterate through the address to check for the '@'
      {
         if(emailAddr.charAt(i) == "@") //if the address has an '@'
         {
            hasAt++; //set it to true
         }
      }

      //since hasAt is an integer, it can be treated as a boolean
      if(!hasAt || emailAddr.charAt(0) != "") //if there's no '@' or the first character is blank
      {
         alert("Your e-mail address is not valid."); //alert validation failure
      }
   }
</script>

 

You should be careful about using charAt() to check a value's length.  Just because the first character in a string is blank, it doesn't necessarily mean the entire form input is blank.  You should probably check to see if the entire length of the value (in this case, emailAddr.length) is equal to 0.  Not only that, but even if an inputed value has an '@', that doesn't mean it's a valid e-mail address, either.  I mean:

@hello_kitty

Isn't a valid address, yet your function will treat it as one.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.