Skip to content

Commit 03f6d3c

Browse files
author
shankarThiyagaraajan
committed
- Error element scroll | Fixed.
- Simplified flow.
1 parent 03c395f commit 03f6d3c

3 files changed

Lines changed: 69 additions & 24 deletions

File tree

src/demo/index3.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,14 @@
110110
form: 'clientAdd',
111111
forceFilter: true,
112112
onChange: true,
113+
errorClass: '__error_cap',
113114
message: {
114-
required: 'This field is required !',
115-
min: 'This field is less then the limit [INDEX]',
116-
max: 'This field is exceeds the limit of [INDEX]',
117-
password: 'Password doesn\'t match !',
118-
email: 'Invalid Email found !'
115+
required: 'This field is required.',
116+
min: 'This field length is too low.',
117+
max: 'This field length is exceeds the limit.',
118+
password: 'Password does not match.',
119+
email: 'Email is not valid.',
120+
file: 'This file is not allowed.'
119121
}
120122
});
121123

src/js/formValidator.js

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* Released under the MIT license
1818
* https://github.com/global-source/javascript_form_validator/blob/master/LICENSE
1919
*
20-
* Date: 2017-05-01
20+
* Date: 2017-07-21
2121
*/
2222

2323
/*
@@ -59,6 +59,8 @@ var jsValidator = {
5959
this.onlyFilter = option.onlyFilter;
6060
// To Enable/Disable global validator.
6161
this.onChange = option.onChange;
62+
// Update default response "class".
63+
if ('undefined' === typeof option.errorClass) option.errorClass = 'js-error-cop';
6264
// Update "jsSettings" to global object.
6365
this.jsSettings = jsSettings.init(option);
6466
// Update "jsForm" to global object.
@@ -139,6 +141,7 @@ var jsValidator = {
139141
}
140142
if (false == this.initialLoad) validationResponse.init(errorList, this.option);
141143
this.initialLoad = false;
144+
helper.scrollToError();
142145
return status;
143146
},
144147
/*
@@ -221,7 +224,7 @@ var jsFilter = {
221224
checkStatus: function (elem) {
222225
var status;
223226
status = true;
224-
if (false === this.forceFilter) {
227+
if (false === jsValidator.forceFilter) {
225228
status = false;
226229
if (true === elem.required) {
227230
status = true;
@@ -296,8 +299,8 @@ var jsFilter = {
296299
var min = event.target.min;
297300
var max = event.target.max;
298301
// Default values for Min and Max.
299-
if (!min) min = 0;
300-
if (!max) max = 54;
302+
if (!min) min = 1;
303+
if (!max) max = 31;
301304
// Forming pattern for Restriction.
302305
var regex = new RegExp('^[0-9]+$');
303306
// Validation with Code.
@@ -309,7 +312,7 @@ var jsFilter = {
309312
event.preventDefault();
310313
}
311314

312-
var num = +this.value, max = 31, min = 1; //converts value to a Number
315+
var num = +this.value; //converts value to a Number
313316
if (!this.value.length) return false; //allows empty field
314317
this.value = isNaN(num) ? min : num > max ? max : num < min ? min : num;
315318

@@ -535,7 +538,6 @@ var jsRuleSets = {
535538
'id': activeElem.name + '_new1_1_1xv_resp'
536539
});
537540
firstErrorHit = activeElem.name + '_new1_1_1xv_resp';
538-
helper.scrollToItem(firstErrorHit);
539541
}
540542

541543
// To Check the Value is less than minimum or not.
@@ -548,7 +550,6 @@ var jsRuleSets = {
548550
'id': activeElem.name + '_new1_1_1xv_resp'
549551
});
550552
firstErrorHit = activeElem.name + '_new1_1_1xv_resp';
551-
helper.scrollToItem(firstErrorHit);
552553
validElem = false;
553554
}
554555
}
@@ -564,7 +565,6 @@ var jsRuleSets = {
564565
'id': activeElem.name + '_new1_1_1xv_resp'
565566
});
566567
firstErrorHit = activeElem.name + '_new1_1_1xv_resp';
567-
helper.scrollToItem(firstErrorHit);
568568
validElem = false;
569569
}
570570
}
@@ -580,7 +580,6 @@ var jsRuleSets = {
580580
'id': activeElem.name + '_new1_1_1xv_resp'
581581
});
582582
firstErrorHit = activeElem.name + '_new1_1_1xv_resp';
583-
helper.scrollToItem(firstErrorHit);
584583
validElem = false;
585584
}
586585
}
@@ -597,7 +596,6 @@ var jsRuleSets = {
597596
'id': activeElem.name + '_new1_1_1xv_resp'
598597
});
599598
firstErrorHit = activeElem.name + '_new1_1_1xv_resp';
600-
helper.scrollToItem(firstErrorHit);
601599
validElem = false;
602600
}
603601
}
@@ -816,6 +814,19 @@ var helper = {
816814
// Finally return "false" for general keys.
817815
return false;
818816
},
817+
/*
818+
* To Scroll Up / Down to notify the item that have validation message.
819+
*/
820+
scrollToError: function () {
821+
var active_class = validationResponse.getClass();
822+
if (0 === document.getElementsByClassName(active_class).length) return false;
823+
document.getElementsByClassName(active_class)[0].setAttribute('id', '__header_error_target_temp');
824+
var id = '#' + document.getElementsByClassName(active_class)[0].id;
825+
window.location.href = id;
826+
document.getElementsByClassName(active_class)[0].removeAttribute('id');
827+
// Remove the navigated value.
828+
this.removeHash(id);
829+
},
819830
/*
820831
* To Scroll Up / Down to notify the item that have validation message.
821832
*/
@@ -838,7 +849,7 @@ var helper = {
838849
// Replacing the URL with specific hash value.
839850
path = path.replace(hash, '');
840851
// Update to url history.
841-
window.history.pushState('', 'Title', path)
852+
window.history.pushState('', 'Title', path);
842853
}
843854
};
844855
/**
@@ -883,16 +894,20 @@ var pattern = {
883894
* To Manage all kind of error response.
884895
*/
885896
var validationResponse = {
897+
active_class: false,
886898
/*
887899
* Initiating the Response handler.
888900
*/
889901
init: function (errorList, option) {
890902
this.errorMessage = option.message;
903+
// Updating the class.
904+
this.active_class = option.errorClass;
891905
// var errorElements = option.errorElem;
892906
// jsLogger.out('Errors', errorList);
893907
this.input(errorList.input);
894908
this.select(errorList.select);
895909
this.textArea(errorList.textArea);
910+
896911
},
897912
/*
898913
* To handle the "input" element.
@@ -906,6 +921,9 @@ var validationResponse = {
906921
select: function (elem) {
907922
this.process(elem);
908923
},
924+
getClass: function () {
925+
return this.active_class;
926+
},
909927
/*
910928
* To handle the "textArea" element.
911929
*/
@@ -917,6 +935,7 @@ var validationResponse = {
917935
*/
918936
process: function (elem) {
919937
var elementDefaultResponse = '';
938+
var active_class = this.getClass();
920939
for (var i in elem) {
921940
// jsLogger.out('Element', document.getElementById(elem[i].id));
922941
if (elem[i].el && true === elem[i].el.required) {
@@ -932,6 +951,7 @@ var validationResponse = {
932951
// jsLogger.out('Element Found', false);
933952
spanTag = document.createElement('span');
934953
spanTag.setAttribute('id', activeElem.id);
954+
spanTag.setAttribute('class', active_class);
935955
spanTag.innerHTML = elementDefaultResponse;
936956
} else {
937957
// Re-use Existing response Message SPAN.
@@ -980,16 +1000,17 @@ var validationResponse = {
9801000
* then it will be replaces.
9811001
*/
9821002
default: function (errorType) {
1003+
var active_class = this.getClass();
9831004
var errorMessages = {
984-
required: 'This field is required',
985-
min: 'This field length is too low.',
986-
max: 'This field length is exceeds the limit',
987-
password: 'Password does not match.',
988-
email: 'Email is not valid',
989-
file: 'This file is not allowed'
1005+
required: '<span class="' + active_class + '">This field is required.</span>',
1006+
min: '<span class="' + active_class + '">This field length is too low.</span>',
1007+
max: '<span class="' + active_class + '">This field length is exceeds the limit.</span>',
1008+
password: '<span class="' + active_class + '">Password does not match.</span>',
1009+
email: '<span class="' + active_class + '">Email is not valid.</span>',
1010+
file: '<span class="' + active_class + '">This file is not allowed.</span>'
9901011
};
9911012
if (typeof errorType !== 'string') return false;
9921013
if (typeof errorMessages[errorType] === 'undefined') return false;
9931014
return errorMessages[errorType];
9941015
}
995-
};
1016+
};

0 commit comments

Comments
 (0)