请定义我这个简短的Javascript代码!

时间:2010-10-27 10:53:49

标签: javascript-events

我是Javascript的新手......我正在制作一个html格式并通过JS验证它...最近我发现了一个用于网上冲浪的电子邮件验证代码......我理解所用函数的基本用途这段代码,但我无法理解逻辑...请,如果任何人可以让我轻松理解这段代码的逻辑......对我来说变得忙碌......请在解释时逐步明智地回答你的问题。 ..code是;

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;
  }

 }

我将非常感谢你们......焦急地等待回复......问候!!!

3 个答案:

答案 0 :(得分:2)

这是一个快速重新格式化和评论的版本 - 希望这会有所帮助。使用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;
        }
    }
}

答案 1 :(得分:1)

请注意:这可能是验证电子邮件字符串的一种非常蹩脚的方式。无论如何,下面是对代码的解释。

代码正在检查有效的电子邮件ID。它首先解析emailString以查找这些字符的索引('@','.',','),然后检查几个验证。

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

答案 2 :(得分:0)

document.formname.fieldname必须满足所有这些条件:

'@'必须至少在索引1

Valid: a@ aaaa@

''必须至少在@ sign

的索引2处
Valid: @b.ccc @aaaabbbb.ccc

没有逗号或空格

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

最后一次出现'。'必须后跟2-3个字符

Valid: a.bb c.d.eee