开发者

Please define me this short code of JavaScript

I am new to JavaScript. I am making a form in html and validating it via JS; recently I found a code for email validation from web surfing. I understand the basic purposes of the functions used in this code but I am not able to understand the logic well. Please if anyone can make me understand the logic of this code easily, its become hectic for me a lot, please go step wise in your answer while explaining. The code is:

if (document.formname.fieldname.value.length >0) {
  i=document.formname.fieldname.value.indexOf("@")
  j=document.formname.fieldname.value.indexOf(".",i)
  k=document.formname.fieldname.value.indexOf(",")
  kk=document.formname.fieldname.value.indexOf(" ")
  jj=document.formname.fieldname.value.lastIndexOf(".")+1
  len=document.formname.fieldname.value.length

 开发者_运维技巧 if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
  }
  else {
   alert("Please enter an exact email address.\n" +
  document.formname.fieldname.value + " is invalid.");
  return false;
  }

 }


Here's a quickly reformatted and commented version -- hope this helps. Validates nicely with JSLint.

function cleanEmail(email) {
    if (email.length > 0) {
        var atPos = email.indexOf("@"); // Position of the at character.
        var dotPos = email.indexOf(".", atPos); // First dot after the at sign
        var commaPos = email.indexOf(","); // Comma position. Used later to ensure that there is no commas in the string.
        var spacePos = email.indexOf(" "); // Space position. Used later to ensure that there is no spaces in the string.
        var lastDotPos = email.lastIndexOf(".") + 1; // Position _after_ last dot.
        var len = email.length;

        if (
            (atPos > 0) && // At must be at least the second character...
            (dotPos > 2) && // There must be at least one character between the at and the first dot after at
            (commaPos == -1) && // There must be no commas
            (spacePos == -1) && // Nor spaces
            (len - lastDotPos >= 2) && // There must be two characters after the last dot
            (len - lastDotPos <= 3) // But no more than three
        ) {
            // It's valid!
            return true;
        }
        else {
            alert("Please enter an exact email address.\n" + email + " is invalid.");
            return false;
        }
    }
}


Please note: This is probably a very lame way of validating email string. Neverthless, below is explaination of the code.

The code is checking for the Valid email ID. Its first parsing the emailString to finding indexes of these characters ( '@','.',',' ) and then checks for few validations.

i=document.formname.fieldname.value.indexOf("@") = Finding index of '@'
j=document.formname.fieldname.value.indexOf(".",i) = Finding index of '.'
k=document.formname.fieldname.value.indexOf(",") = Finding index of ','
kk=document.formname.fieldname.value.indexOf(" ") = Finding index of space
jj=document.formname.fieldname.value.lastIndexOf(".")+1 = finding the last index of '.'
len=document.formname.fieldname.value.length = getting length of string

((i>0) && (j>(i+1)) = Check @ is present in string and '.' is present after @
(k==-1) && (kk==-1) = characters ',' and space are not present in the string
(len-jj >=2) && (len-jj<=3) = There are some string present between @ and end of string and there are 3 characters after last '.' (probably checking like .com, .org etc


document.formname.fieldname must satisfy all these conditions:

'@' must be at least at index 1

Valid: a@ aaaa@

'.' must be at least at index 2 from @ sign

Valid: @b.ccc @aaaabbbb.ccc

no comma or space

Invalid: a,b.ccc a.b c cc

last occurrence of '.' must be followed by 2-3 characters

Valid: a.bb c.d.eee
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