/* Form handling scripts - called from include file forms-script.jsi - same for each form, configured using hidden fields within form. PIPEX Internet Ltd. 1999-2001 :: Functions in this file: verifyUsername(username) makeTitleCase(frmObj) emailCheck(emailStr) verifyUsername(usernametocheck) validate(isvalidinput, validchars) domainValidate(domain, showalert, allowdots) checkForm(formname, checkonly) mod10Check(number) reroute(urlList) formFill(form) getSelectValue(selectname) getSelectText(selectname) hasCardExpired(expirymonth, expiryyear) o---------------------------------------------o | PIPEX Internet New Media Department | | | 1999,2000,2001 | | | Scripts may not be used without permission | | o---------------------------------------------o | ``````````````````````````````````````````````` */ // ************************************************************************ // // ************************************************************************ // function makeTitleCase(frmObj) { // Convert contents of frmObj into Title Case // ARGUMENTS: // frmObj : form input object // EXAMPLE: // // RETURNS: // nothing - works directly on specified // -- PIPEX Internet Ltd, 2000 : Version 1.0 : 2000 var index; var tmpStr; var tmpChar; var preString; var postString; var strlen; tmpStr = frmObj.value.toLowerCase(); strLen = tmpStr.length; if (strLen > 0) { for (index = 0; index < strLen; index++) { if (index == 0) { tmpChar = tmpStr.substring(0,1).toUpperCase(); postString = tmpStr.substring(1,strLen); tmpStr = tmpChar + postString; } else { tmpChar = tmpStr.substring(index, index+1); if (tmpChar == " " && index < (strLen-1)) { tmpChar = tmpStr.substring(index+1, index+2).toUpperCase(); preString = tmpStr.substring(0, index+1); postString = tmpStr.substring(index+2,strLen); tmpStr = preString + tmpChar + postString; } } } } frmObj.value = tmpStr; } // ************************************************************************ // function checkForm(formname, checkonly) { /* check that the required fields are filled in, and validate the email address and username entered. By default it will submit the form if everything is OK. If it is called with checkForm(document.formname, "checkonly") it will not submit the form at the end, just return true. Reads in from a hidden field in the form called REQUIREDFIELDS, and checks each field named in the value of REQUIREDFIELDS is not empty. Also, if there's a value in submit_by, it ensures the email address syntax is correct. -- PIPEX Internet Ltd 2000-2001 :: Version 1.5, updated 06/03/2001 to handle checkboxes and radio buttons :: */ var submitform = true; // by default, submit form if all ok if (checkonly == "checkonly") { var submitform=false; } var itsalright="yes"; // it's alright (i drive the coach!) var errormessage="Before you can submit the form, you need to correct:\n"; if (formname.REQUIREDFIELDS.value=="") { // no required fields were specified alert('No required fields were specified!'); return false; } numberOfRequired=new String("hello"); RequiredFields=new String("hello"); var requiredFieldsvalue=formname.REQUIREDFIELDS.value; var RequiredFields=requiredFieldsvalue.split(','); var numberOfRequired=RequiredFields.length; for (i=0; i < numberOfRequired; i++) { for (q=0; q < formname.length; q++) { var tempobj=formname.elements[q]; if (tempobj.name==RequiredFields[i]) { // need to treat text inputs and selects differently // IF ITS A TEXTBOX - (text or password box) if (tempobj.type=="text" || tempobj.type=="password" || tempobj.type=="hidden") { if (tempobj.value=="") { if (RequiredFields[i]=="submit_by") { errormessage=errormessage+'\n * Email Address must be completed'; } else { errormessage2 = RequiredFields[i].replace(/_/ig, " "); // ^^ replace all underscores with spaces for the error box errormessage=errormessage+'\n * ' + errormessage2 + " must be completed"; } itsalright="no"; // everything is not ok, gone a bit pete tong } } // IF ITS A SELECT LIST - if (tempobj.type=="select-one") { if (tempobj.options[tempobj.selectedIndex].value=="") { errormessage2 = RequiredFields[i].replace(/_/ig, " "); // ^^ replace all underscores with spaces for the error box errormessage=errormessage+'\n * ' + errormessage2 + " must be selected"; itsalright="no"; // everything is not ok, gone a bit pete tong } } // IF ITS A CHECKBOX - if (tempobj.type=="checkbox") { if (!tempobj.checked) { errormessage2 = RequiredFields[i].replace(/_/ig, " "); // ^^ replace all underscores with spaces for the error box errormessage=errormessage+'\n * ' + errormessage2 + " must be checked"; itsalright="no"; // everything is not ok, gone a bit pete tong } } } } } if (itsalright=="no") { errormessage=errormessage+'\n \n Please complete the above fields then try again'; alert(errormessage); return false; } // check that the email address is valid if (!formname.submit_by.value=="") { var validemail=emailCheck(formname.submit_by.value); if (validemail==false) { alert('The email address you entered does not appear to be valid. \n Please enter a valid address in the format user@domain.com and try again.'); return false; } } if (formname.Username) { if (!formname.Username.value=="") { var validusername=verifyUsername(formname.Username.value); if (validusername==false) { alert('The username you entered does not appear to be a valid PIPEX username. \n Please enter a username in the form abc12 and try again.'); return false; } } } if (submitform) { formname.submit(); return true; } else { return true; } } // ************************************************************************ // /* EMAIL ADDRESS VALIDATION SCRIPT This javascript checks the email address entered is valid according to RFC822. If valid it returns true, if invalid it returns false. PIPEX Internet 2000 */ function emailCheck (emailStr) { /* 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. */ // alert("Email address seems incorrect (check @ and .'s)") return false } var user=matchArray[1] var domain=matchArray[2] // See if "user" is valid if (user.match(userPat)==null) { // 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=domain.match(ipDomainPat) if (IPArray!=null) { // this is an IP address for (var i=1;i<=4;i++) { if (IPArray[i]>255) { // alert("Destination IP address is invalid!") return false } } return true } // Domain is symbolic name var domainArray=domain.match(domainPat) if (domainArray==null) { // 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), 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. // alert("The address must end in a three-letter domain, or two letter country.") return false } // Make sure there's a host name preceding the domain. if (len<2) { var errStr="This address is missing a hostname!" // alert(errStr) return false } // If we've gotten this far, everything's valid! return true; } // ************************************************************************ // function verifyUsername(usernametocheck) { // verify a PIPEX username // checks that the first two characters are letters, // last two are numbers, and the username is between 3 and 10 chars. // -- PIPEX Internet Ltd, 2000 : Version 1.0 var letters = "abcdefghijklmnopqrstuvwxyz"; var numbers = "0123456789"; var username = usernametocheck.toLowerCase(); var ok = "yes"; var temp = "a"; // character being examined temp = username.substring(0,1); if (letters.indexOf(temp) == "-1") ok = "no"; temp = username.substring(1,2); if (letters.indexOf(temp) == "-1") ok = "no"; temp = username.substring(username.length-2, username.length-1); if (numbers.indexOf(temp) == "-1") ok = "no"; temp = username.substring(username.length-1, username.length); if (numbers.indexOf(temp) == "-1") ok = "no"; if (username.length < 3 || username.length > 10) { ok = "no"; } if (ok=="no") { return false; } if (ok=="yes") { return true; } } // ************************************************************************ // /* FORM ENTRY VALIDATOR Checks that a form entry doesn't contain illegal characters Returns FALSE if there are illegal characters, TRUE if the entry is valid Possible values for validchars argument are: [blank] = Allow letters, numbers and punctuation letters = Allow only letters (upper or lower case) numbers = Allow only numbers set2 = Allow only letters and numbers [anything else] = specify your own valid characters PIPEX Internet Ltd 2000 */ function validate(isvalidinput,validchars) { var validset1 = "abcdefghijklmnopqrstuvwxyz0123456789" var validset2 = "abcdefghijklmnopqrstuvwxyz0123456789 .," var validletters = "abcdefghijklmnopqrstuvwxyz" var validnumbers = "0123456789" var validnumbersandspaces = "0123456789 " var validtelnumset = "+01234567890() "; var ok = "yes"; var temp; switch(validchars) { case '': valid = validset2; break; case 'letters': valid = validletters; break; case 'numbers': valid = validnumbers; break; case 'numbersandspaces': valid = validnumbersandspaces; break; case 'set2': valid = validset1; break; case 'telnum': valid = validtelnumset; break; default: valid = validchars; break; } var isvalid=isvalidinput.toLowerCase(); for (var i=0; i= 0; x--) { q = cc.substring(x, x+1); s2 = s2 + q * mr; mr = ((mr==1)? 2 : 1); } for (x=s2.length-1; x >=0; x--) { s3 = s3 + eval(s2.charAt(x)); } isvalid = (s3 % 10); isvalid = ((isvalid==0)? true: false); } return isvalid; } // ************************************************************************ // function formFill(form) { // Automatically complete a form for testing // Fills all inputs of type 'text' with PIPEX Testing // call using formFill(document.formname) for (x=0; x < form.elements.length; x++) { if (form.elements[x].type=="text") { form.elements[x].value="PIPEX Testing"; if (form.elements[x].name=="submit_by") { form.elements[x].value="pipextesting@pipex.net"; } if (form.elements[x].name=="Card_Number") { form.elements[x].value="pipextest"; } } } } // ************************************************************************ // function reroute(urlList) { url=urlList.options[urlList.selectedIndex].value if(url.length) parent.location.href=url; } // ************************************************************************ // function getSelectValue(selectname) /* Return value of selected item in specified list box Call as : getSelectValue(document.form.selectname) :: PIPEX Internet Ltd :: DP :: 2001 :: */ { if ((selectname.options[selectname.selectedIndex].value) != "") { return selectname.options[selectname.selectedIndex].value; } else { return false; } } function getSelectText(selectname) /* Return text of selected item in specified list box Call as : getSelectText(document.form.selectname) :: PIPEX Internet Ltd :: DP :: 2001 :: */ { if (selectname.options[selectname.selectedIndex].text != "") { return selectname.options[selectname.selectedIndex].text; } else { return false; } } // ************************************************************************ // /* Date Manipulation and Comparision Functions */ function hasCardExpired(expirymonth, expiryyear) { /* Check if card has expired ARGUMENTS: expirymonth - the month the card expires, 01 = jan .... 12 = dec expiryyear - the year the card expires, e.g. 2002 RETURNS: TRUE if card has expired FALSE if card is still valid */ cardexpiry = new Date(); cardexpiry.setDate(31); // set expiry to last day of month cardexpiry.setMonth(expirymonth - 1); // JS starts with 00 = jan so take 1 away cardexpiry.setYear(expiryyear); // set year of card expiry // we'll use an SSI to get the date from the server in case the client's // clock isn't set correctly datenow = new Date(""); if (datenow < cardexpiry) { return false; } else { return true; } } // ************************************************************************ // /* Code and functions in this file written by PIPEX Internet Ltd New Media Department, 1999 - 2001 No part of this code may be copied, modified or reused without prior written permission from PIPEX Internet Ltd. All rights reserved. End of Forms-Script.js file DP. */