Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/js/validatorNew.es6.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* JavaScript Validator Library v0.9
* JavaScript Validator Library v1.0
* To perform effective validation and filter with form elements.
*
* Author : Shankar Thiyagaraajan
Expand Down Expand Up @@ -184,9 +184,11 @@ class jsValidator {
static checkValidation(activeElem, log) {
let jsRuleSet = new jsRuleSets();
let validElem = true;
let firstErrorHit = false;
// To Generally checks, the field is empty or not.
if (!jsRuleSets.isSet(activeElem)) {
log.push({'el': activeElem, 'type': 'required', 'id': activeElem.name});
if (false == firstErrorHit) firstErrorHit = activeElem;
}
// To Check the Value is less than min or not.
if (activeElem.min) {
Expand All @@ -197,6 +199,7 @@ class jsValidator {
'type': 'min',
'id': activeElem.name
});
if (false == firstErrorHit) firstErrorHit = activeElem;
validElem = false;
}
}
Expand All @@ -210,6 +213,7 @@ class jsValidator {
'type': 'max',
'id': activeElem.name
});
if (false == firstErrorHit) firstErrorHit = activeElem;
validElem = false;
}
}
Expand All @@ -223,6 +227,7 @@ class jsValidator {
'type': 'email',
'id': activeElem.name
});
if (false == firstErrorHit) firstErrorHit = activeElem;
validElem = false;
}
}
Expand All @@ -237,6 +242,7 @@ class jsValidator {
'type': 'password',
'id': activeElem.name
});
if (false == firstErrorHit) firstErrorHit = activeElem;
validElem = false;
}
}
Expand All @@ -250,6 +256,7 @@ class jsValidator {
}
}
}
if (false !== firstErrorHit) helper.scrollToItem(firstErrorHit);
// Return overall log report of validation.
return log;
}
Expand Down Expand Up @@ -763,6 +770,31 @@ let helper = {

// Finally return "false" for general keys.
return false;
},
// To Scroll Up / Down to notify the item that have validation message.
scrollToItem: function (item) {
// Update by Tag Name.
let elem_name = item.nodeName;

// If Element is not valid, then return false.
if (!elem_name) return false;
if (null == elem_name) return false;
item = document.getElementsByName(elem_name);

var diff = (item.offsetTop - window.scrollY) / 20;
if (!window._lastDiff) {
window._lastDiff = 0;
}
if (Math.abs(diff) > 2) {
window.scrollTo(0, (window.scrollY + diff));
clearTimeout(window._TO);
if (diff !== window._lastDiff) {
window._lastDiff = diff;
window._TO = setTimeout(this.scrollToItem, 100, item);
}
} else {
window.scrollTo(0, item.offsetTop)
}
}
};

Expand Down Expand Up @@ -908,4 +940,4 @@ let validationResponse = {
if (typeof errorMessages[errorType] === 'undefined') return false;
return errorMessages[errorType];
}
};
};
43 changes: 40 additions & 3 deletions src/js/validatorNew.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* JavaScript Validator Library v0.9
* JavaScript Validator Library v1.0
* To perform effective validation and filter with form elements.
*
* Author : Shankar Thiyagaraajan
Expand All @@ -23,7 +23,7 @@
/*
* For Managing overall Validation flow.
*/

var firstErrorHit = false;
var jsValidator = {
// Holding form element data.
formData: false,
Expand Down Expand Up @@ -177,9 +177,11 @@ var jsValidator = {
checkValidation: function (activeElem, log) {
jsLogger.out('Active Elem', activeElem);
var validElem = true;

// To Generally checks, the field is empty or not.
if (!jsRuleSets.isSet(activeElem)) {
log.push({'el': activeElem, 'type': 'required', 'id': activeElem.name});
if (false == firstErrorHit) firstErrorHit = activeElem;
}
// To Check the Value is less than min or not.
if (activeElem.min) {
Expand All @@ -190,6 +192,7 @@ var jsValidator = {
'type': 'min',
'id': activeElem.name
});
if (false == firstErrorHit) firstErrorHit = activeElem;
validElem = false;
}
}
Expand All @@ -203,6 +206,7 @@ var jsValidator = {
'type': 'max',
'id': activeElem.name
});
if (false == firstErrorHit) firstErrorHit = activeElem;
validElem = false;
}
}
Expand All @@ -216,6 +220,7 @@ var jsValidator = {
'type': 'email',
'id': activeElem.name
});
if (false == firstErrorHit) firstErrorHit = activeElem;
validElem = false;
}
}
Expand All @@ -230,6 +235,7 @@ var jsValidator = {
'type': 'password',
'id': activeElem.name
});
if (false == firstErrorHit) firstErrorHit = activeElem;
validElem = false;
}
}
Expand All @@ -244,6 +250,10 @@ var jsValidator = {
}
}
}
if (false !== firstErrorHit) {
jsLogger.out('First Hit ', firstErrorHit);
helper.scrollToItem(firstErrorHit);
}
// Return overall log report of validation.
return log;
},
Expand Down Expand Up @@ -706,6 +716,33 @@ var helper = {

// Finally return "false" for general keys.
return false;
},
// To Scroll Up / Down to notify the item that have validation message.
scrollToItem: function (item) {
// Update by Tag Name.
var elem_name = item.nodeName;

// If Element is not valid, then return false.
if (!elem_name) return false;
if (null == elem_name) return false;

// Re-Fetching elements by its Name.
item = document.getElementsByName(elem_name);

var diff = (item.offsetTop - window.scrollY) / 20;
if (!window._lastDiff) {
window._lastDiff = 0;
}
if (Math.abs(diff) > 2) {
window.scrollTo(0, (window.scrollY + diff));
clearTimeout(window._TO);
if (diff !== window._lastDiff) {
window._lastDiff = diff;
window._TO = setTimeout(this.scrollToItem, 100, item);
}
} else {
window.scrollTo(0, item.offsetTop)
}
}
};

Expand Down Expand Up @@ -852,4 +889,4 @@ var validationResponse = {
if (typeof errorMessages[errorType] === 'undefined') return false;
return errorMessages[errorType];
}
};
};