Commit 294afbc217a45c6aa8e3016dae89ff3da631a36a

Added functionality for form action being a function
Added hooks function(not tested yet)
README
(44 / 0)
  
1This is a prototype/scriptaculous based validator.
2This validator is small, and highly configurable. You dont need a detailed
3 knowledge of javascript to start using it.
4
5Your first point of call is tests/index.html. To start using validator
6 * Make sure your form and form elements all have id
7 * Make sure your form has an action
8 * Make sure you have a submit button
9
10 * You can specify a function as your form action, see the second form
11 in tests/index.html
12
13 * All the fields you want to validate will have a title attribute. This
14 attribute will specify what kind of validation you want. You can also
15 have a list of space, separated titles if you want more than one type
16 of validation to be carried out. tests/index.html contain these scenarios.
17
18
19The file you will need to look at next is validator_rules.js. This is required
20 only if you need to tweak validator in any way. If you are confortable with
21 regular expressions, you can loook at var regex.
22
23All changes you might ever want to make, should be done to this file.
24
25The keen eyed will see that some messages dont have the corresponding
26 regex definition. These ones will have their regex generated at run time.
27
28If you also wish to generate some regex and/or messages at run time, use the
29 function hooks(). An example is provided for you. Note that hooks should
30 always return a true whenever a hook is defined, else just return false.
31 The variable v is the value of the element's title(if you provide multiple
32 titles, hooks will be called on that element for each title) and e is the
33 element being called. An example is provided in validator.js.
34
35Please do not redefine the regex already defined without deleting them.
36 This might cause undesirable effects.
37
38Please do not name another method as validator_hooks or name a method starting
39 with validator_, this might conflict with some methods in the plugin.
40
41
42
43NOTE
44For equals#id, {n} will be replaced by document.getElementById(id).name
  
1<?xml version="1.0" encoding="UTF-8"?>
2<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
3 <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
4</project-private>
  
99 <script src="../validator_rules.js" language="javascript"></script>
1010 <script src="../scripty/src/scripty.js" language="javascript"></script>
1111 <script src="../scripty/src/effects.js" language="javascript"></script>
12 <script language="javascript">
13 function submit_form() {
14 alert("Funky Form Action");
15 }
16 </script>
1217 <link rel="stylesheet" href="../css/validator.css" />
1318 <body>
1419 <form name="test" action="test.php" method="POST" id="test">
2323 User CustomF1: <INPUT type="TEXT" name="ct1" id="ct1" title="length{3:6}" /><br />
2424 User CustomF2: <INPUT type="TEXT" name="ct1" id="ct2" title="equals:don" /><br />
2525 User CustomF3: <INPUT type="PASSWORD" name="ct1" id="ct3" title="equals:#pass" /><br />
26 <INPUT type="SUBMIT" value="Submit" />
27 </form>
28 <form name="test2" action="function:submit_form()" method="POST" id="test2">
29 User Column: <input type="text" name="uc" id="uc" title="required"/><br />
30 User Row: <input type="text" name="ur" id="ur" title="required"/><br />
2631 <INPUT type="SUBMIT" value="Submit" />
2732 </form>
2833 </body>
validator.js
(32 / 16)
  
