Guest User

Untitled

a guest
Feb 26th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. <script id="customMessageTemplate" type="text/html">
  2. <em class="customMessage" data-bind='validationMessage: field'></em>
  3. </script>
  4. <fieldset>
  5. <legend>User: <span data-bind='text: errors().length'></span> errors</legend>
  6. <label>First name: <input data-bind='value: firstName'/></label>
  7. <label>Last name: <input data-bind='value: lastName'/></label>
  8. <div data-bind='validationOptions: { messageTemplate: "customMessageTemplate" }'>
  9. <label>Email: <input data-bind='value: emailAddress' required pattern="@"/></label>
  10. <label>Location: <input data-bind='value: location'/></label>
  11. <label>Age: <input data-bind='value: age' required/></label>
  12. </div>
  13. <label>
  14. Subscriptions:
  15. <select data-bind='value: subscription, options: subscriptionOptions, optionsCaption: "Choose one..."'></select>
  16. </label>
  17. <label>Password: <input data-bind='value: password' type="password"/></label>
  18. <label>Retype password: <input data-bind='value: confirmPassword' type="password"/></label>
  19. <label>10 + 1 = <input data-bind='value: captcha'/></label>
  20. </fieldset>
  21. <button type="button" data-bind='click: submit'>Submit</button>
  22. <br />
  23. <br />
  24. <button type="button" data-bind='click: requireLocation'>Make 'Location' required</button>
  25.  
  26. ko.validation.rules.pattern.message = 'Invalid.';
  27. ko.validation.configure({
  28. registerExtenders: true,
  29. messagesOnModified: true,
  30. insertMessages: true,
  31. parseInputAttributes: true,
  32. messageTemplate: null
  33. });
  34.  
  35. var captcha = function (val) {
  36. return val == 11;
  37. };
  38.  
  39. var mustEqual = function (val, other) {
  40. return val == other();
  41. };
  42.  
  43. var viewModel = {
  44. firstName: ko.observable().extend({ minLength: 2, maxLength: 10 }),
  45. lastName: ko.observable().extend({ required: true }),
  46. emailAddress: ko.observable().extend({ // custom message
  47. required: { message: 'Please supply your email address.' }
  48. }),
  49. age: ko.observable().extend({ min: 1, max: 100 }),
  50. location: ko.observable(),
  51. subscriptionOptions: ['Technology', 'Music'],
  52. subscription: ko.observable().extend({ required: true }),
  53. password: ko.observable(),
  54. captcha: ko.observable().extend({ // custom validator
  55. validation: { validator: captcha, message: 'Please check.' }
  56. }),
  57. submit: function () {
  58. if (viewModel.errors().length == 0) {
  59. alert('Thank you.');
  60. } else {
  61. alert('Please check your submission.');
  62. viewModel.errors.showAllMessages();
  63. }
  64. }
  65. };
  66.  
  67. viewModel.confirmPassword = ko.observable().extend({
  68. validation: { validator: mustEqual, message: 'Passwords do not match.', params: viewModel.password }
  69. }),
  70. viewModel.errors = ko.validation.group(viewModel);
  71. viewModel.requireLocation = function () {
  72. viewModel.location.extend({ required: true });
  73. };
  74. ko.applyBindings(viewModel);
Add Comment
Please, Sign In to add comment