| 1 | <!DOCTYPE HTML> |
| 2 | <html> |
| 3 | <head> |
| 4 | <script src="../../../dojo/dojo.js"></script> |
| 5 | <script> |
| 6 | require(["dojo/dom", "dojo/on", "dojo/dom-construct", "dojo/query", "dojo/_base/array", "dojox/validate", "dojox/validate/check", "dojox/validate/web", "dojo/domReady!" |
| 7 | ], function(dom, on, domConstruct, query, arrayUtil, validate) { |
| 8 | var thisForm = dom.byId('myForm'); |
| 9 | on(thisForm, "submit", function(event) { |
| 10 | event.preventDefault(); |
| 11 | var profile = { |
| 12 | required: [ "field1", "field2", "field3" ], |
| 13 | constraints: { |
| 14 | field1: [myValidationFunction, { "message" : "Field 1 is incorrect. Please try again." }], |
| 15 | field2: [validate.isEmailAddress, false, true], |
| 16 | field3: ["myValidationFunction", { "message" : "Field 3 is incorrect. Please try again." }] |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | var results = validate.check(thisForm, profile); |
| 21 | if (results.isSuccessful()) { // If validation is a success |
| 22 | return true; |
| 23 | } else { |
| 24 | // Start by removing error messages already generated from previous submission attempt |
| 25 | query(".error", thisForm).forEach(domConstruct.destroy); |
| 26 | |
| 27 | var missing = results.getMissing(); |
| 28 | if (missing.length){ |
| 29 | arrayUtil.forEach(missing, function(missingField){ |
| 30 | domConstruct.create("span", { innerHTML : missingField + " is required.", "class" : "error" }, query("input[name=" + missingField + "]")[0], "after"); |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | var invalid = results.getInvalid(); |
| 35 | console.log(invalid); |
| 36 | if (invalid.length) { |
| 37 | arrayUtil.forEach(invalid, function(constraintResponse){ |
| 38 | domConstruct.create("span", { innerHTML : constraintResponse.message || "Invalid input. Please try again.", "class" : "error" }, query("input[name=" + constraintResponse.field + "]")[0], "after"); |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | return false; |
| 43 | } |
| 44 | }); |
| 45 | }); |
| 46 | function myValidationFunction(value, options) { |
| 47 | // validation stuff goes here |
| 48 | return { isValid : false, message : options.message } |
| 49 | } |
| 50 | </script> |
| 51 | <style> |
| 52 | body { font-family: sans-serif; } |
| 53 | .error { font-size:0.8em; color:red; font-style:italic; } |
| 54 | dd { margin-bottom:20px; } |
| 55 | input[type=text] { padding:5px; color:#242424; border:1px solid #242424; width: 50%; margin-right:5%; } |
| 56 | input[type=submit] { padding:10px; font-weight:bold; text-transform:uppercase; margin:10px 0; } |
| 57 | </style> |
| 58 | </head> |
| 59 | <body> |
| 60 | <form id="myForm"> |
| 61 | <fieldset> |
| 62 | <legend>Validate Check Demo</legend> |
| 63 | <dl> |
| 64 | <dt><label for="field1">Field 1 (custom validation function)</label></dt> |
| 65 | <dd><input type='text' name='field1' value='field1 test value' /></dd> |
| 66 | <dt><label for="field2">Field 2 (validate.isEmailAddress)</label></dt> |
| 67 | <dd><input type='text' name='field2' value='[email protected]' /></dd> |
| 68 | <dt><label for="field3">Field 3 (custom validation function called by string)</label></dt> |
| 69 | <dd><input type='text' name='field3' value='field3 test value' /></dd> |
| 70 | </dl> |
| 71 | </fieldset> |
| 72 | <input type="submit" value="Submit" /> |
| 73 | </form> |
| 74 | </body> |
| 75 | </html> |