script.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. $(function(){
  2. //set global variables and cache DOM elements for reuse later
  3. var form = $('#contact-form').find('form'),
  4. formElements = form.find('input[type!="submit"],textarea'),
  5. formSubmitButton = form.find('[type="submit"]'),
  6. errorNotice = $('#errors'),
  7. successNotice = $('#success'),
  8. loading = $('#loading'),
  9. errorMessages = {
  10. required: ' is a required field',
  11. email: 'You have not entered a valid email address for the field: ',
  12. minlength: ' must be greater than '
  13. }
  14. //feature detection + polyfills
  15. formElements.each(function(){
  16. //if HTML5 input placeholder attribute is not supported
  17. if(!Modernizr.input.placeholder){
  18. var placeholderText = this.getAttribute('placeholder');
  19. if(placeholderText){
  20. $(this)
  21. .addClass('placeholder-text')
  22. .val(placeholderText)
  23. .bind('focus',function(){
  24. if(this.value == placeholderText){
  25. $(this)
  26. .val('')
  27. .removeClass('placeholder-text');
  28. }
  29. })
  30. .bind('blur',function(){
  31. if(this.value == ''){
  32. $(this)
  33. .val(placeholderText)
  34. .addClass('placeholder-text');
  35. }
  36. });
  37. }
  38. }
  39. //if HTML5 input autofocus attribute is not supported
  40. if(!Modernizr.input.autofocus){
  41. if(this.getAttribute('autofocus')) this.focus();
  42. }
  43. });
  44. //to ensure compatibility with HTML5 forms, we have to validate the form on submit button click event rather than form submit event.
  45. //An invalid html5 form element will not trigger a form submit.
  46. formSubmitButton.bind('click',function(){
  47. var formok = true,
  48. errors = [];
  49. formElements.each(function(){
  50. var name = this.name,
  51. nameUC = name.ucfirst(),
  52. value = this.value,
  53. placeholderText = this.getAttribute('placeholder'),
  54. type = this.getAttribute('type'), //get type old school way
  55. isRequired = this.getAttribute('required'),
  56. minLength = this.getAttribute('data-minlength');
  57. //if HTML5 formfields are supported
  58. if( (this.validity) && !this.validity.valid ){
  59. formok = false;
  60. console.log(this.validity);
  61. //if there is a value missing
  62. if(this.validity.valueMissing){
  63. errors.push(nameUC + errorMessages.required);
  64. }
  65. //if this is an email input and it is not valid
  66. else if(this.validity.typeMismatch && type == 'email'){
  67. errors.push(errorMessages.email + nameUC);
  68. }
  69. this.focus(); //safari does not focus element an invalid element
  70. return false;
  71. }
  72. //if this is a required element
  73. if(isRequired){
  74. //if HTML5 input required attribute is not supported
  75. if(!Modernizr.input.required){
  76. if(value == placeholderText){
  77. this.focus();
  78. formok = false;
  79. errors.push(nameUC + errorMessages.required);
  80. return false;
  81. }
  82. }
  83. }
  84. //if HTML5 input email input is not supported
  85. if(type == 'email'){
  86. if(!Modernizr.inputtypes.email){
  87. var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  88. if( !emailRegEx.test(value) ){
  89. this.focus();
  90. formok = false;
  91. errors.push(errorMessages.email + nameUC);
  92. return false;
  93. }
  94. }
  95. }
  96. //check minimum lengths
  97. if(minLength){
  98. if( value.length < parseInt(minLength) ){
  99. this.focus();
  100. formok = false;
  101. errors.push(nameUC + errorMessages.minlength + minLength + ' charcters');
  102. return false;
  103. }
  104. }
  105. });
  106. //if form is not valid
  107. if(!formok){
  108. //animate required field notice
  109. $('#req-field-desc')
  110. .stop()
  111. .animate({
  112. marginLeft: '+=' + 5
  113. },150,function(){
  114. $(this).animate({
  115. marginLeft: '-=' + 5
  116. },150);
  117. });
  118. //show error message
  119. showNotice('error',errors);
  120. }
  121. //if form is valid
  122. else {
  123. loading.show();
  124. $.ajax({
  125. url: form.attr('action'),
  126. type: form.attr('method'),
  127. data: form.serialize(),
  128. success: function(){
  129. showNotice('success');
  130. form.get(0).reset();
  131. loading.hide();
  132. }
  133. });
  134. }
  135. return false; //this stops submission off the form and also stops browsers showing default error messages
  136. });
  137. //other misc functions
  138. function showNotice(type,data)
  139. {
  140. if(type == 'error'){
  141. successNotice.hide();
  142. errorNotice.find("li[id!='info']").remove();
  143. for(x in data){
  144. errorNotice.append('<li>'+data[x]+'</li>');
  145. }
  146. errorNotice.show();
  147. }
  148. else {
  149. errorNotice.hide();
  150. successNotice.show();
  151. }
  152. }
  153. String.prototype.ucfirst = function() {
  154. return this.charAt(0).toUpperCase() + this.slice(1);
  155. }
  156. });