PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/webapp/static/js/validate.result.new.js

https://github.com/Hooyoo/HealtheMe
JavaScript | 172 lines | 150 code | 6 blank | 16 comment | 10 complexity | 7c9a87f9f46fc0cebc3731934f37a7e6 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright (C) 2012 KRM Associates, Inc. healtheme@krminc.com
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. $.validator.methods.regexMatch = function(value, element, param) {
  17. var re = new RegExp(param);
  18. return value.match(re);
  19. };
  20. $.validator.methods.notEqualTo = function(value, element, param) {
  21. return value != $(param).val();
  22. };
  23. $.validator.methods.regexMatchOrEmpty = function(value, element, param) {
  24. if (value.length > 0) {
  25. var re = new RegExp(param);
  26. return value.match(re);
  27. } else {
  28. return true;
  29. }
  30. };
  31. $().ready(function() {
  32. $(":input").bind("onBlur" ,function(){
  33. $(this).valid()}
  34. );
  35. $("#detailform").bind("invalid-form.validate", function(e, validator) {
  36. if (validator.numberOfInvalids()) {
  37. $("#notifier").html("<strong class=\"error\">Please fix any fields highlighted in red before continuing.<\/strong>").slideDown();
  38. } else {
  39. $("#notifier").html("").slideUp();
  40. }
  41. }).validate({
  42. submitHandler: function() {
  43. var sourceid = $("input#sourceid").val();
  44. var testname = $("input#testname").val();
  45. var result = $("input#result").val();
  46. var resultunit = $("input#resultunit").val();
  47. var resultdate = $("input#resultdate").val();
  48. var patientid = $("input#patientid").val();
  49. jsonObject = JSON.stringify({
  50. "@uri":ctx +'/results/',
  51. "healthRecordId":patientid,
  52. "dataSourceId":"",
  53. "sourceId":sourceid,
  54. "resultId":"",
  55. "testName":testname,
  56. "resultDateExact":resultdate,
  57. "result":result,
  58. "resultUnit":resultunit},null,1);
  59. /** Add using REST API **/
  60. $.ajax({
  61. type: "POST",
  62. url: apipath + "/results/",
  63. data: jsonObject,
  64. contentType: "application/json",
  65. success: function() {
  66. $('#notifier')
  67. .html('Created record.')
  68. .removeClass('ui-state-error').addClass('ui-state-highlight')
  69. .slideDown();
  70. document.forms['detailform'].reset();
  71. },
  72. error: function() {
  73. $('#notifier').html('Unable to create new record.')
  74. .removeClass('ui-state-highlight').addClass('ui-state-error')
  75. .slideDown();
  76. }
  77. });
  78. return false;
  79. },
  80. errorPlacement: function(error, element) {
  81. if(error.text().length > 1){
  82. var found=false;
  83. function foundFunction (bool) { found = bool;}
  84. $(element).nextAll().each(function(){
  85. if ($(this).hasClass("errorText")) {
  86. $(this).text(error.text());
  87. foundFunction(true);
  88. }
  89. });
  90. if (!found) {
  91. element.parent()
  92. .append('<div class="errorText">' + error.text() + '</p>')
  93. .slideDown();
  94. }
  95. } else {
  96. $(element).nextAll().each(function(){
  97. if ($(this).hasClass("errorText")) {
  98. $(this).remove();
  99. }
  100. });
  101. }
  102. },
  103. success: function(label) {
  104. },
  105. highlight: function(element, errorClass) {
  106. $(element).parent().addClass(errorClass);
  107. },
  108. unhighlight: function(element, errorClass) {
  109. $(element).parent().removeClass(errorClass);
  110. },
  111. rules: {
  112. sourceid: {
  113. required:true
  114. },
  115. action: {
  116. required:true
  117. },
  118. sourceurl: {
  119. required:true,
  120. minlength:1
  121. },
  122. returnurl: {
  123. required:true,
  124. minlength:1
  125. },
  126. patientid: {
  127. required:true,
  128. digits:true
  129. },
  130. testname: {
  131. required:true,
  132. minlength:1
  133. },
  134. resultdate: {
  135. regexMatchOrEmpty: /^(([0]?[1-9])|(1[0-2]))\/(([0]?[1-9])|([1,2]\d{1})|([3][0,1]))\/[12]\d{3}$/
  136. }
  137. },
  138. messages: {
  139. sourceid: {
  140. required:'Form is invalid, please refresh and try again.'
  141. },
  142. action: {
  143. required:'Form is invalid, please refresh and try again.'
  144. },
  145. sourceurl: {
  146. required:'Form is invalid, please refresh and try again.',
  147. minlength:'Form is invalid, please refresh and try again.'
  148. },
  149. returnurl: {
  150. required:'Form is invalid, please refresh and try again.',
  151. minlength:'Form is invalid, please refresh and try again.'
  152. },
  153. patientid: {
  154. required:'Form is invalid, please refresh and try again.',
  155. digits:'Form is invalid, please refresh and try again.'
  156. },
  157. testname: {
  158. required:'Please enter a test name.',
  159. minlength:'Please enter a longer test name.'
  160. },
  161. resultdate: {
  162. regexMatchOrEmpty:'Selected date is invalid. Please use MM/DD/YYYY format.'
  163. }
  164. }
  165. });
  166. });