Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: String validation


Message #1 by bjarni@v... on Mon, 20 Nov 2000 16:49:13 -0000
This is a multi-part message in MIME format.

------=_NextPart_000_045B_01C053D0.C33F9180
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

hi,

using Regular Expressions is very good.
but for browsers below versions 4 that will not work

try this function

do tell me if u like this one :-)

jigs

 function isValidEmail(txt)
 {
  // check (1) one "@" (2) one "." (3) only one "@" (4) one "." after 
"@" (5) valid starting (6) valid characters
  var ret =3D true
  var validstart =3D "abcdefghijklmnopqrstuvwxyz0123456789";
  var valids =3D "abcdefghijklmnopqrstuvwxyz0123456789-_";
  var email =3D txt.toLowerCase();
  var emailarray =3D email.replace("@",".").split(".")
  if (email.indexOf("@") < 1) ret =3D false; // @ must be there
  if (email.indexOf(".") < 1) ret =3D false; // . must be there
  if ((email.lastIndexOf(".") - email.indexOf("@")) < 2) ret =3D false; 
// @ should be only once
  if (email.lastIndexOf(".")  < email.indexOf("@")) ret =3D false; // 
atleast one . after @
  if (ret =3D=3D true)
   for (var i=3D0 ; i < emailarray.length ; i++)
   {
    if (ret =3D=3D true)
    {
     if (validstart.indexOf(emailarray[i].charAt(0)) < 0) ret =3D false 
// check start letter
     if 
(validstart.indexOf(emailarray[i].charAt(emailarray[i].length-1)) < 0) 
ret =3D false // check last letter
     for (var j=3D0 ; j < emailarray[i].length ; j++) // check each 
character
      if (ret =3D=3D true)
       if (valids.indexOf(emailarray[i].charAt(j)) < 0) ret =3D false
    }
   }
  return ret;
 }

  ----- Original Message -----
  From: CebuCity Undernet Chatters Community
  To: javascript
  Sent: Tuesday, November 21, 2000 1:40 AM
  Subject: [javascript] Re: String validation


  Eggert,


  Here's a sample email checker function i wrote, try this out..pass a 
string to the function...hope tis helps u..


  Gerald

  <!-- Begin Email Check
  function emailCheck (emailStr) {
  /* The following pattern is used to check if the entered e-mail 
address
     fits the user@d... format.  It also is used to separate the 
username
     from the domain. */
  var emailPat=3D/^(.+)@(.+)$/
  /* The following string represents the pattern for matching all 
special
     characters.  We don't want to allow special characters in the 
address.
     These characters include ( ) < > @ , ; : \ " . [ ]    */
  var specialChars=3D"\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
  /* The following string represents the range of characters allowed in 
a
     username or domainname.  It really states which chars aren't 
allowed. */
  var validChars=3D"\[^\\s" + specialChars + "\]"
  /* The following pattern represents the range of characters allowed as
     the first character in a valid username or domain.  I just made it
     the same as above, but if you want to add a different constraint,
     you would change it here. */
  var firstChars=3DvalidChars
  /* The following pattern applies if the "user" is a quoted string (in
     which case, there are no rules about which characters are allowed
     and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
     is a legal e-mail address. */
  var quotedUser=3D"(\"[^\"]*\")"
  /* The following pattern applies for domains that are IP addresses,
     rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
     e-mail address. NOTE: The square brackets are required. */
  var ipDomainPat=3D/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
  /* The following string represents at atom (basically a series of
     non-special characters.) */
  var atom=3D"(" + firstChars + validChars + "*" + ")"
  /* The following string represents one word in the typical username.
     For example, in john.doe@s..., john and doe are words.
     Basically, a word is either an atom or quoted string. */
  var word=3D"(" + atom + "|" + quotedUser + ")"
  // The following pattern describes the structure of the user
  var userPat=3Dnew RegExp("^" + word + "(\\." + word + ")*$")
  /* The following pattern describes the structure of a normal symbolic
     domain, as opposed to ipDomainPat, shown above. */
  var domainPat=3Dnew RegExp("^" + atom + "(\\." + atom +")*$")


  /* Finally, let's start trying to figure out if the supplied address 
is
     valid. */

  /* Begin with the course pattern to simply break up user@d... into
     different pieces that are easy to analyze. */
  var matchArray=3DemailStr.match(emailPat)
  if (matchArray=3D=3Dnull) {
    /* Too many/few @'s or something; basically, this address doesn't
       even fit the general mould of a valid e-mail address. */
  alert("Email address seems incorrect (check @ and .'s)")
  return false
  }
  var user=3DmatchArray[1]
  var domain=3DmatchArray[2]

  // See if "user" is valid
  if (user.match(userPat)=3D=3Dnull) {
      // user is not valid
      alert("The username doesn't seem to be valid.")
      return false
  }
  /* if the e-mail address is at an IP address (as opposed to a symbolic
     host name) make sure the IP address is valid. */
  var IPArray=3Ddomain.match(ipDomainPat)
  if (IPArray!=3Dnull) {
      // this is an IP address
    for (var i=3D1;i<=3D4;i++) {
      if (IPArray[i]>255) {
          alert("Destination IP address is invalid!")
  return false
      }
      }
      return true
  }

  // Domain is symbolic name
  var domainArray=3Ddomain.match(domainPat)
  if (domainArray=3D=3Dnull) {
  alert("The domain name doesn't seem to be valid.")
      return false
  }
  /* domain name seems valid, but now make sure that it ends in a
     three-letter word (like com, edu, gov) or a two-letter word,
     representing country (uk, nl).
     If there's a country code at the end of the address, the full 
domain
     must include a hostname and category (e.g. host.co.uk or 
host.pub.nl).
     If it ends in a .com or something, make sure there's a hostname.*/

  /* Now we need to break up the domain to get a count of how many atoms
     it consists of. */
  var atomPat=3Dnew RegExp(atom,"g")
  var domArr=3Ddomain.match(atomPat)
  var len=3DdomArr.length
  if (domArr[domArr.length-1].length<2 ||
      domArr[domArr.length-1].length>3) {
     // the address must end in a two letter or three letter word.
     alert("The address must end in a three-letter domain, or two letter 
country.")

     return false
  }

  /* If it ends in a country code, we want to make sure there are at
     least 2 atoms preceding it (representing host and category (i.e.
     com, gov, etc.)) */
  if (domArr[domArr.length-1].length=3D=3D2 && len<3) {
     var errStr=3D"This address ends in two characters, which is a 
country"
     errStr+=3D" code.  Country codes must be preceded by "
     errStr+=3D"a hostname and category (like com, co, pub, pu, etc.)"
     alert(errStr)
     return false
  }

  /* If it just ends in .com, .gov, etc., make sure there's a host name.
     This case can never actually happen because earlier checks take
     care of this implicitly, but we'll do it anyway. */
  if (domArr[domArr.length-1].length=3D=3D3 && len<2) {
     var errStr=3D"This address is missing a hostname!"
     alert(errStr)
     return false
  }
  // If we've gotten this far, everything's valid!
  return true;
  }
  //  End -->


  ---
  NEED TECHNICAL TIPS, TOOLS, AND INSIGHTS?  Is FREE okay?
  Visit EarthWeb for the latest in IT Management, Software Development,
  Web Development, Networking & Communications, and Hardware & Systems.  

  Click on http://www.earthweb.com for FREE articles, tutorials,
  and discussions from the experts.
$subst('Email.Unsub')





  Return to Index