Skip to content

Commit 6de0737

Browse files
Merge pull request #5 from global-source/newValidationElements
Major Update with Elements and Security
2 parents 7ac44ba + 6a3c00d commit 6de0737

File tree

3 files changed

+219
-153
lines changed

3 files changed

+219
-153
lines changed

src/demo/index.html

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,46 @@
1010
<div>
1111
<div>
1212
<label for="name">Name :</label>
13-
<input type="text" data-allow="onlyAlpha" id="check" data-allowSpecial="_." value="" name="name" required>
13+
<input type="text" data-allow="onlyAlpha" pattern="^[a-zA-C]+$" id="check" data-allowSpecial="_."
14+
value=""
15+
name="name"
16+
required>
1417
</div>
1518
<div>
1619
<label for="name">String :</label>
1720
<input type="text" data-allow="string" value="" name="name" required>
1821
</div>
1922
<div>
2023
<label for="name">String + [0-5]:</label>
21-
<input type="text" data-allow="^[a-zA-Z0-5]+$" value="" name="name" required>
24+
<input type="text" data-allow="string " pattern="^[a-zA-Z0-5]+$" value="" name="name" required>
2225
</div>
2326
<div>
2427
<label for="name">Number in [3-7] :</label>
2528
<input type="number" name="name" id="number" min="3" max="7" required>
2629
</div>
2730
<div>
2831
<label for="name">Password :</label>
29-
<input type="password" data-check="rePassword" id="password" data-allow="^[a-zA-Z0-5]+$" value=""
32+
<input type="password" data-check="rePassword" id="password" pattern="^[a-zA-Z0-5]+$"
33+
data-allow="password"
34+
value=""
3035
name="name"
3136
required>
3237
</div>
3338
<div>
3439
<label for="name">Re-Password :</label>
35-
<input type="password" name="name" data-parent="password" id="rePassword" data-allow="^[a-zA-Z0-5]+$"
40+
<input type="password" name="name" data-parent="password" id="rePassword" pattern="^[a-zA-Z0-5]+$"
41+
data-allow="password"
3642
required>
3743
</div>
3844
<div>
3945
<select required>
46+
<option value=""></option>
4047
<option value="123">123</option>
4148
</select>
4249
</div>
4350
<div>
4451
<select required>
52+
<option value=""></option>
4553
<option value="456">456</option>
4654
</select>
4755
</div>
@@ -54,22 +62,22 @@
5462
</div>
5563

5664
<!--<script src="./../js/formValidator.min.js"></script>-->
57-
<!--<script src="./../js/validatorNew.js"></script>-->
58-
<script src="../js/validatorNew.es6.js"></script>
65+
<script src="./../js/validatorNew.js"></script>
66+
<!--<script src="../js/validatorNew.es6.js"></script>-->
5967
<script>
6068

6169
(function () {
6270

63-
// var val = jsValidator.init({
64-
// form: 'demoValidation',
65-
// onlyFilter: false
66-
// });
67-
68-
var val = new jsValidator().init({
71+
var val = jsValidator.init({
6972
form: 'demoValidation',
7073
onlyFilter: false
7174
});
7275

76+
// var val = new jsValidator().init({
77+
// form: 'demoValidation',
78+
// onlyFilter: false
79+
// });
80+
7381
}());
7482
</script>
7583
</body>

src/js/validatorNew.es6.js

Lines changed: 100 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ class jsValidator {
3939
this.jsFormError = false;
4040
// Overall error list.
4141
this.formErrorList = {};
42-
// Overall validation status.
43-
this.validationPass = false;
4442
// Common Logger Instance.
4543
this.jsFilter = false;
4644
this.jsRuleSet = false;
@@ -175,12 +173,8 @@ class jsValidator {
175173
// Single step instance validator for Ajax form submissions.
176174
validate() {
177175
// Initiate form Check.
178-
this.check();
179-
// Return validation status.
180-
return this.validationPass;
176+
return this.check();
181177
}
182-
183-
//};
184178
}
185179

186180
/*
@@ -213,6 +207,10 @@ class jsFilter {
213207
case 'string':
214208
element.addEventListener("keypress", current.constructor.isAlphaNumeric, false);
215209
break;
210+
// Allow only alpha Numeric [a-zA-Z0-9] not special characters.
211+
case 'password':
212+
element.addEventListener("keypress", current.constructor.isValidPassword, false);
213+
break;
216214
// Allow based on the pattern given.
217215
default:
218216
element.addEventListener("keypress", current.constructor.isPatternValid, false);
@@ -238,7 +236,7 @@ class jsFilter {
238236
static isInLimit(event) {
239237
var value = event.target.value;
240238
// To check is this action is from "windows" action or not.
241-
if (true === isWindowAction(event)) return true;
239+
if (true === helper.isWindowAction(event)) return true;
242240

243241
// Getting object from element.
244242
var min = event.target.min;
@@ -265,60 +263,52 @@ class jsFilter {
265263
// Only allow alpha([a-zA-Z]).
266264
static isAlpha(event) {
267265
// To check is this action is from "windows" action or not.
268-
if (true === isWindowAction(event)) return true;
269-
// Getting special characters list.
270-
var allow_special = event.target.getAttribute('data-allowSpecial');
271-
// Set default values for special characters.
272-
if (!allow_special && allow_special == null) allow_special = '';
273-
// Format to string.
274-
allow_special = allow_special.toString();
275-
// Validate with special formed pattern.
276-
var regex = new RegExp('^[a-zA-Z' + allow_special + ']+$');
277-
// Validation with Code.
278-
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
279-
jsLogger.out('Alpha', regex.test(key));
266+
if (true === helper.isWindowAction(event)) return true;
267+
var status = helper.patternValid(event, 'a-zA-Z');
268+
console.log(status);
280269
// Return status of the Action.
281-
if (false === regex.test(key)) event.preventDefault();
270+
if (false === status) event.preventDefault();
282271
};
283272

284273
// Only allow alpha([a-zA-Z0-9]).
285274
static isAlphaNumeric(event) {
286275
// To check is this action is from "windows" action or not.
287-
if (true === isWindowAction(event)) return true;
288-
// Getting special characters list.
289-
var allow_special = event.target.getAttribute('data-allowSpecial');
290-
// Set default values for special characters.
291-
if (!allow_special && allow_special == null) allow_special = '';
292-
// Format to string.
293-
allow_special = allow_special.toString();
294-
// Validate with special formed pattern.
295-
var regex = new RegExp('^[a-zA-Z0-9' + allow_special + ']+$');
296-
// Validation with Code.
297-
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
298-
jsLogger.out('Alpha', regex.test(key));
276+
if (true === helper.isWindowAction(event)) return true;
277+
// Managing the Pattern.
278+
var status = helper.patternValid(event, 'a-zA-Z0-9');
279+
// Return status of the Action.
280+
if (false === status) event.preventDefault();
281+
};
282+
283+
static isValidPassword(event) {
284+
// Prevent using "space".
285+
var charCode = (event.which) ? event.which : event.keyCode;
286+
if (charCode === 32) {
287+
event.preventDefault();
288+
return false;
289+
}
290+
// To check is this action is from "windows" action or not.
291+
if (true === helper.isWindowAction(event)) return true;
292+
// Managing the Pattern.
293+
var status = helper.patternValid(event, 'a-zA-Z0-9');
299294
// Return status of the Action.
300-
if (false === regex.test(key)) event.preventDefault();
295+
if (false === status) event.preventDefault();
301296
};
302297

303298
// Only allow by pattern(ex. ^[a-zA-Z0-3@#$!_.]+$).
304299
static isPatternValid(event) {
305300
// To check is this action is from "windows" action or not.
306-
if (true === isWindowAction(event)) return true;
307-
// Getting special characters list.
308-
var pattern = event.target.getAttribute('data-allow');
309-
// Validate with special formed pattern.
310-
var regex = new RegExp(pattern);
311-
// Validation with Code.
312-
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
313-
jsLogger.out('Alpha', regex.test(key));
301+
if (true === helper.isWindowAction(event)) return true;
302+
// Managing the Pattern.
303+
var status = helper.patternValid(event, 'a-zA-Z0-9');
314304
// Return status of the Action.
315-
if (false === regex.test(key)) event.preventDefault();
305+
if (false === status) event.preventDefault();
316306
};
317307

318308
// Check is numeric or not.
319309
static isNumberKey(event) {
320310
// To check is this action is from "windows" action or not.
321-
if (true === isWindowAction(event)) return true;
311+
if (true === helper.isWindowAction(event)) return true;
322312
// Validation with Code.
323313
var charCode = (event.which) ? event.which : event.keyCode;
324314
if (charCode === 46 || charCode > 31 && (charCode < 48 || charCode > 57)) {
@@ -466,23 +456,29 @@ class jsRuleSets {
466456
// To Check, whether the element have value or not.
467457
static isSet(elem) {
468458
var status = true;
469-
// Check length and empty or not.
470-
if (elem.length === 0 || elem.value === '') status = false;
459+
var value = elem.value;
460+
//TODO: Implement suitable solution for this.
461+
if (value.length == 0 || value == '' || value == ' ') status = false;
471462
return status;
472463
};
473464

474465
// To Check Element with Min Condition.
475466
static min(elem) {
476467
var status = true;
468+
var value = elem.value;
469+
var min = elem.min;
477470
//TODO: Implement suitable solution for this.
478-
//if (elem.length < elem.min && elem.length !== 0) status = false;
471+
if (value < min) status = false;
479472
return status;
480473
};
481474

482475
// To Check Element with Max Condition.
483476
static max(elem) {
484477
var status = true;
485-
if (elem.value > elem.max) status = false;
478+
var value = elem.value;
479+
var max = elem.max;
480+
//TODO: Implement suitable solution for this.
481+
if (value > max) status = false;
486482
return status;
487483
};
488484

@@ -514,11 +510,13 @@ class jsRuleSets {
514510
var elem2_id = elem1.getAttribute('data-check');
515511

516512
if (elem2_id == null) elem2_id = elem1.getAttribute('data-parent');
517-
518513
elem2_id = elem2_id.toString();
514+
519515
var elem2 = document.getElementById(elem2_id);
516+
520517
var status = true;
521-
if ((elem1.value !== elem2.value) && elem1.length !== elem2.length) status = false;
518+
if (elem1.value !== elem2.value) status = false;
519+
jsLogger.out('Compare Status', status);
522520
return status;
523521
}
524522
}
@@ -581,24 +579,56 @@ var jsLogger = {
581579
}
582580
};
583581

584-
/*
585-
* To check the keyboard action is window action or not.
586-
*/
587-
function isWindowAction(event) {
588-
// Getting the event to be triggered.
589-
var theEvent = event || window.event;
590-
// Getting the type of event or code.
591-
var key = theEvent.keyCode || theEvent.which;
592-
593-
// Check with list of code and ignore holding.
594-
// Tab, Space, Home, End, Up, Down, Left, Right...
595-
if (key === 9 || key === 32 || key === 13 || key === 8 || (key >= 35 && key <= 40)) { //TAB was pressed
596-
return true;
582+
var helper = {
583+
/*
584+
* To check the keyboard action is window action or not.
585+
*/
586+
isWindowAction: function (event) {
587+
588+
// Getting the event to be triggered.
589+
var theEvent = event || window.event;
590+
// Getting the type of event or code.
591+
var key = theEvent.shiftKey || theEvent.which;
592+
// Check with list of code and ignore holding.
593+
// Tab, Space, Home, End, Up, Down, Left, Right...
594+
if (key === 9 || key === 0 || key === 8 || key === 32 || key === 13 || key === 8 || (key >= 35 && key <= 40)) {
595+
return true;
596+
}
597+
598+
// If not in list then check return with corresponding data.
599+
key = String.fromCharCode(key);
600+
// Return also if length is 0.
601+
if (key.length == 0) return true;
602+
603+
// Finally return "false" for general keys.
604+
return false;
605+
},
606+
getDefaultPattern: function (event, originalPattern) {
607+
if (typeof originalPattern == 'undefined') var originalPattern = false;
608+
// Getting special characters list.
609+
var allow_special = event.target.getAttribute('data-allowSpecial');
610+
var pattern = event.target.pattern;
611+
console.log(pattern.length);
612+
var defaultPattern;
613+
// Set default values for special characters.
614+
if (!allow_special && allow_special == null) allow_special = '';
615+
// Format to string.
616+
allow_special = allow_special.toString();
617+
618+
if (pattern != '' && pattern.length > 0 && pattern != null) {
619+
defaultPattern = pattern;
620+
} else {
621+
defaultPattern = '^[' + originalPattern + allow_special + ']+$';
622+
}
623+
return defaultPattern;
624+
},
625+
patternValid: function (event, pattern) {
626+
// Managing the Pattern.
627+
var defaultPattern = this.getDefaultPattern(event, pattern);
628+
// Validate with special formed pattern.
629+
var regex = new RegExp(defaultPattern);
630+
// Validation with Code.
631+
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
632+
return regex.test(key);
597633
}
598-
// If not in list then check return with corresponding data.
599-
key = String.fromCharCode(key);
600-
// Return also if length is 0.
601-
if (key.length == 0) return true;
602-
// Finally return "false" for general keys.
603-
return false;
604-
}
634+
};

0 commit comments

Comments
 (0)