Commit 294afbc217a45c6aa8e3016dae89ff3da631a36a
- Diff rendering mode:
- inline
- side by side
README
(44 / 0)
|   | |||
| 1 | This is a prototype/scriptaculous based validator. | ||
| 2 | This validator is small, and highly configurable. You dont need a detailed | ||
| 3 | knowledge of javascript to start using it. | ||
| 4 | |||
| 5 | Your 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 | |||
| 19 | The 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 | |||
| 23 | All changes you might ever want to make, should be done to this file. | ||
| 24 | |||
| 25 | The 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 | |||
| 28 | If 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 | |||
| 35 | Please do not redefine the regex already defined without deleting them. | ||
| 36 | This might cause undesirable effects. | ||
| 37 | |||
| 38 | Please 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 | |||
| 43 | NOTE | ||
| 44 | For 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> |
tests/index.html
(10 / 0)
|   | |||
| 9 | 9 | <script src="../validator_rules.js" language="javascript"></script> | |
| 10 | 10 | <script src="../scripty/src/scripty.js" language="javascript"></script> | |
| 11 | 11 | <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> | ||
| 12 | 17 | <link rel="stylesheet" href="../css/validator.css" /> | |
| 13 | 18 | <body> | |
| 14 | 19 | <form name="test" action="test.php" method="POST" id="test"> | |
| … | … | ||
| 23 | 23 | User CustomF1: <INPUT type="TEXT" name="ct1" id="ct1" title="length{3:6}" /><br /> | |
| 24 | 24 | User CustomF2: <INPUT type="TEXT" name="ct1" id="ct2" title="equals:don" /><br /> | |
| 25 | 25 | 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 /> | ||
| 26 | 31 | <INPUT type="SUBMIT" value="Submit" /> | |
| 27 | 32 | </form> | |
| 28 | 33 | </body> |
validator.js
(32 / 16)
|   | |||
| 2 | 2 | var thisForm = ""; | |
| 3 | 3 | var elementEvent = ""; | |
| 4 | 4 | var error_elements = []; | |
| 5 | var error = false; | ||
| 5 | 6 | ||
| 6 | 7 | var obj = { | |
| 7 | validate_fm: function(event) { | ||
| 8 | validate_fm: function(event) { | ||
| 9 | error = false; | ||
| 8 | 10 | form = $(event.target.id); | |
| 9 | 11 | elements = $A(form.elements); | |
| 10 | 12 | 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 | } | ||
| 12 | 23 | }, | |
| 13 | 24 | ||
| 14 | 25 | validate_et: function(event) { | |
| 15 | 26 | element = $(event.target.id); | |
| 16 | 27 | elementEvent = event; | |
| 17 | validate_element(element); | ||
| 28 | validator_validate_element(element); | ||
| 18 | 29 | } | |
| 19 | 30 | }; | |
| 20 | 31 | ||
| … | … | ||
| 37 | 37 | for(i=0; i < forms.length; i++) { | |
| 38 | 38 | thisForm = forms[i]; | |
| 39 | 39 | Event.observe(forms[i].id, "submit", obj.validate_form) | |
| 40 | } | ||
| 40 | } | ||
| 41 | 41 | }); | |
| 42 | 42 | ||
| 43 | function do_min_max_length(v) { | ||
| 43 | function validator_do_min_max_length(v) { | ||
| 44 | 44 | pos = v.indexOf(":", 0); | |
| 45 | 45 | front = v.substr(0, pos); | |
| 46 | 46 | back = v.substr(pos, v.length); | |
| … | … | ||
| 56 | 56 | regex[v] = new RegExp("^.{" + front + "," + back + "}$"); | |
| 57 | 57 | } | |
| 58 | 58 | ||
| 59 | function do_min_length(v) { | ||
| 59 | function validator_do_min_length(v) { | ||
| 60 | 60 | len = v.replace(/[^\d*]/g, ""); | |
| 61 | 61 | pre_def = "length{x}"; | |
| 62 | 62 | if(messages[pre_def] == undefined) { | |
| … | … | ||
| 67 | 67 | regex[v] = new RegExp("^.{" + len + "}$"); | |
| 68 | 68 | } | |
| 69 | 69 | ||
| 70 | function do_equals_word(v) { | ||
| 70 | function validator_do_equals_word(v) { | ||
| 71 | 71 | word = v.substr(v.indexOf(":") + 1, v.length); | |
| 72 | 72 | pre_def = "equals:x"; | |
| 73 | 73 | if(messages[pre_def] == undefined) { | |
| … | … | ||
| 78 | 78 | regex[v] = new RegExp("^" + word + "$"); | |
| 79 | 79 | } | |
| 80 | 80 | ||
| 81 | function do_equals_id(v) { | ||
| 81 | function validator_do_equals_id(v) { | ||
| 82 | 82 | id = v.substr(v.indexOf("#") + 1, v.length); | |
| 83 | 83 | pre_def = "equals:#id"; | |
| 84 | 84 | if(messages[pre_def] == undefined) { | |
| … | … | ||
| 86 | 86 | } else { | |
| 87 | 87 | messages[v] = messages[pre_def].replace(/\{n\}/,$(id).name); | |
| 88 | 88 | } | |
| 89 | regex[v] = new RegExp("^" + $(id).value + "$"); | ||
| 89 | 90 | } | |
| 90 | 91 | ||
| 91 | function validate_regex(validator, element) { | ||
| 92 | if(validator.search(/length\{\d*\}/) == 0) { | ||
| 93 | do_min_length(validator); | ||
| 92 | function validator_validate_regex(validator, element) { | ||
| 93 | if(validator_hooks(validator, element)) { | ||
| 94 | |||
| 94 | 95 | } | |
| 96 | else if(validator.search(/length\{\d*\}/) == 0) { | ||
| 97 | validator_do_min_length(validator); | ||
| 98 | } | ||
| 95 | 99 | else if(validator.search(/length\{\d*:\d*\}/) == 0) { | |
| 96 | do_min_max_length(validator); | ||
| 100 | validator_do_min_max_length(validator); | ||
| 97 | 101 | } | |
| 98 | 102 | else if(validator.search(/equals:\#\w/) == 0) { | |
| 99 | do_equals_id(validator); | ||
| 103 | validator_do_equals_id(validator); | ||
| 100 | 104 | } | |
| 101 | 105 | else if(validator.search(/equals:\W*/) == 0) { | |
| 102 | do_equals_word(validator); | ||
| 106 | validator_do_equals_word(validator); | ||
| 103 | 107 | } | |
| 104 | 108 | ||
| 105 | 109 | if(element.value.search(regex[validator]) == 0) { | |
| … | … | ||
| 113 | 113 | } | |
| 114 | 114 | } | |
| 115 | 115 | ||
| 116 | function validate_element(element) { | ||
| 116 | function validator_validate_element(element) { | ||
| 117 | 117 | validate_class_array = element.title.split(" "); | |
| 118 | 118 | error_elements = []; | |
| 119 | 119 | ||
| 120 | 120 | validate_class_array.each(function(one_validate_class) { | |
| 121 | 121 | if(one_validate_class.search(/^\s*$/) == -1) { | |
| 122 | if(validate_regex(one_validate_class, element)) { | ||
| 122 | if(validator_validate_regex(one_validate_class, element)) { | ||
| 123 | 123 | span_id = "error-span" + element.id; | |
| 124 | 124 | if($(span_id) != null) | |
| 125 | 125 | $(span_id).remove(); | |
| … | … | ||
| 144 | 144 | error_elements[element.id] = true; | |
| 145 | 145 | Effect.Appear(span_id); | |
| 146 | 146 | Event.observe(element, "change", obj.validate_elem); | |
| 147 | error = true; | ||
| 147 | 148 | Event.stop(formEvent); | |
| 148 | 149 | } | |
| 149 | 150 | } |
validator_rules.js
(14 / 3)
|   | |||
| 26 | 26 | "yyyymmdd": /(19|20)\d\d([-. ]|\/)(0?[1-9]|1[0-2])([-. ]|\/)(0?[1-9]|[12][0-9]|3[01])/ | |
| 27 | 27 | } | |
| 28 | 28 | ||
| 29 | /* | ||
| 30 | * For equals, {n} will be replaced by document.getElementById(id).name | ||
| 31 | */ | ||
| 29 | function 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; | ||
| 41 | return false; | ||
| 42 | } |

