ryxeo-glpi-git / htdocs / epack / validate.js @ b4660b5a
Historique | Voir | Annoter | Télécharger (3,87 ko)
1 |
//By: Chris Campbell
|
---|---|
2 |
//Created: May 20, 2005
|
3 |
//Last Modified: June 27th, 2005
|
4 |
//www.particletree.com
|
5 |
|
6 |
|
7 |
window.onload = attachFormHandlers; |
8 |
|
9 |
var gShow; //variable holding the id where feedback will be sent to. |
10 |
var sUrl = "/epack/formvalidation.php?validationtype=ajax&val=";//url is the page which will be processing all of the information. it is important to make sure validationtype is ajax |
11 |
var gErrors = 0; //number of errors is set to none to begin with |
12 |
var http = getHTTPObject();//don't worry about this |
13 |
|
14 |
|
15 |
|
16 |
function attachFormHandlers() |
17 |
{ |
18 |
var form = document.getElementById('form1') |
19 |
|
20 |
if (document.getElementsByTagName)//make sure were on a newer browser |
21 |
{ |
22 |
var objInput = document.getElementsByTagName('input'); |
23 |
for (var iCounter=0; iCounter<objInput.length; iCounter++) |
24 |
objInput[iCounter].onblur = function(){return validateMe(this);} //attach the onchange to each input field |
25 |
} |
26 |
form.onsubmit = function(){return validate();} //attach validate() to the form |
27 |
} |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
/*validateMe is the function called with onblur each time the user leaves the input box
|
33 |
passed into it is the value entered, the rules (which you could create your own), and the id of the area the results will show in*/
|
34 |
function validateMe(objInput) { |
35 |
sVal = objInput.value; //get value inside of input field
|
36 |
sRules = objInput.className.split(' '); // get all the rules from the input box classname |
37 |
sRequired = sRules[1]; // determines if field is required or not |
38 |
sTypeCheck = sRules[2]; //typecheck are additional validation rules (ie. email, phone, date) |
39 |
gShow = sRules[3]; //gShow is the td id where feedback is sent to. |
40 |
|
41 |
//sends the rules and value to the asp page to be validated
|
42 |
http.open("GET", sUrl + (sVal) + "&sRequired=" + (sRequired) + "&sTypeCheck=" + sTypeCheck, true); |
43 |
|
44 |
http.onreadystatechange = handleHttpResponse; // handle what to do with the feedback
|
45 |
http.send(null);
|
46 |
} |
47 |
|
48 |
|
49 |
function handleHttpResponse() { |
50 |
//if the process is completed, decide to do with the returned data
|
51 |
if (http.readyState == 4) |
52 |
{ |
53 |
sResults = http.responseText.split(","); //results is now whatever the feedback from the asp page was |
54 |
//whatever the variable glo_show's (usermsg for example) innerHTML holds, is now whatever was returned by the asp page.
|
55 |
document.getElementById(gShow).innerHTML = "";
|
56 |
document.getElementById(gShow).appendChild(document.createTextNode(sResults[0]));
|
57 |
} |
58 |
} |
59 |
|
60 |
|
61 |
function validate() |
62 |
{ |
63 |
var tables;
|
64 |
|
65 |
tables = document.getElementsByTagName('td')
|
66 |
|
67 |
for (i=0; i<tables.length; i++)//loop through all the <td> elements |
68 |
{ |
69 |
// if the class name of that td element is rules check to see if there are error warnings
|
70 |
if (tables[i].className == "rules") |
71 |
{ |
72 |
//if there is a thank you or its blank then it passes
|
73 |
if (tables[i].innerHTML == 'Merci' || tables[i].innerHTML == '' ) |
74 |
{ |
75 |
tables[i].style.color = '#000000';//the color is changed to black or stays black |
76 |
} |
77 |
else
|
78 |
{ |
79 |
gErrors = gErrors + 1; //the error count increases by 1 |
80 |
tables[i].style.color = '#ff0000';//error messages are changed to red |
81 |
} |
82 |
} |
83 |
} |
84 |
|
85 |
if (gErrors > 0) |
86 |
{ |
87 |
//if there are any errors give a message
|
88 |
alert ("Vérifiez que tous les champs sont correctement remplis, les erreurs sont marquées en rouge.");
|
89 |
gErrors = 0;// reset errors to 0 |
90 |
return false; |
91 |
} |
92 |
else return true; |
93 |
|
94 |
} |
95 |
|
96 |
|
97 |
function getHTTPObject() { |
98 |
var xmlhttp;
|
99 |
/*@cc_on
|
100 |
@if (@_jscript_version >= 5)
|
101 |
try {
|
102 |
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
|
103 |
} catch (e) {
|
104 |
try {
|
105 |
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
|
106 |
} catch (E) {
|
107 |
xmlhttp = false;
|
108 |
}
|
109 |
}
|
110 |
@else
|
111 |
xmlhttp = false;
|
112 |
@end @*/
|
113 |
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { |
114 |
try
|
115 |
{ |
116 |
xmlhttp = new XMLHttpRequest();
|
117 |
} catch (e) {
|
118 |
xmlhttp = false;
|
119 |
} |
120 |
} |
121 |
return xmlhttp;
|
122 |
} |