Skip to content

Instantly share code, notes, and snippets.

@RyannosaurusRex
Created April 29, 2015 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyannosaurusRex/2901edaade59323c4afc to your computer and use it in GitHub Desktop.
Save RyannosaurusRex/2901edaade59323c4afc to your computer and use it in GitHub Desktop.
Mark Fields as Required
// Checks an element to see if the validation says it is required
// and if so, adds a '*' to the label using the 'for' semantic attribute.
function MarkAsRequired(element) {
var req = $(element).attr('data-val-required');
if (undefined != req) {
var label = $('label[for="' + $(element).attr('id') + '"]');
var text = label.text();
if (text.length > 0) {
label.append('<span style="color:red"> *</span>');
}
}
};
// EXAMPLE USAGE
// Setup the required markers for each type of input item.
$('input').each(function () {
MarkAsRequired(this);
});
$('select').each(function () {
MarkAsRequired(this);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment