PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax_forms/static/js/jquery.ajax_forms.validation.js

https://code.google.com/p/django-ajax-forms/
JavaScript | 150 lines | 113 code | 21 blank | 16 comment | 22 complexity | eaca528a83cf04679ead03980a5c9f63 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*! Copyright 2009 Tim Savage <tim.savage@jooceylabs.com>
  2. * Licensed under the BSD license (http://www.opensource.org/licenses/bsd-license.php)
  3. *
  4. * Version: 0.2
  5. * Requires jQuery 1.2.6+
  6. * Docs: http://code.google.com/p/django-ajax-forms/
  7. *
  8. * Validation methods, additional functions can be added to preform
  9. * custom validation eg:
  10. * $.fn.validation.rules['foo'] = function(field, arg, value, msgs) {
  11. * if (validation fail) {
  12. * throw new $.validation.ValidationError(msgs['msg_code']);
  13. * }
  14. * };
  15. */
  16. (function($) {
  17. var ValidationError = $.validation.ValidationError;
  18. // Regular expressions see http://regexpal.com/
  19. var DECIMAL_LENGTHS = /^[-\s0]*(\d*).?(\d*)\s*$/;
  20. var IS_FLOAT = /^-?\d*(\.?\d*)$/;
  21. var IS_INTEGER = /^-?\d+$/;
  22. var IS_DATE = '^(\\d{4}-((0?\\d)|(1[0-2]))-(([0-2]\\d)|(3[01])))$|' +
  23. '^((([0-2]?[0-9])|(3[01]))/((0?\\d)|(1[0-2]))/(\\d{2}|\\d{4}))$';
  24. var IS_TIME = '^(([0-1]\\d)|(2[0-3])|\\d)(:[0-5]?\\d)(:[0-5]?\\d)?$';
  25. var IS_DATETIME = '^((\\d{4}-((0?\\d)|(1[0-2]))-(([0-2]?\\d)|(3[01])))|' +
  26. '(((0?\\d)|(1[0-2]))/(([0-2]?\\d)|(3[01]))/(\\d{2}|\\d{4})?))' +
  27. '((([0-1]\\d)|(2[0-3])|\\d)(:[0-5]?\\d)(:[0-5]?\\d)?)?$';
  28. $.extend($.validation.rules, {
  29. max_length: function(field, arg, value, msgs) {
  30. if (value.length > arg) {
  31. throw new ValidationError(msgs['max_length'], {
  32. '%(limit_value)s': arg,
  33. '%(length)d': value.length
  34. });
  35. }
  36. },
  37. min_length: function(field, arg, value, msgs) {
  38. if (value.length < arg) {
  39. throw new ValidationError(msgs['min_length'], {
  40. '%(limit_value)s': arg,
  41. '%(length)d': value.length
  42. });
  43. }
  44. },
  45. decimal_length: function(field, arg, value, msgs) {
  46. var match = DECIMAL_LENGTHS.exec(value);
  47. if (match) {
  48. var max_digits = arg[0];
  49. if (max_digits !== null && (match[1].length + match[2].length) > max_digits) {
  50. throw new ValidationError(msgs['max_digits'], {
  51. '%s': max_digits
  52. });
  53. }
  54. var max_decimal_places = arg[1];
  55. if (max_decimal_places !== null && match[2].length > max_decimal_places) {
  56. throw new ValidationError(msgs['max_decimal_places'], {
  57. '%s': max_decimal_places
  58. });
  59. }
  60. if (max_digits !== null && max_decimal_places !== null) {
  61. var max_whole_digits = max_digits - max_decimal_places;
  62. if (match[1].length > max_whole_digits) {
  63. throw new ValidationError(msgs['max_whole_digits'], {
  64. '%s': max_whole_digits
  65. });
  66. }
  67. }
  68. }
  69. },
  70. is_float: function(field, arg, value, msgs) {
  71. value = $.trim(value);
  72. if (!IS_FLOAT.test(value) || isNaN(parseFloat(value))) {
  73. throw new ValidationError(msgs['invalid']);
  74. }
  75. },
  76. is_integer: function(field, arg, value, msgs) {
  77. value = $.trim(value);
  78. if (!IS_INTEGER.test(value) || isNaN(parseInt(value))) {
  79. throw new ValidationError(msgs['invalid']);
  80. }
  81. },
  82. is_date: function(field, arg, value, msgs) {
  83. var is_date = new RegExp(IS_DATE);
  84. if (!is_date.test($.trim(value))) {
  85. throw new ValidationError(msgs['invalid']);
  86. }
  87. },
  88. is_datetime: function(field, arg, value, msgs) {
  89. var is_datetime = new RegExp(IS_DATETIME);
  90. if (!is_datetime.test($.trim(value))) {
  91. throw new ValidationError(msgs['invalid']);
  92. }
  93. },
  94. is_time: function(field, arg, value, msgs) {
  95. var is_time = new RegExp(IS_TIME);
  96. if (!is_time.test($.trim(value))) {
  97. throw new ValidationError(msgs['invalid']);
  98. }
  99. },
  100. max_value: function(field, arg, value, msgs) {
  101. var value = Number(value);
  102. if (value > arg) {
  103. throw new ValidationError(msgs['max_value'], {
  104. '%(limit_value)s': arg
  105. });
  106. }
  107. },
  108. min_value: function(field, arg, value, msgs) {
  109. var value = Number(value);
  110. if (value < arg) {
  111. throw new ValidationError(msgs['min_value'], {
  112. '%(limit_value)s': arg
  113. });
  114. }
  115. },
  116. regex: function(field, arg, value, msgs) {
  117. var re = RegExp(arg[0], arg[1]);
  118. if (!re.test(value)) {
  119. throw new ValidationError(msgs['invalid']);
  120. }
  121. },
  122. equal_to_field: function(field, arg, value, msgs) {
  123. var other = $(field.form).find(':input[name='+arg+']').val();
  124. if (other != value) {
  125. throw new ValidationError(msgs['equal_to_field']);
  126. }
  127. }
  128. });
  129. })(jQuery);