
/*
#######################################################################
#
#   $Author: fdavis $
#   $Locker: fdavis $
#
#     $Date: 1999/01/15 23:40:16 $
#
#   $Source: C:\\Documents\\RCS\\C\\InetPub\\wwwroot\\cgi-bin\\mask.js,v $
# $Revision: 1.1 $
#    $State: Exp $
#
#      $Log: mask.js,v $
#      Revision 1.1  1999/01/15 23:40:16  fdavis
#      Initial revision
#
#      Revision 1.1  1998/10/29 15:41:20  fdavis
#      Initial revision
#
#
#######################################################################

Example of use:

//#######################################################################
//# function isDataValid()
//#######################################################################
 function isDataValid(oForm)
   {
        if (!isPopulated(oform.shopper_name.value, 4))
           {
                alert("Your full name must be in the Name field!");
                oform.shopper_name.focus();
                return false;
           }
        if (!isPopulated(oform.shopper_street.value, 4))
           {
                alert("Please provide your full street address!");
                oform.shopper_street.focus();
                return false;
           }
        if (!isPopulated(oform.shopper_city.value, 2))
           {
                alert("Please provide the name of your city!");
                oform.shopper_city.focus();
                return false;
           }
        if (!isNumberChar (oform.shopper_zip.value, 5))
           {
                alert("Please provide your zip code!");
                oform.shopper_zip.focus();
                return false;
           }
        if (!CheckPhoneNumber(oform.shopper_phone.value))
           {
                alert("Please provide your phone number including area code!");
                oform.shopper_phone.focus();
                return false;
           }

        var semailCheckReturn;
        semailCheckReturn = emailCheck(oform.shopper_email.value)
        if (semailCheckReturn != "true")
           {
                alert(semailCheckReturn);
                oform.shopper_email.focus();
                return false;
           }

     // All required data exists //
     return true;
   }
 <FORM ACTION="csguseradd.pl#last" method="POST" NAME="theform" onsubmit="return isDataValid(this)">

 <!-- General form stuff goes here -->

 </FORM>
*/

//#######################################################################
//#  function mask (InString, Mask, Debug)
//#######################################################################
function mask (InString, Mask, Debug)  {

 LenStr = InString.length;
 LenMsk = Mask.length;

 if ((LenStr==0) || (LenMsk==0))
    {
     if (Debug) alert("No Length!");
     return false;
    }

 if (LenStr!=LenMsk)
    {
     if (Debug) alert("String and Mask of inequal lengths!\n" + LenStr + " <> " + LenMsk);
     return false;
    }

 TempString="";

 for (Count=0; Count<=InString.length; Count++)  {

  StrChar = InString.substring(Count, Count+1);
  MskChar = Mask.substring(Count, Count+1);

  if (MskChar=='#') {
   if(!isNumberChar(StrChar))
    {
     if (Debug) alert("isNumberChar: " + Count);
     return false;
    }
  }else if (MskChar=='?') {
   if(!isAlphabeticChar(StrChar))
    {
     if (Debug) alert("isAlphabeticChar: " + Count);
     return false;
    }
  }else if (MskChar=='!') {
   if(!isNumOrChar(StrChar))
    {
     if (Debug) alert("isNumOrChar: " + Count);
     return false;
    }
  }else if (MskChar=='*') {
  }else{
   if (MskChar!=StrChar)
    {
     if (Debug) alert("Not Equal: " + Count);
     return false;
    }
  }
 }
 return true;
}

//#######################################################################
//#  function isAlphabeticChar (InString)
//#######################################################################
function isAlphabeticChar (InString)  
   {
    if(InString.length != 1)
       return (false);
 
    InString=InString.toLowerCase();
 
    RefString="abcdefghijklmnopqrstuvwxyz";
 
    if (RefString.indexOf (InString.toLowerCase(), 0) == -1)
       return (false);
 
    return (true);
   }

//#######################################################################
//#  function isNumberChar (sString)
//#######################################################################
function isNumberChar (sString, iMinLength)  
   {
    var GoodChars = "0123456789.- ";
    
    return GenericCheck(sString, GoodChars, iMinLength);
   }

//#######################################################################
//#  function isNumOrChar (sString)
//#######################################################################
function isNumOrChar (sString)  
   {
    var GoodChars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    return GenericCheck(sString, GoodChars, 0);
    
   }

//#######################################################################
//#  function isStringPopulated(inputString)
//#######################################################################
function isPopulated(sString, iMinLength)
{

  if (sString == "")
     {
      return false;
     }
 
  if (sString.length < iMinLength)
     {
      return false;
     }
 
  for (var i = 0; i < sString.length; i++)
     {
      var ch = sString.substring(i, i + 1);
      if (ch != ' ')
         {
          return true;
         }
     }

  return false;
}

//#######################################################################
//#  function Monify(value)
//#######################################################################
function Monify(value)
   {
    var str = "" + Math.round(value*100); var len = str.length;
    return (str=="0")?"0.00":(str.substring(0,len-2)+"."+str.substring(len-2, len));
   }

//#######################################################################
//#  function istime(obj, retval)
//#
//# istime - validate time format (hh:mm:ss)              */
//# note: requires function alltrim() in file alltrim.cfm */
//# it also requires nochars() in file isdate.cfm */
//#
//#######################################################################
function istime(obj,retval) {
 if (retval) {
  retval=false
  var hours, minutes, seconds
  obj=alltrim(obj)
  if (obj.value.length==5) { /* seconds are optional */
	   obj.value+=":00"
  }
     
  if (obj.value!=""){
	   /* check formatting of string */
	   if (obj.value.length != 8 || obj.value.substring(2,3)!=":" || obj.value.substring(5,6)!=":") 
              {
	      }
	   else {
		  /* Hours must be between 0 and 24 */ 
		  hours=obj.value.substring(0,2)
		  if (nochars(hours) && parseInt(hours) >= 0 && parseInt(hours) < 24) {
			hours=parseInt(hours)
			minutes=obj.value.substring(3,5)
			if (nochars(minutes)) {
			      minutes=parseInt(minutes)
			      if (minutes >=0 && minutes <=59) {
				    seconds=obj.value.substring(6,8)
				    /* seconds between 0 and 59 */
				    if (nochars(seconds) && parseInt(seconds) >= 0 && parseInt(seconds)<=59) {
					  retval=true
				    }
			      }
			
		        }
		 }
	   }
	   if (!retval) {
		 alert("Invalid : You must enter a time 24 hour format (hh:mm:ss) ¬or (hh:mm)")
	   }
  }
 }
 return retval
}

//#######################################################################
//#
//#  function CheckPhoneNumber(sString)
//#
//#######################################################################
function CheckPhoneNumber(sString) 
   {
    var GoodChars = "0123456789()-.+xX ";
    
    return GenericCheck(sString, GoodChars, 10);
   }

//#######################################################################
//#
//#  function GenericCheck(sString, GoodChars, iMinLength)
//#
//#######################################################################
function GenericCheck(sString, GoodChars, iMinLength) 
   {
    var valid = 1
    var i = 0

    if (sString == "") 
       {
	// Return false if number is empty
	valid = 0
        return valid
       }

    if (sString.length < iMinLength)
       {
	// Return false if number doesn't at least include iMinLength characters
	valid = 0
        return valid
       }

    for (i = 0; i <= sString.length -1; i++) 
       {
        if (GoodChars.indexOf(sString.charAt(i)) == -1) 
           {
            // Note: Remove the comments from the following line to see this
            // for loop in action.
            // alert(sString.charAt(i) + " is no good.")
            valid = 0
           } // End if statement
       } // End for loop

    return valid
   }

//#######################################################################
//#
//#  function  emailCheck (emailStr) 
//#
//#######################################################################
function emailCheck(emailStr) {

var sReturnString;

sReturnString = "";

/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/

/* 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="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"

/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"

/* 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="(\"[^\"]*\")"

/* 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=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/

/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'

/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"

// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")

/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)

if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	sReturnString += "   Email address seems incorrect (check @ and .'s)\n"
        return sReturnString;
}

var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    sReturnString += "   The username doesn't seem to be valid.\n"
    return sReturnString;
}

/* 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=domain.match(ipDomainPat)

if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        sReturnString += "   Destination IP address is invalid!\n"
                return sReturnString;
	    }
    }
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)

if (domainArray==null) {
	sReturnString += "   The domain name doesn't seem to be valid.\n"
        return sReturnString;
}

/* 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), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.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.
   sReturnString += "   The address must end in a three-letter domain, or two letter country.\n"
   return sReturnString;
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   sReturnString += "   This address is missing a hostname!\n"
   return sReturnString;
}

// Here we return true or an error string so the calling function must
// do an explicit check.
if (sReturnString)
   {
    return sReturnString;
   }
else
   {
    return "true";
   }
}
