function getError(field) {
var validity = field.validity;
if (validity.valid) return;
if (field.disabled || field.type === 'file' || field.type === 'reset' || field.type === 'submit' || field.type === 'button') return;
if (validity.valueMissing) {
if (field.type === 'checkbox') return msg.valueMissingCheckbox;
if (field.type === 'radio') return msg.valueMissingRadio;
if (field.type === 'select-one') return msg.valueMissingSelect;
if (field.type === 'select-multiple') return msg.valueMissingSelectMulti;
return msg.valueMissing;
}
if (validity.typeMismatch) {
if (field.type === 'email') return msg.typeMismatchEmail;
if (field.type === 'url') return msg.typeMismatchURL;
}
if (validity.tooShort) return msg.tooShort(field);
if (validity.tooLong) return msg.tooLong(field);
if (validity.badInput) return msg.badInput;
if (validity.stepMismatch) return msg.stepMismatch;
if (validity.rangeOverflow) return msg.rangeOverflow;
if (validity.rangeUnderflow) return msg.rangeUnderflow;
if (validity.patternMismatch) {
if (field.hasAttribute('title')) return field.getAttribute('title');
return msg.patternMismatch;
}
return msg.generic;
}
var IGNORED_TYPES = [
'file',
'reset',
'submit',
'button'
].reduce(function(acc, type) {
acc[type] = 1;
return acc;
}, {});
var VALUE_MISSING_MESSAGES = {
checkbox: msg.valueMissingCheckbox,
radio: msg.valueMissingRadio,
'select-one': msg.valueMissingSelect,
'select-multiple': msg.valueMissingSelectMulti
};
var TYPE_MISMATCH_MESSAGES = {
email: msg.typeMismatchEmail,
url: msg.typeMismatchURL
};
function getError(field) {
var validity = field.validity;
var type = field.type;
switch (true) {
case (validity.valid || field.disabled || type in IGNORED_TYPES):
return;
case validity.valueMissing:
return VALUE_MISSING_MESSAGES[type] || msg.valueMissing;
case (validity.typeMismatch && type in TYPE_MISMATCH_MESSAGES):
return TYPE_MISMATCH_MESSAGES[type];
case validity.tooShort:
return msg.tooShort(field);
case validity.tooLong:
return msg.tooLong(field);
case validity.badInput:
return msg.badInput;
case validity.stepMismatch:
return msg.stepMismatch;
case validity.rangeOverflow:
return msg.rangeOverflow;
case validity.rangeUnderflow:
return msg.rangeUnderflow;
case validity.patternMismatch:
if (field.hasAttribute('title'))
return field.getAttribute('title');
return msg.patternMismatch;
}
return msg.generic;
}