22var thisForm = "";
33var elementEvent = "";
44var error_elements = [];
5var error = false;
56
67var obj = {
7 validate_fm: function(event) {
8 validate_fm: function(event) {
9 error = false;
810 form = $(event.target.id);
911 elements = $A(form.elements);
1012 formEvent = event;
11 elements.each(validate_element);
13 elements.each(validator_validate_element);
14 if(form.action.search(/function:\w/) == 0) {
15 if(!error) {
16 Event.stop(formEvent);
17 funktion = form.action.substr(form.action.indexOf(":") + 1, form.action.length);
18 eval(funktion);
19 }
20 } else {
21 return true;
22 }
1223 },
1324
1425 validate_et: function(event) {
1526 element = $(event.target.id);
1627 elementEvent = event;
17 validate_element(element);
28 validator_validate_element(element);
1829 }
1930};
2031
3737 for(i=0; i < forms.length; i++) {
3838 thisForm = forms[i];
3939 Event.observe(forms[i].id, "submit", obj.validate_form)
40 }
40 }
4141});
4242
43function do_min_max_length(v) {
43function validator_do_min_max_length(v) {
4444 pos = v.indexOf(":", 0);
4545 front = v.substr(0, pos);
4646 back = v.substr(pos, v.length);
5656 regex[v] = new RegExp("^.{" + front + "," + back + "}$");
5757}
5858
59function do_min_length(v) {
59function validator_do_min_length(v) {
6060 len = v.replace(/[^\d*]/g, "");
6161 pre_def = "length{x}";
6262 if(messages[pre_def] == undefined) {
6767 regex[v] = new RegExp("^.{" + len + "}$");
6868}
6969
70function do_equals_word(v) {
70function validator_do_equals_word(v) {
7171 word = v.substr(v.indexOf(":") + 1, v.length);
7272 pre_def = "equals:x";
7373 if(messages[pre_def] == undefined) {
7878 regex[v] = new RegExp("^" + word + "$");
7979}
8080
81function do_equals_id(v) {
81function validator_do_equals_id(v) {
8282 id = v.substr(v.indexOf("#") + 1, v.length);
8383 pre_def = "equals:#id";
8484 if(messages[pre_def] == undefined) {
8686 } else {
8787 messages[v] = messages[pre_def].replace(/\{n\}/,$(id).name);
8888 }
89 regex[v] = new RegExp("^" + $(id).value + "$");
8990}
9091
91function validate_regex(validator, element) {
92 if(validator.search(/length\{\d*\}/) == 0) {
93 do_min_length(validator);
92function validator_validate_regex(validator, element) {
93 if(validator_hooks(validator, element)) {
94
9495 }
96 else if(validator.search(/length\{\d*\}/) == 0) {
97 validator_do_min_length(validator);
98 }
9599 else if(validator.search(/length\{\d*:\d*\}/) == 0) {
96 do_min_max_length(validator);
100 validator_do_min_max_length(validator);
97101 }
98102 else if(validator.search(/equals:\#\w/) == 0) {
99 do_equals_id(validator);
103 validator_do_equals_id(validator);
100104 }
101105 else if(validator.search(/equals:\W*/) == 0) {
102 do_equals_word(validator);
106 validator_do_equals_word(validator);
103107 }
104108
105109 if(element.value.search(regex[validator]) == 0) {
113113 }
114114}
115115
116function validate_element(element) {
116function validator_validate_element(element) {
117117 validate_class_array = element.title.split(" ");
118118 error_elements = [];
119119
120120 validate_class_array.each(function(one_validate_class) {
121121 if(one_validate_class.search(/^\s*$/) == -1) {
122 if(validate_regex(one_validate_class, element)) {
122 if(validator_validate_regex(one_validate_class, element)) {
123123 span_id = "error-span" + element.id;
124124 if($(span_id) != null)
125125 $(span_id).remove();
144144 error_elements[element.id] = true;
145145 Effect.Appear(span_id);
146146 Event.observe(element, "change", obj.validate_elem);
147 error = true;
147148 Event.stop(formEvent);
148149 }
149150 }
  
2626 "yyyymmdd": /(19|20)\d\d([-. ]|\/)(0?[1-9]|1[0-2])([-. ]|\/)(0?[1-9]|[12][0-9]|3[01])/
2727}
2828
29/*
30 * For equals, {n} will be replaced by document.getElementById(id).name
31 */
29function validator_hooks(v, e) {
30// if(v.search(/equals:\W*/)== 0) {
31// word = v.substr(v.indexOf(":") + 1, v.length);
32// pre_def = "equals:x";
33// if(messages[pre_def] == undefined) {
34// messages[v] = "This field must be equal to " + word;
35// } else {
36// messages[v] = messages[pre_def].replace(/\{x\}/,word);
37// }
38// regex[v] = new RegExp("^" + word + "$");
39// }
40// return true;
41return false;
42}