Advertisement
Guest User

Untitled

a guest
Apr 17th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // -----------------------------------------
  2. // Input validation
  3. // -----------------------------------------
  4.  
  5. function validateInput($elm, ignoreEmpty) {
  6.     var value     = $elm.val();
  7.     var classTarget = getClassTarget();
  8.  
  9.     // fail if no value is entered
  10.     // or succeed if empty and should ignore empty
  11.     if (!$elm.val().length) {
  12.         if (ignoreEmpty) { return succeed(); }
  13.         else { return fail(); }
  14.     }
  15.  
  16.     // fail if this specific type doesn't validate
  17.     var type = $elm.data("validation");
  18.     if (validate.hasOwnProperty(type)
  19.         && !validate[type](value)) { return fail() }
  20.  
  21.     // everything passed; return true
  22.     return succeed();
  23.  
  24.     // helper functions
  25.     function fail() {
  26.         classTarget.addClass("group-error");
  27.         $elm[0].valid = false;
  28.         return false;
  29.     }
  30.     function succeed() {
  31.         classTarget.removeClass("group-error");
  32.         $elm[0].valid = true;
  33.         return true;
  34.     }
  35.     function getClassTarget() {
  36.         if ($elm.parent().hasClass("row")) {
  37.             return $elm.parent().parent();
  38.         }
  39.         return $elm.parent();
  40.     }
  41. };
  42.  
  43. // VALIDATORS
  44. var validate = {
  45.     // Validates a URL
  46.     url: function(str) {
  47.         var urlRegexp = /^(http(?:s)?\:\/\/[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:\/?|(?:\/[\w\-]+)*)(?:\/?|\/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*(?:\.([a-zA-Z0-9]+))?)$/i;
  48.         return urlRegexp.test(str);
  49.     },
  50.     // Validates an image URL
  51.     image: function(str) {
  52.         // if the string isn't a valid URL, fail
  53.         if (!validate.url(str)) { return false; }
  54.  
  55.         // ensure that the extension is a supported file format
  56.         var supportedFormats = ["jpg", "jpeg", "png"];
  57.         var extension = /.+\/(?:.+)?\.(.+)/.exec(str)[1];
  58.  
  59.         if (supportedFormats.indexOf(extension) === -1) { return false; }
  60.  
  61.         return true;
  62.     }
  63. };
  64.  
  65. // Validate inputs when they lose focus
  66. $("input[data-required], textarea[data-required], input[data-validation]").blur(function(){
  67.     validateInput($(this), true);
  68. })
  69. // Validate input when their text is changed
  70.   .on("input", function(){
  71.     var self = this;
  72.     // if it's currently at an error, validate instantly
  73.     if (!this.valid) {
  74.         validateInput($(self), true);
  75.         return;
  76.     }
  77.  
  78.     // otherwise, wait until the user hasn't typed for 1 sec
  79.     clearTimeout(self.validateTimer);
  80.     self.validateTimer = setTimeout(function(){
  81.         validateInput($(self), true);
  82.     }, 1000);
  83. });
  84.  
  85. // Verify forms
  86. $('form').each(function() {
  87.     // Variables
  88.     var form    = $(this);
  89.     var submit  = form.find('input[type="submit"]');
  90.  
  91.     submit.click(function(e) {
  92.         e.preventDefault();
  93.  
  94.         // Variables
  95.         var self        = $(this);
  96.         var fields      = form.find('input[data-required], textarea[data-required]').not('.btn');
  97.         var allFields   = form.find('input, textarea').not('.btn');
  98.         var valid       = true;
  99.  
  100.         fields.each(function() {
  101.             if (!validateInput($(this))) { valid = false; }
  102.         });
  103.  
  104.         // If everything went ok, do certain actions
  105.         if (valid) {
  106.             allFields.val('');
  107.         }
  108.  
  109.         console.log('Valid: ' + valid);
  110.     });
  111. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement