formvalidation
Version:
The best jQuery plugin to validate form fields. Support Bootstrap, Foundation, Pure, SemanticUI, UIKit and custom frameworks
50 lines (46 loc) • 1.76 kB
JavaScript
/**
* stringCase validator
*
* @link http://formvalidation.io/validators/stringCase/
* @author https://twitter.com/nghuuphuoc
* @copyright (c) 2013 - 2015 Nguyen Huu Phuoc
* @license http://formvalidation.io/license/
*/
(function($) {
FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
'en_US': {
stringCase: {
'default': 'Please enter only lowercase characters',
upper: 'Please enter only uppercase characters'
}
}
});
FormValidation.Validator.stringCase = {
html5Attributes: {
message: 'message',
'case': 'case'
},
/**
* Check if a string is a lower or upper case one
*
* @param {FormValidation.Base} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Consist of key:
* - message: The invalid message
* - case: Can be 'lower' (default) or 'upper'
* @returns {Object}
*/
validate: function(validator, $field, options) {
var value = validator.getFieldValue($field, 'stringCase');
if (value === '') {
return true;
}
var locale = validator.getLocale(),
stringCase = (options['case'] || 'lower').toLowerCase();
return {
valid: ('upper' === stringCase) ? value === value.toUpperCase() : value === value.toLowerCase(),
message: options.message || (('upper' === stringCase) ? FormValidation.I18n[locale].stringCase.upper : FormValidation.I18n[locale].stringCase['default'])
};
}
};
}(jQuery));