PageRenderTime 23ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/validators.js

https://github.com/deadlyicon/validation.js
JavaScript | 64 lines | 51 code | 13 blank | 0 comment | 7 complexity | c3500d12b748a2e4814d494b6eb1d7ec MD5 | raw file
  1. Object.extend(Form.Element.Validators, (function() {
  2. function isBlank(value, reportErrors){
  3. if (!value.blank())
  4. reportErrors(['must be blank']);
  5. else
  6. reportErrors();
  7. };
  8. function isNotBlank(value, reportErrors){
  9. if (value.blank())
  10. reportErrors(['cannot be blank']);
  11. else
  12. reportErrors();
  13. };
  14. function isChecked(checked, reportErrors){
  15. if (!checked)
  16. reportErrors(['must be checked']);
  17. else
  18. reportErrors();
  19. };
  20. function isNotChecked(checked, reportErrors){
  21. if (!!checked)
  22. reportErrors(['cannot be checked']);
  23. else
  24. reportErrors();
  25. };
  26. var EMAIL_ADDRESS_REGEX = /^([A-Za-z0-9]{1,}([-_\.&'][A-Za-z0-9]{1,}){0,}){1,}@(([A-Za-z0-9]{1,}[-]{0,1})\.){1,}[A-Za-z]{2,6}$/;
  27. function isAnEmailAddress(value, reportErrors){
  28. if (!EMAIL_ADDRESS_REGEX.test(value))
  29. reportErrors(['must be a valid email address']);
  30. else
  31. reportErrors();
  32. };
  33. return {
  34. 'is blank' : isBlank,
  35. 'is not blank' : isNotBlank,
  36. 'is checked' : isChecked,
  37. 'is not checked' : isNotChecked,
  38. 'is an email address' : isAnEmailAddress
  39. };
  40. })());
  41. Object.extend(Form.Validators, (function() {
  42. function passwordsMatch(values, reportErrors){
  43. if (values.password != values.password_confirmation)
  44. reportErrors(['passwords do not match']);
  45. else
  46. reportErrors();
  47. }
  48. return {
  49. 'passwords match': passwordsMatch
  50. };
  51. })());