Skip to content

Instantly share code, notes, and snippets.

@azrad
Created May 30, 2013 08:42
Show Gist options
  • Save azrad/5676549 to your computer and use it in GitHub Desktop.
Save azrad/5676549 to your computer and use it in GitHub Desktop.
<form>
<div class="control-group">
<label for="name" class="control-label">Name</label>
<div class="controls">
<input type="text" name="name" id="name" class="required" />
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="submit" value="Submit" />
</div>
</div>
</form>
jQuery(document).ready(function() {
$("form").on('submit', function() {
var ok = true;
$('.control-group').removeClass('error');
$('.required').each(function() {
if ($(this).val() == "") {
ok = false;
$(this).closest('.control-group').addClass('error');
}
});
$('.error').first().find('input').focus();
if (!ok) {
return false;
}
return true;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment