form-maxlength.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*=========================================================================================
  2. File Name: form-maxlength.js
  3. Description: Bootstrap-Maxlength uses a Twitter Bootstrap label to show a visual
  4. feedback to the user about the maximum length of the field where the user is
  5. inserting text. Uses the HTML5 attribute "maxlength" to work.
  6. ----------------------------------------------------------------------------------------
  7. Item name: Vuexy - Vuejs, HTML & Laravel Admin Dashboard Template
  8. Author: Pixinvent
  9. Author URL: hhttp://www.themeforest.net/user/pixinvent
  10. ==========================================================================================*/
  11. (function (window, document, $) {
  12. 'use strict';
  13. var $danger = "#ea5455";
  14. var $primary = "#7367f0";
  15. var $textcolor = "#4e5154";
  16. $(".char-textarea").on("keyup", function (event) {
  17. checkTextAreaMaxLength(this, event);
  18. // to later change text color in dark layout
  19. $(this).addClass("active")
  20. });
  21. /*
  22. Checks the MaxLength of the Textarea
  23. -----------------------------------------------------
  24. @prerequisite: textBox = textarea dom element
  25. e = textarea event
  26. length = Max length of characters
  27. */
  28. function checkTextAreaMaxLength(textBox, e) {
  29. var maxLength = parseInt($(textBox).data("length"));
  30. if (!checkSpecialKeys(e)) {
  31. if (textBox.value.length < maxLength - 1) textBox.value = textBox.value.substring(0, maxLength);
  32. }
  33. $(".char-count").html(textBox.value.length);
  34. if (textBox.value.length > maxLength) {
  35. $(".counter-value").css("background-color", $danger);
  36. $(".char-textarea").css("color", $danger);
  37. // to change text color after limit is maxedout out
  38. $(".char-textarea").addClass("max-limit")
  39. }
  40. else {
  41. $(".counter-value").css("background-color", $primary);
  42. $(".char-textarea").css("color", $textcolor);
  43. $(".char-textarea").removeClass("max-limit")
  44. }
  45. return true;
  46. }
  47. /*
  48. Checks if the keyCode pressed is inside special chars
  49. -------------------------------------------------------
  50. @prerequisite: e = e.keyCode object for the key pressed
  51. */
  52. function checkSpecialKeys(e) {
  53. if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
  54. return false;
  55. else
  56. return true;
  57. }
  58. })(window, document, jQuery);