Advertisement
Guest User

CustomFormCode

a guest
Oct 29th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let result = (function () {
  2.     class Textbox {
  3.         constructor(selector, regex) {
  4.             this.selector = selector;
  5.             this._elements = $(selector);
  6.             this._invalidSymbols = regex;
  7.             this._value = '';
  8.  
  9.             let that = this;
  10.             $(selector).on('input change', function () {
  11.                 let value = $(this).val();
  12.                 $(that.selector).val(value);
  13.                 that.value = value;
  14.             });
  15.         }
  16.  
  17.         get elements() { return this._elements }
  18.  
  19.         get value() { return this._value }
  20.         set value(v) {
  21.             this._value = v;
  22.             $(this.selector).val(v);
  23.         }
  24.  
  25.         isValid() { return !this._invalidSymbols.test($(this.selector).val()); }
  26.     }
  27.  
  28.     class Form {
  29.         constructor() {
  30.             this._element = $('<div>').addClass('form');
  31.             this._textboxes = [];
  32.             for (let a of arguments) {
  33.                 if (!(a instanceof Textbox))
  34.                     throw new Error(`Element is not a Textbox`);
  35.             }
  36.             for (let a of arguments) {
  37.                 this._element.append($(a.selector));
  38.                 this._textboxes.push(a);
  39.             }
  40.         }
  41.         submit() {
  42.             for (let tb of this._textboxes) {
  43.                 if (tb.isValid()) {
  44.                     $(tb.selector).css('border', '2px solid green');
  45.                 } else {
  46.                     $(tb.selector).css('border', '2px solid red');
  47.                 }
  48.             }
  49.         }
  50.         attach(selector) {
  51.             $(selector).append(this._element);
  52.         }
  53.     }
  54.  
  55.     return {
  56.         Textbox: Textbox,
  57.         Form: Form
  58.     }
  59. })();
  60.  
  61. let Textbox = result.Textbox;
  62. let Form = result.Form;
  63. let username = new Textbox("#username",/[^a-zA-Z0-9]/);
  64. let password = new Textbox("#password",/[^a-zA-Z]/);
  65. username.value = "username";
  66. password.value = "password2";
  67. let form = new Form(username,password);
  68. form.attach("#root");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement