Skip to content

- Validation response handling Improved [Core & ES6]. #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2017
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
24 changes: 19 additions & 5 deletions src/demo/index2.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<form method="post" id="form2submit" novalidate>
<div>
<label>Only Alpha</label>
<input type="text" data-allow="onlyAlpha" name="name1" data-message="This field is required." required>
<input type="text" data-allow="onlyAlpha" name="name1"
data-message="<b style='color:green'>This field is required.</b>" required>
</div>
<div>
<label>String</label>
Expand All @@ -22,7 +23,19 @@
</div>
<div>
<label>Min Validator</label>
<input type="text" data-allow="onlyAlpha" name="name4" min="5" required>
<input type="text" data-allow="onlyAlpha" name="name4" min="5" max="10" required>
</div>
<div>
<label>Email Validator</label>
<input type="email" name="email" required>
</div>
<div>
<label>Password</label>
<input type="password" name="name5" data-check="repassword" id="password" required>
</div>
<div>
<label>Re-Password</label>
<input type="password" name="name6" data-parent="password" id="repassword" required>
</div>
<div>
<label></label>
Expand All @@ -42,7 +55,8 @@
// required: 'This field is required 123 !',
// min: '<br><span style="color: red;">This field is less then the limit [INDEX]</span>',
// max: 'This field is exceeds the limit of [INDEX]',
// password: 'Password doesn\'t match !'
// password: 'Password doesn\'t match !',
// email: 'Invalid Email found !'
// }
// });

Expand All @@ -53,9 +67,9 @@
required: 'This field is required !',
min: 'This field is less then the limit [INDEX]',
max: 'This field is exceeds the limit of [INDEX]',
password: 'Password doesn\'t match !'
password: 'Password doesn\'t match !',
email: 'Invalid Email found !'
}

});

</script>
Expand Down
103 changes: 61 additions & 42 deletions src/js/validatorNew.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class jsValidator {

option.push({'errorElem': errorList});

jsLogger.out('Error List', option);
// jsLogger.out('Error List', option);

// To Update global Validation Status.
// If, Input elements have no errors.
Expand All @@ -148,7 +148,7 @@ class jsValidator {
// Initiate empty array for keep list of errors.
let log = [];
if (formElem === null || typeof formElem === 'undefined') return false;
jsLogger.out('Elem Loop Filter', formElem);
// jsLogger.out('Elem Loop Filter', formElem);
// Looping elements.
for (let i in formElem) {
if (formElem[i]) {
Expand All @@ -173,7 +173,7 @@ class jsValidator {
// Apply filter for Email elements.
if (activeElem.type == 'email') this.jsFilter.constructor.email(activeElem);
// Apply filter for Numeric elements.
if (activeElem.min || activeElem.max) this.jsFilter.limit(activeElem);
// if (activeElem.min || activeElem.max) this.jsFilter.limit(activeElem);
// Apply filter with string, alphaNumeric and pregMatch.
if (activeElem.getAttribute('data-allow')) this.jsFilter.string(activeElem);
}
Expand All @@ -188,47 +188,55 @@ class jsValidator {
}
// To Check the Value is less than min or not.
if (activeElem.min) {
if (!jsRuleSet.constructor.min(activeElem)) {
log.push({
'el': activeElem,
'type': 'min',
'id': activeElem.name
});
validElem = false;
if (jsRuleSet.constructor.isSet(activeElem)) {
if (!jsRuleSet.constructor.min(activeElem)) {
log.push({
'el': activeElem,
'type': 'min',
'id': activeElem.name
});
validElem = false;
}
}
}
// To Check the Value is grater than max or not.
if (activeElem.max) {
if (!jsRuleSet.constructor.max(activeElem)) {
log.push({
'el': activeElem,
'type': 'max',
'id': activeElem.name
});
validElem = false;
if (jsRuleSet.constructor.isSet(activeElem)) {
if (!jsRuleSet.constructor.max(activeElem)) {
log.push({
'el': activeElem,
'type': 'max',
'id': activeElem.name
});
validElem = false;
}
}
}
// To Check the Entered E-mail is Valid or Not.
if (activeElem.type == "email") {
if (!jsRuleSet.constructor.email(activeElem)) {
log.push({
'el': activeElem,
'type': 'email',
'id': activeElem.name
});
validElem = false;
if (jsRuleSet.constructor.isSet(activeElem)) {
if (!jsRuleSet.constructor.email(activeElem)) {
log.push({
'el': activeElem,
'type': 'email',
'id': activeElem.name
});
validElem = false;
}
}
}
// To Compare the Password is Same or Not with Re-Password.
// TODO: Implement Simplified Comparison.
if (activeElem.type == "password") {
if (!jsRuleSet.constructor.compare(activeElem)) {
log.push({
'el': activeElem,
'type': 'password',
'id': activeElem.name
});
validElem = false;
if (jsRuleSet.constructor.isSet(activeElem)) {
if (!jsRuleSet.constructor.compare(activeElem)) {
log.push({
'el': activeElem,
'type': 'password',
'id': activeElem.name
});
validElem = false;
}
}
}
// If valid, then reset validation message.
Expand Down Expand Up @@ -571,6 +579,7 @@ class jsRuleSets {
static isSet(elem) {
// If field is not required, then return "true".
if (false === elem.required) return true;

let status = true;
let value = elem.value;
//TODO: Implement suitable solution for this.
Expand Down Expand Up @@ -598,25 +607,28 @@ class jsRuleSets {
let value = elem.value;
let max = elem.max;
//TODO: Implement suitable solution for this.
if (value > max) status = false;
if (value.length > max) status = false;
return status;
}

// To Check Element Email is Valid or Not.
static email(elem) {
// If field is not required, then return "true".
if (false === elem.required) return true;
let status = true;

let status = false;
let email = elem.value;
// To Validate Email.
// Convert to Native String Format.
email = email.toString();
// To Check it as String or Not.
if (!email) status = false;
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
// Valid Email.
status = true;
}

if (!email) status = false;

return status;
}

Expand All @@ -631,18 +643,26 @@ class jsRuleSets {

// To Compare two Elements Values.
static compare(elem1) {
let status = false;

// If field is not required, then return "true".
if (false === elem1.required) return true;
if (false === elem1.required) status = true;

let elem2_id = elem1.getAttribute('data-check');

if (typeof elem2_id == 'undefined' || elem2_id == null) status = false;

if (elem2_id === null) elem2_id = elem1.getAttribute('data-parent');
elem2_id = elem2_id.toString();
if (elem2_id === null) {
status = false;
} else {
elem2_id = elem2_id.toString();

let elem2 = document.getElementById(elem2_id);
let elem2 = document.getElementById(elem2_id);

if (elem1.value === elem2.value) status = true;
}

let status = true;
if (elem1.value !== elem2.value) status = false;
jsLogger.out('Compare Status', status);
return status;
}
Expand Down Expand Up @@ -811,7 +831,7 @@ let validationResponse = {
elementDefaultResponse = this.template(activeElem, errorType);

let spanTag = document.getElementById(activeElem.id);
jsLogger.out('Element Hit', errorType);
// jsLogger.out('Element Hit', errorType);
// Create new response Message SPAN.
if (typeof(spanTag) === 'undefined' || spanTag === null) {
jsLogger.out('Element Found', false);
Expand All @@ -821,9 +841,8 @@ let validationResponse = {
} else {
// Re-use Existing response Message SPAN.
spanTag.innerHTML = elementDefaultResponse;
jsLogger.out('Element Found', true);
}
jsLogger.out('Error Elem', activeElem.el);
// jsLogger.out('Error Elem', activeElem.el);
// Append HTML response to the Element.
activeElem.el.parentNode.insertBefore(spanTag, activeElem.el.nextSibling);
}
Expand Down
Loading