Changeset 1370
- Timestamp:
- 02/18/07 18:46:36 (1 year ago)
- Location:
- branches/joern-dev/fuzz/validation
- Files:
-
- 1 added
- 4 modified
-
formPluginIntegration.html (modified) (3 diffs)
-
js/validate.js (modified) (20 diffs)
-
validateCustomTest.html (modified) (4 diffs)
-
validateTest.html (modified) (1 diff)
-
validatKeypressTest.html (added)
Legend:
- Unmodified
- Added
- Removed
-
branches/joern-dev/fuzz/validation/formPluginIntegration.html
r1288 r1370 21 21 <p> 22 22 <label for="user">Username</label> 23 <input id="user" name="user" title="Please enter your username (at least 3 characters)" class="{ minLength:3}" />23 <input id="user" name="user" title="Please enter your username (at least 3 characters)" class="{required:true,minLength:3}" /> 24 24 </p> 25 25 <p> 26 26 <label for="pass">Password</label> 27 <input type="password" name="password" id="password" class="{ minLength:5}" />27 <input type="password" name="password" id="password" class="{required:true,minLength:5}" /> 28 28 </p> 29 29 <p> … … 48 48 }); 49 49 50 // validate the form when it is submitted51 var options = {52 debug: true,50 $.validator.defaults.debug = true; 51 52 $("#form").validate({ 53 53 submitHandler: function(form) { 54 54 $(form).ajaxSubmit({ … … 61 61 } 62 62 }); 63 } 64 };65 var v = $("#form").validate(options);63 }, 64 event: "keyup" 65 }); 66 66 }); 67 67 </script> -
branches/joern-dev/fuzz/validation/js/validate.js
r1363 r1370 10 10 11 11 /** 12 * Validates either a single form on submit or a list of elements immediately.12 * Validates either a single form on submit or a list of elements on a user-defined event. 13 13 * 14 14 * The normal behaviour is to validate a form when a submit button is clicked or 15 15 * the user presses enter when an input of that form is focused. 16 16 * 17 * It is also possible to validate e lements immediately on blur or any other event.17 * It is also possible to validate each individual element of that form, eg. on blur or keyup. 18 18 * 19 19 * Following are a few aspects that you should know about when using this plugin. … … 22 22 * Markup recommendations: A good form has labels associated with each input 23 23 * element: The for attribute of the label has the same value as the id of the input. 24 * 25 * Validation rules: You can specify validation rules via metadata or via plugin26 * settings (option rules). Its more a matter of taste which way you choose.27 * Using metadata is good for fast prototyping.28 * 29 * Validation methods:Included are a set of common validation methods, like required.24 * If IDs aren't provided, they are generated, combining the ID of the containing 25 * form (if any) with the name of the element, with the exception of radio and checkbox 26 * inputs. 27 * 28 * Validation methods: A valiation method decides whether an element is valid. 29 * Included are a set of common validation methods, like required. 30 30 * Except required itself and equalTo, all validation methods declare an element valid 31 31 * when it has no value at all. That way you can specify an element input to 32 32 * contain a valid email adress, or nothing at all. All available methods are documented 33 33 * below, as well as $.validator.addMethod, which you can use to add your own methods. 34 * 35 * Validation rules: A validation rule applies one or more validation methods to an 36 * input element. You can specify validation rules via metadata or via plugin 37 * settings (option rules). It is more a matter of taste which way you choose. 38 * Using metadata is good for fast prototyping. Plugin settings are good for perfect 39 * clean markup. Valid markup results from both approaches. 34 40 * 35 * Error messages: There are three ways to provide error messages. Via the title attribute 36 * of the input element to validate, via error labels and via plugin settings 37 * (option messages). All included validation rules provide a default error message 38 * which you can use for prototyping. 41 * Error messages: An error message displays a hint for the user about invalid 42 * elements, and what is wrong. There are three ways to provide error messages. 43 * Via the title attribute of the input element to validate, via error labels and 44 * via plugin settings (option messages). All included validation rules provide a 45 * default error message which you can use for prototyping, because it is used when 46 * no specific message is provided. 39 47 * 40 48 * Error message display: Error messages are handled via label elements with an … … 43 51 * after the invalid element. It is also possible to put them into an error container 44 52 * (option errorContainer), even a container containing a general warning followed by 45 * thelist of errors is possible (option errorContainer together with errorLabelContainer).53 * a list of errors is possible (option errorContainer together with errorLabelContainer). 46 54 * 47 55 * Focusing of invalid elements: By default, the first invalid element in a form is focused … … 54 62 * Form submit: By default, the form submission is prevented when the form is invalid, 55 63 * and submitted as normal when it is valid. You can also handle the submission 56 * (option submitHandler) manually. 64 * manually (option submitHandler). 65 * 66 * Validation event: By default, forms are validated on submit, triggered by the user clicking 67 * the submit button or pressing enter when a form input is focused. Instead, each element can 68 * be validated on a certain event like blur or keyup (option event). 57 69 * 58 70 * Developing and debugging a form: While developing and debugging the form, you should set … … 62 74 * your rules all accept empty elements as valid (like email or url methods). 63 75 * 64 * Validati onmultiple forms on one page: The plugin can handle only one form per call. In case76 * Validating multiple forms on one page: The plugin can handle only one form per call. In case 65 77 * you have multiple forms on a single page which you want to validate, you can avoid having 66 * to duplicate the plugin settings by modifying the defaults via $.validator.defaults. 67 * 68 * Validator object: The validation plugin returns the instance of the validator object it uses. 78 * to duplicate the plugin settings by modifying the defaults via $.validator.defaults. Use 79 * $.extend($.validator.defaults, {...}) to override multiple settings at once. 80 * 81 * Validator object: The validation plugin method returns the instance of the validator object it uses. 69 82 * That gives you full control over every aspect of the validation. Let me know if you come up 70 83 * with something that I didn't have in mind. Thats why the plugin has nearly no private methods. … … 76 89 * @desc Validates a form on submit. Rules are read from metadata. 77 90 * 78 * @example $("input").blur(function() { 79 * $(this).validate({ 80 * focusInvalid: false 81 * }); 91 * @example $("input").validate({ 92 * focusInvalid: false, 93 * event: blur 82 94 * }); 83 95 * @desc Validates all input elements on blur event (when the element looses focus). … … 197 209 * blur or keypress, each element is validated on that event. Default: submit, 198 210 * validates the entire form on submit 211 * @option Boolean onsubmit Validate the form on submit. Set to false to use only other 212 * events for validation (option event). Default: true 199 213 * @option String metaWrapper In case you use metadata for other plugins, too, you 200 214 * want to wrap your validation rules … … 210 224 $.fn.validate = function(options) { 211 225 var validator = new $.validator(options, this); 212 if( !validator.settings.event ) {226 if( validator.settings.onsubmit ) { 213 227 // validate the form on submit 214 228 this.submit(function(event) { … … 219 233 return validator.validateForm(); 220 234 }); 221 } else { 235 } 236 if( validator.settings.event ) { 222 237 // validate all elements on some other event like blur or keypress 223 238 validator.elements[validator.settings.event](function() { … … 243 258 this.currentForm = form[0]; 244 259 // find the element to look for error labels 245 this.errorCont ext= settings.errorLabelContainer.length && settings.errorLabelContainer260 this.errorContainer = settings.errorLabelContainer.length && settings.errorLabelContainer 246 261 || settings.errorContainer.length && settings.errorContainer 247 || this.currentForm; 262 || $([]) 263 this.errorContext = this.errorContainer.length && this.errorContainer || form; 248 264 249 265 // listen for focus events to save reference to last focused element … … 267 283 focusInvalid: true, 268 284 errorContainer: $([]), 269 errorLabelContainer: $([]) 285 errorLabelContainer: $([]), 286 onsubmit: true 270 287 }; 271 288 … … 323 340 } 324 341 var list = this.errorList[id] || (this.errorList[id] = []); 325 list[list.length] = method.message && this.formatMessage(method.message, rule .parameters);342 list[list.length] = method.message && this.formatMessage(method.message, rule, id); 326 343 } 327 344 } catch(e) { … … 345 362 * @param Array<Object>|Object param 346 363 */ 347 formatMessage: function(message, param) { 348 var first = param.constructor == Array ? param[0] : param; 364 formatMessage: function(message, rule, id) { 365 var m = this.settings.messages, 366 param = rule.parameters, 367 first = param.constructor == Array ? param[0] : param; 368 if(m && m[id]) { 369 if(m[id].constructor == String) 370 message = m[id]; 371 else 372 message = m[id][rule.name]; 373 } 349 374 return message.replace("{0}", first || "").replace("{1}", param[1] || ""); 350 375 }, … … 384 409 return false; 385 410 } 386 if(this.settings.debug && window.console)387 console.log("form is valid (or rules have errors)")388 411 return true; 389 412 } … … 431 454 */ 432 455 showError: function(elementID, message) { 433 var element = $("#" + elementID).addClass(this.settings.errorClass); 434 435 // find message for this label 436 var m = this.settings.messages; 437 var message = (m && m[elementID]) || element.attr('title') || message || "<strong>Warning: No message defined for " + elementID + "</strong>"; 438 439 var errorLabel = $("label." + this.settings.errorClass, this.errorContext) 440 .filter("[@for=" + elementID + "]"); 441 var w = this.settings.errorWrapper; 456 var element = $("#" + elementID).addClass(this.settings.errorClass), 457 // find message for this label 458 message = element.attr('title') || message || "<strong>Warning: No message defined for " + elementID + "</strong>", 459 errorLabel = $("label." + this.settings.errorClass, this.errorContext).filter("[@for=" + elementID + "]"), 460 wrapper = this.settings.errorWrapper; 442 461 if( errorLabel.length ) { 443 462 // check if we have a generated label, replace the message then … … 446 465 } 447 466 errorLabel.show(); 448 if( w) {449 errorLabel.parents(w ).show();467 if(wrapper) { 468 errorLabel.parents(wrapper).show(); 450 469 } 451 470 } else { … … 454 473 // TODO can't change message 455 474 var errorLabel = $("<label>").attr({"for": elementID, generated: true}).addClass("error").html(message); 456 if(w ) {457 errorLabel = errorLabel.show().wrap("<" + w + "></" + w+ ">").parent();475 if(wrapper) { 476 errorLabel = errorLabel.show().wrap("<" + wrapper + "></" + wrapper + ">").parent(); 458 477 } 459 if(!this.errorCont ext.append(errorLabel).length)478 if(!this.errorContainer.append(errorLabel).length) 460 479 errorLabel.insertAfter(element); 461 480 errorLabel.show(); … … 473 492 } else { 474 493 data = $(element).data(); 475 var meta = this.settings.metaWrapper;476 if(meta )477 data = data[meta ];494 var metaWrapper = this.settings.metaWrapper; 495 if(metaWrapper) 496 data = data[metaWrapper]; 478 497 } 479 498 var rules = []; 480 499 if(!data) 481 500 return rules; 482 $.each(data, function(key ) {501 $.each(data, function(key, value) { 483 502 var rule = rules[rules.length] = {}; 484 503 rule.name = key; 485 rule.parameters = this;504 rule.parameters = value; 486 505 }); 487 506 return rules; … … 492 511 // generate id when none is found 493 512 if(!id) { 494 var formId = element.form.id; 495 var id = element.id = (formId ? formId.replace(/[^a-zA-Z0-9\-_]/g, "") : "") + element.name.replace(/[^a-zA-Z0-9\-_]/g, ""); 513 var formId = element.form.id, 514 idcleanup = /[^a-zA-Z0-9\-_]/g; 515 id = element.id = (formId ? formId.replace(idcleanup, "") : "") + element.name.replace(idcleanup, ""); 496 516 } 497 517 return id; … … 595 615 } 596 616 default: 597 return value.length > 0;617 return $.trim(value).length > 0; 598 618 } 599 619 }, -
branches/joern-dev/fuzz/validation/validateCustomTest.html
r1362 r1370 15 15 <body> 16 16 17 < divid="texttests">17 <form action="foo.html" id="texttests"> 18 18 19 19 <h2>Some more tests with text and textarea and validating on blur</h2> … … 25 25 textarea 26 26 <textarea id="textarea" cols="25" rows="5" 27 title="Please enter a date with at least 3 and max 15 characters! (length and date)"27 title="Please enter a number with at least 3 and max 15 characters!" 28 28 test="required:true, minLength:3, maxLength:15, number:true"></textarea> 29 29 <br/> … … 58 58 <label for="family" class="error">Please select your family status.</label> 59 59 </div> 60 61 </ div>60 <input type="submit" /> 61 </form> 62 62 63 63 <script type="text/javascript"> … … 81 81 $(document).ready(function() { 82 82 $.validator.defaults.debug = true; 83 var options = { 83 84 $("#texttests").validate({ 84 85 // important: combining focus with validation on blur can crash browsers 85 86 focusInvalid: false, 86 event: "blur" 87 }; 88 89 $("#texttests").validate(options); 87 event: "blur", 88 onsbumit: false 89 }); 90 90 91 91 }); -
branches/joern-dev/fuzz/validation/validateTest.html
r1288 r1370 77 77 <li><a href="validateRadioTest.html">A very simple test to validate radio buttons</a></li> 78 78 <li><a href="validateCheckboxTest.html">A very simple test to validate checkbox buttons</a></li> 79 <li><a href="validatKeypressTest.html">Validation on keypress and submit</a></li> 79 80 <li><a href="formPluginIntegration.html">Integration with Form Plugin (AJAX submit)</a></li> 80 81 </ul>