PageSourceSearch

https://www.powerp.co.uk/scripts/uni_form2.js?v=06102026183941

js powerp.co.uk collected 2026-10-06 17:39:54 UTC 10,857 bytes, 281 lines download raw bytes

1// WebWiz Universal Form Validation Script - Version 2.0.009
2//
3// ©Copyright 2005-2014 WidaGroup Ltd - All Rights Reserved
4// www.widagroup.com
5//
6//
7// CUT BEGIN // CUT BEGIN // CUT BEGIN // CUT BEGIN // CUT BEGIN
8//
9//
10// Developed intermittently between 30/05/2005 and 02/06/2014 by Matthew Ellis for Wida Group Ltd
11//
12//
13// Features
14//
15// Deals with multiple check boxes, select boxes, and regular text fields/text areas.
16// Colours and sets focus to fields that fail validation as well as alert box notification.
17// Can define minimum and maximum length of data required as well as the type of data (i.e. Email address, post code).
18// Deals with relationships between fields (i.e. Password retypes and groups of fields where one pass is enough).
19// Highlight colour and message configuration.
20// 
21//
22// Future Features... maybe...
23//
24// Error tolerance... because there is BARELY any now (i.e. It will ignore (without error) fields it can't find).
25// Multiple forms per page functionality.
26// Possibly advanced data type checks. Like for real dates and credit card numbers.
27// Relationship where the value of one field alters the validation of another (i.e. check/radio box with text field).
28//
29//
30// Q. How do I use this fabulous script?
31//
32//	A. 1. Include this file in the page
33//
34//		2. put the following in a JavaScript block in the page:
35//			setvalidation(['fieldname1','fieldname2'],['validation,minlen,maxlen,rule','validation,minlen,maxlen,rule'],['Field 1','Field 2']);
36//
37//		3. put the following in your <form> tag:
38//			onsubmit="return checkform(formname);"
39//
40//
41// Q. But what does it all mean Holmes?
42//
43// A. 'validation' is what the field must contain if it contains anything at all. Such as an email address or post code.
44//    Basically anything with a rigid recognisable structure.
45//
46//    'minlen' and 'maxlen' are the minimum and maximum lengths of the value that are allowed. Putting a minlen of 1 on an
47//    email field is enough to force a proper email address while having a minlen of 0 would make the email optional.
48//
49//    'rule' is for everything else, often when fields interact. For example you could ensure fields are repeated (think
50//    password retypes) or where you need only fill in one from a group of fields.
51//
52//
53// Q. Your Validation rules suck. How do I add my own?
54//
55// A. Just run this function on the page where you want to use your custom Regular Expression:
56//
57//    settype('name of new validation',/regular expression/);
58//
59//
60// Q. Config? Is that like ripping off fruit?
61//
62// A. You can alter some of the ways which the script provides feed back to the surfer as follows:
63//
64//    setconfig('pass colour','#ffdddd');
65//
66//    That would make fields that pass validation appear as a nice pink colour... if that's the kind of thing you like.
67//
68//    Here is a list of all the things you can change:
69//
70//    pass colour          colour of passed fields
71//    fail colour          colour of failed fields
72//    oneofmany colour     colour of 'oneofmany' fields where none have passed
73//    failed message       message in the popup if any fields fail
74//    oneofmany message    message to clarify 'oneofmany' fields
75//    resubmit message     message to notify the user that the form has already been submitted
76//
77//
78// CUT END // CUT END // CUT END // CUT END // CUT END //
79
80var arrFieldName=new Array();
81var arrFieldType=new Array();
82var arrFieldDescription=new Array();
83var blnFormSubmitted=false;
84var reCheck=new Object();
85var objConfig=new Object();
86
87// assign default validation types
88settype('number',/^[\d\s\-\,\.]*$/);
89settype('email',/^[a-zA-Z\d][^\(\)\<\>\@\,\;\:\\\"\[\]]*\@[a-zA-Z\d][a-zA-Z\d\-\.]*\.[a-zA-Z]{2,6}$/); //"
90settype('postcode',/^[a-zA-Z]{1,2}\d{1,2}[a-zA-Z]?\s\d[a-zA-Z]{2}$/);
91settype('ipaddress',/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/);
92settype('date',/^[0123]?\d[\\\/\-\.][01]?\d[\\\/\-\.]\d{2,4}$/);
93settype('url',/^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$/); //'
94settype('name',/^[a-zA-Z][a-zA-Z\d\-\s\'\.]*[a-zA-Z]$/); //'
95settype('telephone',/^[\d\s\(\)\-\,\.\+]*$/);
96settype('sttelephone',/^\d[\d]*$/);
97settype('stpostcode',/^[a-zA-Z]\d\s\d[a-zA-Z][a-zA-Z]$|^[a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z][a-zA-Z]$|^[a-zA-Z]\d\d\s\d[a-zA-Z][a-zA-Z]$|^[a-zA-Z][a-zA-Z]\d\s\d[a-zA-Z][a-zA-Z]$|^[a-zA-Z][a-zA-Z]\d\d\s\d[a-zA-Z][a-zA-Z]$|^[a-zA-Z][a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z][a-zA-Z]$|^[a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z]\d$|^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$|^[a-zA-Z]\d\d[a-zA-Z][a-zA-Z]$|^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z][a-zA-Z]$|^[a-zA-Z]\d\d\d[a-zA-Z][a-zA-Z]$|^[a-zA-Z][a-zA-Z]\d\d[a-zA-Z][a-zA-Z]$|^[a-zA-Z][a-zA-Z]\d\d\d[a-zA-Z][a-zA-Z]$|^[a-zA-Z][a-zA-Z]\d[a-zA-Z]\d[a-zA-Z][a-zA-Z]$|^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$|^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$|^\d{4,10}$/);
98
99
100// assign default config
101setconfig('pass colour',''); // Leave me in - or you'll be soooorrieeeee...
102setconfig('oneofmany colour','#eef5ff');
103setconfig('fail colour','#ddeeff');
104setconfig('oneofmany message','You need only fill in one of these details.');
105setconfig('failed message','Please check the following details:');
106setconfig('resubmit message','This form has already been submitted. Please reload the page and try again.');
107
108// assign default validation
109setvalidation([''],[''],['']);
110
111function setvalidation(fieldnames,checktypes,fielddescription){arrFieldName=fieldnames;arrFieldType=checktypes;arrFieldDescription=fielddescription;}
112function addvalidation(fieldnames,checktypes,fielddescription){arrFieldName.push(fieldnames);arrFieldType.push(checktypes);arrFieldDescription.push(fielddescription);}
113function remvalidation(fieldnames){
114	for(var n=0;n<arrFieldName.length;n++){
115		if(arrFieldName[n]==fieldnames){
116			arrFieldName.remove(n);
117			arrFieldType.remove(n);
118			arrFieldDescription.remove(n);
119			n--;
120		}
121	}
122}
123function setconfig(strConfigName,strNewValue){objConfig[strConfigName.toLowerCase()]=strNewValue;}
124function settype(strTypeName,strNewValue){reCheck[strTypeName]=strNewValue;}
125
126function checkform(theForm){
127	var arrFieldValues=new Array();
128	var arrCheckTypeDatas=new Array();
129	
130	if(blnFormSubmitted){
131		alert(objConfig['resubmit message'])
132		return false;
133	}else{
134		var strMessage='';
135		var blnOneOfManyPresent=false;
136		
137		for(n=0;n<arrFieldName.length;n++){
138			var objField=theForm[arrFieldName[n]];
139			
140			if(typeof(objField)=='undefined'){
141				arrFieldName.splice(n,1);
142				arrFieldType.splice(n,1);
143				arrFieldDescription.splice(n,1);
144				n--;
145			}else{
146				if(objField.disabled){
147					arrFieldName.splice(n,1);
148					arrFieldType.splice(n,1);
149					arrFieldDescription.splice(n,1);
150					n--;
151				}
152			}
153		}
154		
155		for(n=0;n<arrFieldName.length;n++){
156			var objField=theForm[arrFieldName[n]];
157			
158			var blnFieldCollectionLength=objField.length?objField.length:0;
159			
160			arrFieldValues[n]='';
161			arrCheckTypeDatas[n]=arrFieldType[n].split(',')
162
163			if(blnFieldCollectionLength==0){
164				if(objField.type=='checkbox'){
165					arrFieldValues[n]=objField.checked?objField.value:'';
166				}else{
167					arrFieldValues[n]=objField.value;
168				}
169			}else{
170				if(objField.options){
171					if(objField.selectedIndex==-1){
172						arrFieldValues[n]='';
173					}else{
174						arrFieldValues[n]=objField.options[objField.selectedIndex].value;
175					}
176				}else{
177					for(var i=0;i<blnFieldCollectionLength;i++){
178						if(objField[i].type=='checkbox'){
179							if(objField[i].checked){
180								arrFieldValues[n]+=objField[i].value+',';
181							}
182						}else{
183							arrFieldValues[n]+=objField[i].value;
184						}
185					}
186					arrFieldValues[n]=arrFieldValues[n].substr(0,arrFieldValues[n].length-1);
187				}
188			}
189		}
190		
191		for(n=0;n<arrFieldName.length;n++){
192			var objField=theForm[arrFieldName[n]];
193			
194			var intCheckResult=check_all(arrFieldValues,n,arrCheckTypeDatas);
195			var blnFieldCollectionLength=objField.length?objField.length:0;
196			
197			switch(intCheckResult){
198			case 0: // failed
199				if(strMessage=='' && focus in objField){objField.focus();}
200				
201				strMessage+=' - '+arrFieldDescription[n]+'\n';
202				
203				setfieldcolour(objField,blnFieldCollectionLength,objConfig['fail colour']);
204			break;
205			case 1: // failed and is oneofmany where none passed
206				if(strMessage=='' && focus in objField){objField.focus();}
207
208				strMessage+=' - '+arrFieldDescription[n]+' *\n';
209				blnOneOfManyPresent=true;
210				
211				setfieldcolour(objField,blnFieldCollectionLength,objConfig['oneofmany colour']);
212			break;
213			case 2: // passed or is one of many where one or more passed
214				setfieldcolour(objField,blnFieldCollectionLength,objConfig['pass colour']);
215			break;
216			}
217		}
218		
219		blnFormSubmitted=(strMessage=='');
220		
221		if(blnOneOfManyPresent){strMessage+='\n* '+objConfig['oneofmany message']}
222		
223		if(!blnFormSubmitted){alert(objConfig['failed message']+'\n\n'+strMessage);}
224		
225		return blnFormSubmitted;
226	}
227}
228
229function setfieldcolour(strFieldName,blnFieldCollectionLength,strNewColour){
230	if(blnFieldCollectionLength==0){
231		strFieldName.style.backgroundColor=strNewColour;
232	}else{
233		if(strFieldName.options){
234			strFieldName.style.backgroundColor=strNewColour;
235			for(var i=0;i<blnFieldCollectionLength;i++){
236				strFieldName.options[i].style.backgroundColor=strNewColour;
237			}
238		}else{
239			for(var i=0;i<blnFieldCollectionLength;i++){
240				strFieldName[i].style.backgroundColor=strNewColour;
241			}
242		}
243	}
244}
245
246function check_all(arrFieldValues,n,arrCheckTypeDatas){
247	var strTemp='';
248	
249	if(checkstringtype(arrFieldValues[n],arrCheckTypeDatas[n][0]) && checkstringlength(arrFieldValues[n],arrCheckTypeDatas[n][1],arrCheckTypeDatas[n][2])){
250		if(arrCheckTypeDatas[n][3]=='repeat'){
251			if (arrFieldValues[n]!=arrFieldValues[n-1]){
252				return 0;
253			}
254		}
255		return 2;
256	}else{
257		if(arrCheckTypeDatas[n][3]=='oneofmany'){
258			for(var m=0;m<arrFieldName.length;m++){
259				if(arrCheckTypeDatas[m][3]==arrCheckTypeDatas[n][3]){
260					if(checkstringtype(arrFieldValues[m],arrCheckTypeDatas[m][0]) && checkstringlength(arrFieldValues[m],arrCheckTypeDatas[m][1],arrCheckTypeDatas[m][2])){
261						return 2;
262					}
263				}
264			}
265			return 1;
266		}
267		return 0;
268	}
269}
270
271function checkstringtype(strString,strCheckType){return (strCheckType=='' || strString=='' || reCheck[strCheckType].test(strString));}
272function checkstringlength(strString,intMinimum,intMaximum){return (!intMinimum || strString.length>=intMinimum) && (!intMaximum || strString.length<=intMaximum);}
273
274Array.prototype.remove=function(start,finish){
275	var rest=this.slice((finish||start)+1||this.length);
276	this.length=start<0?this.length+start:start;
277	
278	return this.push.apply(this,rest);
279};
280
281//End of Universal Form Validation Script

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.