Advertisement
Guest User

Untitled

a guest
Oct 9th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import { observable, action } from "mobx";
  2.  
  3. class FormStore {
  4. @observable username = "";
  5. @observable email = "";
  6. @observable password = "";
  7. @observable message = "";
  8. @observable isValid = false;
  9. @observable usernameError = "";
  10. @observable emailError = "";
  11. @observable passwordError = "";
  12. @observable messageError = "";
  13.  
  14.  
  15. @action
  16. usernameOnChange(id) {
  17. this.username = id;
  18. this.validateUsername();
  19. }
  20.  
  21. @action
  22. validateUsername() {
  23. const usernamePatter = /[^a-zA-Z0-9 ]/i;
  24. const required = this.username ? undefined : "Required";
  25. this.usernameError = required
  26. ? required
  27. : usernamePatter.test(this.username) ? undefined : "Invalid username";
  28. }
  29.  
  30.  
  31. @action
  32. emailOnChange(id) {
  33. this.email = id;
  34. this.validateEmail();
  35. }
  36.  
  37. @action
  38. validateEmail() {
  39. const emailPatter = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i;
  40. const required = this.email ? undefined : "Required";
  41. this.emailError = required
  42. ? required
  43. : emailPatter.test(this.email) ? undefined : "Invalid email address";
  44. }
  45.  
  46. @action
  47. passwordOnChange(pwd) {
  48. this.password = pwd;
  49. this.validatePassword();
  50. }
  51.  
  52. @action
  53. validatePassword() {
  54. const alphaNumeric = /[^a-zA-Z0-9 ]/i.test(this.password)
  55. ? "Only alphanumeric characters"
  56. : undefined;
  57. const maxLength =
  58. this.password.length > 15 ? "Must be 15 characters or less" : undefined;
  59. const minLength =
  60. this.password.length < 6 ? "Must be 8 characters or more" : undefined;
  61. const required = this.password ? undefined : "Required";
  62. this.passwordError = required
  63. ? required
  64. : alphaNumeric ? alphaNumeric : maxLength ? maxLength : minLength;
  65. }
  66.  
  67. @action
  68. messageOnChange(id) {
  69. this.message = id;
  70. this.validateMessage();
  71. }
  72.  
  73. @action
  74. validateMessage() {
  75. const messagePatter = /[^a-zA-Z0-9 ]/i;
  76. const required = this.message ? undefined : "Required";
  77. this.messageError = required
  78. ? required
  79. : messagePatter.test(this.message) ? undefined : "Please write a message ";
  80. }
  81.  
  82.  
  83. @action
  84. validateSigninForm() {
  85. if (this.emailError === undefined && this.passwordError === undefined) {
  86. this.isValid = true;
  87. }
  88. }
  89.  
  90. @action
  91. validateSignupForm() {
  92. if (this.usernameError === undefined & this.emailError === undefined && this.passwordError === undefined) {
  93. this.isValid = true;
  94. }
  95. }
  96.  
  97. @action
  98. validateForgetpassForm() {
  99. if (this.emailError === undefined) {
  100. this.isValid = true;
  101. }
  102. }
  103.  
  104.  
  105. @action
  106. validateContactForm() {
  107. if (this.emailError === undefined && this.messageError === undefined) {
  108. this.isValid = true;
  109. }
  110. }
  111.  
  112.  
  113. @action
  114. clearSignin() {
  115. this.email = "";
  116. this.isValid = false;
  117. this.emailError = "";
  118. this.password = "";
  119. this.passwordError = "";
  120. }
  121. }
  122.  
  123. @action
  124. clearSignup() {
  125. this.username = "";
  126. this.email = "";
  127. this.isValid = false;
  128. this.usernameError = "";
  129. this.emailError = "";
  130. this.password = "";
  131. this.passwordError = "";
  132. }
  133. }
  134.  
  135. @action
  136. clearForgetpass() {
  137. this.email = "";
  138. this.isValid = false;
  139. this.emailError = "";
  140. }
  141. }
  142.  
  143. @action
  144. clearContact() {
  145. this.email = "";
  146. this.isValid = false;
  147. this.emailError = "";
  148. this.message = "";
  149. this.messageError = "";
  150. }
  151. }
  152.  
  153. export default FormStore;
  154.  
  155. @action
  156. clearSignup() {
  157. this.username = "";
  158. this.email = "";
  159. this.isValid = false;
  160. this.usernameError = "";
  161. this.emailError = "";
  162. this.password = "";
  163. this.passwordError = "";
  164. }
  165. }
  166.  
  167. @action
  168. clearForgetpass() {
  169. this.email = "";
  170. this.isValid = false;
  171. this.emailError = "";
  172. }
  173. }
  174.  
  175. @action
  176. clearContact() {
  177. this.email = "";
  178. this.isValid = false;
  179. this.emailError = "";
  180. this.message = "";
  181. this.messageError = "";
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement