Skip to content

Instantly share code, notes, and snippets.

@fernandofleury
Created February 4, 2014 12:48
Show Gist options
  • Save fernandofleury/8803008 to your computer and use it in GitHub Desktop.
Save fernandofleury/8803008 to your computer and use it in GitHub Desktop.
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