Please define me this javascript shortcode!

I am new to Javascript ... I am making a form in html and validating it through JS ... I recently found code for checking email from web surfing ... I understand the main purpose of the functions used in this code, but I'm not able to understand the logic well ... please, if someone can make me understand the logic of this code easily ... it gets troubling for me a lot ... please step by step in your answer explaining ... 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;
  }

 }

I will be very grateful to you, people ... I look forward to hearing ... Sincerely !!!

+3
source share
3 answers

Here's a quick reformatted and commented version - hope this helps. Works well 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 valid!
            return true;
        }
        else {
            alert("Please enter an exact email address.\n" + email + " is invalid.");
            return false;
        }
    }
}
+2

: , , . , .

. 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
+1

document.formname.fieldname must satisfy all of these conditions:

'@' must be at least with index 1

Valid: a@ aaaa@

'' must be at least with index 2 from the @ sign

Valid: @b.ccc @aaaabbbb.ccc

no comma or space

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

last occurrence of '.' 2-3 characters must follow

Valid: a.bb c.d.eee
0
source

Source: https://habr.com/ru/post/1771687/


All Articles