Skip to content

Commit da546df

Browse files
committed
added inline errorMsg processing
allows form element to have attribute that describes customized error message to display, overwriting default error msg inline attribute name is set in config section validate and doValidate functions get that attr value and use it both onblur and onsubmit
1 parent 8a9af2f commit da546df

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

jquery.formvalidator.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@
8585

8686
var config = {
8787
validationRuleAttribute : 'data-validation',
88+
validationErrorMsgAttribute : 'data-validation-error-msg', // define custom err msg inline with element
8889
errorElementClass : 'error', // Class that will be put on elements which value is invalid
8990
borderColorOnError : 'red',
9091
dateFormat : 'yyyy-mm-dd'
92+
9193
};
9294

9395
if (settings) {
@@ -155,6 +157,7 @@
155157
borderColorOnError : 'red', // Border color of elements which value is invalid, empty string to not change border color
156158
errorMessageClass : 'jquery_form_error_message', // class name of div containing error messages when validation fails
157159
validationRuleAttribute : 'data-validation', // name of the attribute holding the validation rules
160+
validationErrorMsgAttribute : 'data-validation-error-msg', // define custom err msg inline with element
158161
errorMessagePosition : 'top', // Can be either "top" or "element"
159162
scrollToTopOnError : true,
160163
dateFormat : 'yyyy-mm-dd'
@@ -759,7 +762,9 @@ jQueryFormUtils.validateDomain = function(val) {
759762
jQueryFormUtils.validateInput = function(el, language, config, form) {
760763
var value = jQuery.trim(el.val());
761764
var validationRules = el.attr(config.validationRuleAttribute);
762-
765+
// see if form element has inline err msg attribute
766+
var validationErrorMsg = el.attr(config.validationErrorMsgAttribute);
767+
763768
if (typeof validationRules != 'undefined' && validationRules !== null) {
764769

765770
/**
@@ -775,98 +780,99 @@ jQueryFormUtils.validateInput = function(el, language, config, form) {
775780

776781
// Required
777782
if (validationRules.indexOf('required') > -1 && value === '') {
778-
return language.requiredFields;
783+
// return custom inline err msg if defined
784+
return validationErrorMsg || language.requiredFields;
779785
}
780786

781787
// Min length
782788
if (validationRules.indexOf('validate_min_length') > -1 && value.length < getAttributeInteger(validationRules, 'length')) {
783-
return language.toShortStart + getAttributeInteger(validationRules, 'length') + language.toShortEnd;
789+
return validationErrorMsg || language.tooShortStart + getAttributeInteger(validationRules, 'length') + language.tooShortEnd;
784790
}
785791

786792
// Max length
787793
if (validationRules.indexOf('validate_max_length') > -1 && value.length > getAttributeInteger(validationRules, 'length')) {
788-
return language.toLongStart + getAttributeInteger(validationRules, 'length') + language.toLongEnd;
794+
return validationErrorMsg || language.tooLongStart + getAttributeInteger(validationRules, 'length') + language.tooLongEnd;
789795
}
790796

791797
// Length range
792798
if (validationRules.indexOf('validate_length') > -1) {
793799
var range = getAttributeInteger(validationRules, 'length').split('-');
794800
if (value.length < parseInt(range[0],10) || value.length > parseInt(range[1],10)) {
795-
return language.badLength + getAttributeInteger(validationRules, 'length') + language.toLongEnd;
801+
return validationErrorMsg || language.badLength + getAttributeInteger(validationRules, 'length') + language.tooLongEnd;
796802
}
797803
}
798804

799805
// Email
800806
if (validationRules.indexOf('validate_email') > -1 && !jQueryFormUtils.validateEmail(value)) {
801-
return language.badEmail;
807+
return validationErrorMsg || language.badEmail;
802808
}
803809

804810
// Domain
805811
else if (validationRules.indexOf('validate_domain') > -1 && !jQueryFormUtils.validateDomain(value)) {
806-
return language.badDomain;
812+
return validationErrorMsg || language.badDomain;
807813
}
808814

809815
// Url
810816
else if (validationRules.indexOf('validate_url') > -1 && !jQueryFormUtils.validateUrl(value)) {
811-
return language.badUrl;
817+
return validationErrorMsg || language.badUrl;
812818
}
813819

814820
// Float
815821
else if (validationRules.indexOf('validate_float') > -1 && !jQueryFormUtils.validateFloat(value)) {
816-
return language.badFloat;
822+
return validationErrorMsg || language.badFloat;
817823
}
818824

819825
// Integer
820826
else if (validationRules.indexOf('validate_int') > -1 && !jQueryFormUtils.validateInteger(value)) {
821-
return language.badInt;
827+
return validationErrorMsg || language.badInt;
822828
}
823829

824830
// Time
825831
else if (validationRules.indexOf('validate_time') > -1 && !jQueryFormUtils.validateTime(value)) {
826-
return language.badTime;
832+
return validationErrorMsg || language.badTime;
827833
}
828834

829835
// Date
830836
else if (validationRules.indexOf('validate_date') > -1 && !jQueryFormUtils.parseDate(value, config.dateFormat)) {
831-
return language.badDate;
837+
return validationErrorMsg || language.badDate;
832838
}
833839

834840
// Birth date
835841
else if (validationRules.indexOf('validate_birthdate') > -1 && !jQueryFormUtils.validateBirthdate(value, config.dateFormat)) {
836-
return language.badDate;
842+
return validationErrorMsg || language.badDate;
837843
}
838844

839845
// Phone number
840846
else if (validationRules.indexOf('validate_phone') > -1 && !jQueryFormUtils.validatePhoneNumber(value)) {
841-
return language.badTelephone;
847+
return validationErrorMsg || language.badTelephone;
842848
}
843849

844850
// Swedish phone number
845851
else if (validationRules.indexOf('validate_swemobile') > -1 && !jQueryFormUtils.validateSwedishMobileNumber(value)) {
846-
return language.badTelephone;
852+
return validationErrorMsg || language.badTelephone;
847853
}
848854

849855
// simple spam check
850856
else if (validationRules.indexOf('validate_spamcheck') > -1 && !jQueryFormUtils.simpleSpamCheck(value, validationRules)) {
851-
return language.badSecurityAnswer;
857+
return validationErrorMsg || language.badSecurityAnswer;
852858
}
853859

854860
// UK VAT Number check
855861
else if (validationRules.indexOf('validate_ukvatnumber') > -1 && !jQueryFormUtils.validateUKVATNumber(value)) {
856-
return language.badUKVatAnswer;
862+
return validationErrorMsg || language.badUKVatAnswer;
857863
}
858864

859865
// Custom regexp validation
860866
if (validationRules.indexOf('validate_custom') > -1 && validationRules.indexOf('regexp/') > -1) {
861867
var regexp = new RegExp(validationRules.split('regexp/')[1].split('/')[0]);
862868
if (!regexp.test(value)) {
863-
return language.badCustomVal;
869+
return validationErrorMsg || language.badCustomVal;
864870
}
865871
}
866872

867873
// Swedish social security number
868874
if (validationRules.indexOf('validate_swesc') > -1 && !jQueryFormUtils.validateSwedishSecurityNumber(value)) {
869-
return language.badSecurityNumber;
875+
return validationErrorMsg || language.badSecurityNumber;
870876
}
871877

872878
// confirmation
@@ -877,7 +883,7 @@ jQueryFormUtils.validateInput = function(el, language, config, form) {
877883
conf = confInput.val();
878884
}
879885
if (value !== conf) {
880-
return language.notConfirmed;
886+
return validationErrorMsg || language.notConfirmed;
881887
}
882888
}
883889
}
@@ -898,10 +904,10 @@ jQueryFormUtils.LANG = {
898904
badTelephone : 'You have not given a correct phone number',
899905
badSecurityAnswer : 'You have not given a correct answer to the security question',
900906
badDate : 'You have not given a correct date',
901-
toLongStart : 'You have given an answer longer than ',
902-
toLongEnd : ' characters',
903-
toShortStart : 'You have given an answer shorter than ',
904-
toShortEnd : ' characters',
907+
tooLongStart : 'You have given an answer longer than ',
908+
tooLongEnd : ' characters',
909+
tooShortStart : 'You have given an answer shorter than ',
910+
tooShortEnd : ' characters',
905911
badLength : 'You have to give an answer between ',
906912
notConfirmed : 'Values could not be confirmed',
907913
badDomain : 'Incorrect domain value',

0 commit comments

Comments
 (0)