Created
February 4, 2014 12:48
-
-
Save fernandofleury/8803008 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.validateForm = function(form) { | |
var $form = form, | |
$requiredFields = $form.find('[data-required="true"]'), | |
mailRegex = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"); | |
$requiredFields.each(function () { | |
var $this = $(this), | |
$element = $this.is('input') ? $this.parent() : $this; | |
if (!$this.val()) { | |
$element.addClass('error-field'); | |
} else { | |
if ($this.is('[data-regex="email"]')) { | |
if (mailRegex.test($this.val())) { | |
$element.removeClass('error-field'); | |
} else { | |
$element.addClass('error-field'); | |
} | |
} else if ($this.is('[data-confirm]')) { | |
var toCheck = $this.data('confirm'); | |
var $input = $form.find('[name="' + toCheck + '"]'); | |
if (!($input.parent().hasClass('error-field')) && $this.val() === $input.val()) { | |
$element.removeClass('error-field'); | |
} else { | |
$element.addClass('error-field'); | |
} | |
} else { | |
$element.removeClass('error-field'); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment