/    Sign up×
Community /Pin to ProfileBookmark

Any idea what this code is?

I have been given a piece of code that validates passwords, but need to edit the code with correct variable names and indentations etc. here is the original code:

<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(n1 – 1) != “7”)
{
alert( “Your e-mail address is incorrect”);
}
}

</script>

any ideas what the variables n1 and n2 are/ what they do?
struggling to figure it out

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@pwinfreyApr 26.2011 — n1 is a loop variable that begins at 0 and increments by 1 until it reaches the length of the e-mail address. n2 is set to 1 if there is an "@" symbol found within the string and remains at 0 if it does not. I wouldn't suggest using this logic because an e-mail address could contain an "@" symbol and still be invalid.
Copy linkTweet thisAlerts:
@WolfShadeApr 26.2011 — Better off using a Regular Expression mask for this.

<i>
</i>var valid = true;
var emailf=/^[a-zA-Z0-9w-_.]*@[a-zA-Z0-9w-_.]+.[a-z]{2,}$/i; // [email protected]; [email protected]; [email protected]

var eaddress = document.forms['form name'].formfield.value;
if (!(emailf.test(eaddress))) {
valid = false;
}
Copy linkTweet thisAlerts:
@RN050292authorApr 26.2011 — ok thanks, i have edited the code so far with correct indentations and an attempt at meaninful identifiers. as follows, but the n1 and n2 variables are supposedly "not very meaningful", what would be best to name them?

or is it ok as it is?

<i>
</i>
&lt;script language = "javascript"&gt;

<i> </i>function validateEmail()
<i> </i>{
<i> </i> var strEmail
<i> </i> var n1;
<i> </i> var n2 = 0;

<i> </i> strEmail = document.GetElementById("txtB").value

<i> </i> //Check validity of email entered

<i> </i> for(n1= 0; n1&lt; strEmail.length; n1++)
<i> </i> {

<i> </i> if (strEmail.charAt(n1) == "@" )
<i> </i> {
<i> </i> n2++;
<i> </i> }
<i> </i> }

<i> </i> if ((n2 != 1) || strEmail.charAt(0) != "7"))
<i> </i> {
<i> </i> alert( "Your e-mail address is incorrect&amp;#8221;);
<i> </i> }
<i> </i>}

&lt;/script&gt;
×

Success!

Help @RN050292 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...