// from Chapter 6 of Usable Forms for the Web, glasshaus (c) 2002 function validate(form) { var returnVal=true; var formEls=form.elements; var currEl,currName,currType,currVal,currField,minimum,maximum,temp; var errMsg="There were problems with the following field(s):\n"; var firstErr=-1; var notWhitespace=/\S/; var notAlpha=/[^a-z \-\.\,']/gi; // var notAlphaNumeric=/[^a-z0-9]/gi; // doesn't allow spaces or punctuation var notAlphaNumeric=/[^a-z0-9 \-\.\,']/gi; var notAddress=/[^\w \-#\.\,\/]/gi; var hasSpaces=/\s/g; var notInt=/\D/g; var isDecimal=/^\d+(\.\d+)?$/; //var isCC=/^\d{4}(-\d{4}){3}$/; var isCC=/^\d{4}([- ]\d{4}){3}$/; // allow - or space between number groups var isUSZip=/^\d{5}(-\d{4})?$/; var isCaPost=/^[A-Z][\d][A-Z] [\d][A-Z][\d]$/; // ANA NAN var isUKPost=/[a-zA-Z]{1,2}[\d][1-9a-zA-Z]? [\d][a-zA-Z]{2}/; var isUSPhone=/^\d{3}[-\.]\d{3}[-\.]\d{4}$/; var isEmail=/^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-z]{2,6}(\.[a-z]{2})?$/i; var isCurrency=/^\d+(\.\d{2})?$/; var notComment=/[^a-zA-Z0-9\.\,;:%&#$@!\^-_~`"'\[\]\{\}\*\/\?\(\)]/i; var requirements=new Array("required","alphabetic","address","alphanumeric","nospace", "integer","decimal","minlength","maxlength","ccnumber","uszip","ukpost", "usphone","email","currency","percent","comment", "capost"); for(var i=0;imaximum) { if(firstErr<0) firstErr=i; errMsg+="The field \""+currField+"\" must contain at most "+maximum+" characters.\n"; } if(context.ccnumber&&!isCC.test(currValue)) { if(firstErr<0) firstErr=i; errMsg+="The value in the field \""+currField+"\" must be in the form \"XXXX-XXXX-XXXX-XXXX\".\n"; } if(context.uszip&&!isUSZip.test(currValue)) { if(firstErr<0) firstErr=i; errMsg+="The field \""+currField+"\" does not contain a valid 5 or 9 digit ZIP code.\n"; } if(context.ukpost&&!UKPost.test(currValue)) { if(firstErr<0) firstErr=i; errMsg+="The field \""+currField+"\" does not contain a valid UK Postcode.\n"; } if(context.usphone&&!isUSPhone.test(currValue)) { if(firstErr<0) firstErr=i; errMsg+="The field \""+currField+"\" does not contain a valid telephone number.\n"; } if(context.email&&!isEmail.test(currValue)) { if(firstErr<0) firstErr=i; errMsg+="The field \""+currField+"\" does not contain a valid email address.\n"; } if(context.currency&&currValue!="") { currValue=currValue.replace(/[$£¥\,]/g,""); if(!isCurrency.test(CurrValue)) { if(firstErr<0) firstErr=i; errMsg+="The field \""+currField+"\" does not contain a valid amount.\n"; } } else { form.elements[i].value=currValue; } break; case "checkbox": if(context.required) { temp=0; for(var n=0;nmaximum) { if(firstErr<0) firstErr=i-(form[currName].length-1); errMsg+="Please check no more than "+maximum+" of the \""+currField+"\" checkboxes.\n"; } } break; case "radio": if(context.required) { temp=false; for(var n=0;nminimum) { if(firstErr<0) firstErr=i; errMsg+="You must select no more "+maximum+" of the \""+currField+"\" options.\n"; } break; case "submit": case "reset": case "button": case "file": case "image": case "hidden": break; default: alert(" ERROR!!\nWe should never get here!"); break; } } // end of for loop returnVal=firstErr<0; // alert("returnVal is: " + returnVal); if(!returnVal) { alert(errMsg); form.elements[firstErr].focus(); } return returnVal; }