Advertisement
Cornerstone

form not submitting in firefox

Jul 21st, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. (function($) {
  2. $(document).ready(function() {
  3.  
  4. function sd_replaceValidationUI(form) {
  5. // Suppress the browser's default validation bubbles
  6. form.addEventListener("invalid", function(event) {
  7. event.preventDefault();
  8. }, true);
  9.  
  10. // Support Safari, iOS Safari, and the Android browser—each of which do not prevent form submissions by default
  11. form.addEventListener("submit", function(event) {
  12. if (!this.checkValidity()) {
  13. event.preventDefault();
  14. }
  15. });
  16.  
  17. //When submit is clicked, check for errors and their corresponding existing error messages
  18. var submitButton = form.querySelector("button:not([type=button]), input[type=submit]");
  19. submitButton.addEventListener("click", function(event) {
  20. var invalidFields = form.querySelectorAll(":invalid"),
  21. errorMessages = form.querySelectorAll(".error-message"),
  22. parent;
  23.  
  24. // Remove any existing messages
  25. for (var i = 0; i < errorMessages.length; i++) {
  26. errorMessages[i].parentNode.removeChild(errorMessages[i]);
  27. }
  28.  
  29. //Get custom messages from HTML data attribute for each invalid field
  30. var fields = form.querySelectorAll(".sdForm-input");
  31. for (var i = 0; i < fields.length; i++) {
  32. var message = $(fields[i]).attr("data-ErrorMessage");
  33. $(fields[i]).get(0).setCustomValidity(message);
  34. }
  35.  
  36. //Display custom messages
  37. for (var i = 0; i < invalidFields.length; i++) {
  38. parent = invalidFields[i].parentNode;
  39. parent.insertAdjacentHTML("beforeend", "<div class='error-message'>" +
  40. invalidFields[i].validationMessage +
  41. "</div>");
  42. }
  43.  
  44. // If there are errors, give focus to the first invalid field
  45. if (invalidFields.length > 0) {
  46. invalidFields[0].focus();
  47. }
  48. // Submit the form as long as there are no errors
  49. if (invalidFields.length == 0 || typeof invalidFields === 'undefined') {
  50. $("form.sd_form").submit();
  51. }
  52. });
  53.  
  54. }
  55.  
  56. // Replace the validation UI for all forms
  57. var forms = document.querySelectorAll("form");
  58. for (var i = 0; i < forms.length; i++) {
  59. sd_replaceValidationUI(forms[i]);
  60. }
  61.  
  62.  
  63. //Changes the text of the dropdown to #666 when a selection is made
  64. $('.sdForm-inputWrap select').change(function() {
  65. $(this).css('color', '#666');
  66. });
  67.  
  68. //Auto-format phone numbers while typing
  69. $(function() {
  70. $("input[type='tel']").keyup(function() {
  71. var curchr = this.value.length;
  72. var curval = $(this).val();
  73. if (curchr == 3) {
  74. $("input[type='tel']").val("(" + curval + ")" + "-");
  75. } else if (curchr == 9) {
  76. $("input[type='tel']").val(curval + "-");
  77. }
  78. });
  79. });
  80.  
  81. })
  82.  
  83.  
  84.  
  85. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement