Skip to content

Instantly share code, notes, and snippets.

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 relipse/5b8646cd3da6898f049e12fde717464a to your computer and use it in GitHub Desktop.
Save relipse/5b8646cd3da6898f049e12fde717464a to your computer and use it in GitHub Desktop.
/**
* Generate an array of English requirements from the html element validation
* @param $element
* @returns {*[]}
*/
const generateRequirementsFromHtmlValidation = function($element){
var min = null;
var max = null;
var minlength = null;
var maxlength = null;
var pattern = null;
var requirements = [];
if ($element.attr('required')){
requirements.push('required');
}
if ((min = $element.attr('min')) !== undefined){
requirements.push('a minimum of '+min);
}
if ((max = $element.attr('max')) !== undefined){
requirements.push('a maximum of '+max);
}
if ((minlength = $element.attr('minlength')) !== undefined){
requirements.push('a minimum length of '+minlength);
}
if ((maxlength = $element.attr('maxlength')) !== undefined){
requirements.push('a maximum length of '+maxlength);
}
if ((pattern = $element.attr('pattern')) !== undefined){
requirements.push('has a regular expression of "'+pattern+'"');
}
return requirements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment