-
Notifications
You must be signed in to change notification settings - Fork 3
/
Dynamo_Client_Store_web_store.war_shared_js_ccLibs_cc-ko-validate-rules.js.html
executable file
·1365 lines (1226 loc) · 48.8 KB
/
Dynamo_Client_Store_web_store.war_shared_js_ccLibs_cc-ko-validate-rules.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: Dynamo/Client/Store/web/store.war/shared/js/ccLibs/cc-ko-validate-rules.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: Dynamo/Client/Store/web/store.war/shared/js/ccLibs/cc-ko-validate-rules.js</h1>
<section>
<article>
<pre class="prettyprint source"><code>/**
* @fileoverview Cloud Commerce custom rules for Ko Validate.
*
*
*/
/*global $ */
define('ccKoValidateRules',
['knockout', 'jqueryui', 'notifier','ccLogger', 'koValidate', 'ccPasswordValidator'],
function (ko, jQueryUI, notifier, log, koValidate, CCPasswordValidator) {
"use strict";
/**
* Contains custom knockout validators for Cloud Commerce.
* @namespace koValidation
*/
/*
* NOTE NOTE NOTE NOTE
*
* All validators in this file need to have an entry in the
* validation-translator.js file. validation-translator.js is run to ensure
* the validation messages are returned in the proper locale.
*/
/**
* Adds a credit card number validation rule to koValidate.
* @public
* @class Credit card validation.
* @requires koValidate
* @name creditcard
* @memberof koValidation
* @example
* self.cardNumber.extend({ creditcard:{ iin: iinObservable, length: lengthObservable } });
* @example
* self.cardNumber.extend({ creditcard:{ iin: iinObservable, length: lengthObservable, message errorMsg } }
*/
ko.validation.rules['creditcard'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.creditcard
* @param {string} val The credit card number to be validated
* @param {Object} params Object containing the Card IIN and Card Number length observables.
* The value of these observables should be updated appropriately for the card type
* IIN value can contain a single number, a range of numbers e.g. '[3-6]'
* or a list of numbers separated by the '|' character (i.e. an OR symbol)
* Length value can contain a single number, a comma-seperated range of numbers
* e.g. '3,6' or a list of numbers separated by the '|' character.
* @returns {boolean} Validation success - true or false.
*/
validator: function (val, params) {
var self = this;
if(!params) {
return false;
}
if(!params.iin || !params.length
|| !ko.isObservable(params.iin)
|| !ko.isObservable(params.length)) {
return false;
}
var cardIIN = params.iin();
var cardLength = params.length();
if(!cardIIN || !cardLength
|| cardIIN === '' || cardLength === '' ) {
return false;
}
if(!val || val === '') {
if(cardLength === '0') {
// No length requirement
// Not up to this rule to enforce
// whether val is required or not
return true;
} else {
return false;
}
}
var cardNumber = val;
var iinPattern = new RegExp('^('+cardIIN+')');
if(!cardNumber.match(iinPattern)) {
// Failed IIN pattern match
return false;
}
var cardLengthArray = cardLength.split('|');
var lengthPatternStr = '';
for(var i=0; i < cardLengthArray.length; i++) {
if(i!==0) { lengthPatternStr += '|'; }
lengthPatternStr += '^[0-9]{' + cardLengthArray[i] + '}$';
}
var lengthPattern = new RegExp(lengthPatternStr);
if(!cardNumber.match(lengthPattern)) {
// Failed length match
return false;
}
var luhnMod10 = function (cardNumber) {
var total = 0;
var length = cardNumber.length;
for(var i=1; i<=length; i++) {
var digit = parseInt(cardNumber[(length - i)], 10);
if(i%2 == 0) {
digit *= 2;
if(digit>9) {
// as digit max is 18
// digit%10 + 1 == -9
digit -= 9;
}
}
total += digit;
}
return (total%10 == 0);
};
if(!luhnMod10(cardNumber)) {
//Failed Mod 10 check
return false;
}
// All good
return true;
},
// Default message
message: 'Card Number is invalid.'
};
/**
* Adds a cvv (card verification value) validation rule to koValidate.
* @public
* @class Card verification value (CVV) validation.
* @memberof koValidation
* @name cvv
* @requires koValidate
* @example
* self.cvv.extend({ cvv: requiredCVVLengthObs });
* @example
* self.cvv.extend({ cvv:{ params: requiredCVVLengthObs, message errorMsg } }
*/
ko.validation.rules['cvv'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.cvv
* @param {string} val The cvv value to be validated. Should be a digit string, eg '456' or '4567'.
* @param {observable<integer>} requiredCVVLength The object containing the required cvv length value.
* @returns {boolean} Validation success - true or false.
*/
validator: function (val, requiredCVVLength) {
var self = this;
if(!requiredCVVLength) {
return false;
}
var requiredLength = 0;
if(ko.isObservable(requiredCVVLength)) {
requiredLength = requiredCVVLength();
} else {
requiredLength = requiredCVVLength;
}
requiredLength = parseInt(requiredLength, 10);
if(!val || val === '') {
if(requiredLength === 0) {
// No length requirement
// Not up to this rule to enforce
// whether val is required or not
return true;
} else {
return false;
}
}
var cvvPattern = new RegExp('^[0-9]{'+requiredLength+'}$');
if(!val.match(cvvPattern)) {
// Failed pattern match
return false;
}
// All good
return true;
},
// Default message
message: 'Card CVV is invalid.'
};
/**
* Rather than perform the combined month & year
* date validation twice (once for each field)
* the outcome is saved to the observables below
* by the month validation rules and then simply
* accessed by the year validation rules.
*/
var _startDateValid = ko.observable(true);
var _endDateValid = ko.observable(true);
/**
* Adds a start month validation rule to koValidate.
* The 'startmonth' and 'startyear' combination must be equal to or
* before the current month and year.
* @public
* @class Start month validation.
* @memberof koValidation
* @requires koValidate
* @name startmonth
* @example
* self.startMonth.extend({ startmonth: startYearObs });
* @example
* self.startMonth.extend({ startmonth:{ params: startYearObs, message errorMsg } }
*/
ko.validation.rules['startmonth'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.startmonth
* @param {string} val The month value to be validated. Should be a 2 digit string, eg '06'.
* @param {observable<string>} startYear The observable containing the start year value.
* @returns {boolean} Validation success - true or false.
*/
validator: function (val, startYear) {
var self = this;
if(!startYear || !ko.isObservable(startYear)) {
return false;
}
if(!val || val === '') {
// Not up to this rule to enforce
// whether val is required or not
return true;
}
var startMonth = val;
var monthPattern = new RegExp('^((0[1-9])|(1[0-2]))$');
if(!startMonth.match(monthPattern)) {
// Failed month pattern match
return false;
}
if(!startYear()) {
// no start year value
// so month is good based
// on what is known
return true;
}
var currentDate = new Date();
var currentYear = currentDate.getUTCFullYear();
var currentMonth = currentDate.getUTCMonth(); //zero indexed!
if(parseInt(startYear(), 10) === currentYear) {
// must specify radix to avoid leading zero
// month values being treated as octal
if(parseInt(startMonth,10) > (++currentMonth)) {
// Month not reached yet.
_startDateValid(false);
return false;
}
}
// All good
_startDateValid(true);
return true;
},
// Default message
message: 'Start Month is invalid.'
};
/**
* Adds a start year validation rule to koValidate.
* The 'startmonth' and 'startyear' combination must be equal to or
* before the current month and year.
* @public
* @class Start year validation.
* @memberof koValidation
* @requires koValidate
* @name startyear
* @example
* self.startYear.extend({ startyear:{} });
* @example
* self.startYear.extend({ startmonth:{ message errorMsg } }
*/
ko.validation.rules['startyear'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.startyear
* @param {string} val The year value to be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function (val) {
if(!val || val === '') {
// Not up to this rule to enforce
// whether val is required or not
return true;
}
// return result of combined month & year
// date validation as determined by month
// validatior rule.
return _startDateValid();
} ,
// Default message
message: 'Start Year is invalid.'
};
/**
* Adds a validation rule for import file extension.
* Import files can have only csv or xls type of extensions.
* @public
* @class Import filename extension validation.
* @memberof koValidation
* @requires koValidate
* @name importFileName
* @example
* this.importFilename.extend({
* 'importFileName': { params: true,
* message: CCi18n.t('ns.common:resources.invalidFileExtensionMessageText'),
* 'notify': { id: 'import-file-validation'}
* }
* });
*/
ko.validation.rules['importFileName'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.importFileName
* @param {string} val Name of the file to be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function (val) {
var exts = ['.csv'];
return (new RegExp('(' + exts.join('|').replace(/\./g, '\\.') + ')$')).test(val);
},
message: 'invalid file extension.'
};
/**
* Adds a validation rule for media file extension.
* Media files can have only .zip type of extensions.
* @public
* @class Media file extension validation.
* @memberof koValidation
* @requires koValidate
* @name mediaZipFileName
* @example
* this.uploadFilename.extend({
* 'required': {
* params: true,
* message: CCi18n.t('ns.catalog:resources.pleaseSelectAFileText'),
* 'notify': { id: 'media-file-required-validation' }
* },
* 'mediaZipFileName': {
* params: true,
* message: CCi18n.t('ns.catalog:resources.pleaseSelectAValidFileTypeText'),
* 'notify': { id: 'media-file-type-validation' }
* },
* });
*/
ko.validation.rules['mediaZipFileName'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.mediaZipFileName
* @param {string} val Name of the file to be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function (val) {
var exts = ['.zip'];
return (new RegExp('(' + exts.join('|').replace(/\./g, '\\.') + ')$')).test(val);
}
};
/**
* Adds an end month validation rule to koValidate.
* The 'endmonth' and 'endyear' combination must be equal to or
* after the current month and year.
* @public
* @class End month validation.
* @memberof koValidation
* @requires koValidate
* @name endmonth
* @example
* self.endMonth.extend({ endmonth: endYearObs });
* @example
* self.endMonth.extend({ endmonth:{ params: endYearObs, message errorMsg } }
*/
ko.validation.rules['endmonth'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.endmonth
* @param {string} val The month value to be validated. Should be a 2 digit string, eg '06'.
* @param {observable<string>|string} endYear The observable containing the end year value.
* @returns {boolean} Validation success - true or false.
*/
validator: function (val, endYear) {
var self = this;
if(!val || val === '') {
// Not up to this rule to enforce
// whether val is required or not
return true;
}
var endMonth = val;
var monthPattern = new RegExp('^((0[1-9])|(1[0-2]))$');
if(!endMonth.match(monthPattern)) {
// Failed month pattern match
return false;
}
if(!endYear) {
// no end year value
// so month is good based
// on what is known
return true;
}
var currentDate = new Date();
var currentYear = currentDate.getUTCFullYear();
var currentMonth = currentDate.getUTCMonth(); //zero indexed!
if(parseInt(endYear, 10) === currentYear) {
// must specify radix to avoid leading zero
// month values being treated as octal
if(parseInt(endMonth,10) < (++currentMonth)) {
// month has passed
_endDateValid(false);
return false;
}
}
// All good
_endDateValid(true);
return true;
},
// Default message
message: 'End Month is invalid.'
};
/**
* Adds a end year validation rule to koValidate.
* The 'endmonth' and 'endyear' combination must be equal to or
* after the current month and year.
* @public
* @class End year validation.
* @memberof koValidation
* @requires koValidate
* @name endyear
* @example
* self.endYear.extend({ endyear:{} });
* @example
* self.endYear.extend({ endmonth:{ message errorMsg } }
*/
ko.validation.rules['endyear'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.endyear
* @param {string} val The year value to be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function (val) {
if(!val || val === '') {
// Not up to this rule to enforce
// whether val is required or not
return true;
}
// return result of combined month & year
// date validation as determined by month
// validatior rule.
return _endDateValid();
} ,
// Default message
message: 'End Year is invalid.'
};
/**
* Adds a pattern validation rule to koValidate, which uses an observable
* to allow the pattern to be changed dynamically.
* NB: the standard pattern rule doesn't work with observables for the pattern
* @public
* @class Pattern validation based on the value of an observable.
* @memberof koValidation
* @requires koValidate
* @name observablePattern
* @example
* self.myObservable.extend({ observablePattern: obsPattern });
* @example
* self.myObservable.extend({ observablePattern:{ params: obsPattern, message errorMsg } }
*/
ko.validation.rules['observablePattern'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.observablePattern
* @param {string} val The value to be validated. Should be a string.
* @param {observable<string>|string} obsPattern The observable or string containing the pattern value.
* @returns {boolean} Validation success - true or false.
*/
validator: function (val, obsPattern) {
var self = this;
if(!obsPattern) {
// rule not set up correctly
return false;
}
var patternStr = '';
if(ko.isObservable(obsPattern)) {
patternStr = obsPattern();
} else {
patternStr = obsPattern;
}
if(!patternStr || patternStr === '') {
// rule not set up correctly
return false;
}
if(!val || val === '') {
// Not up to this rule to enforce
// whether val is required or not
return true;
}
var regExpPattern = new RegExp(patternStr);
if(!val.match(regExpPattern)) {
// Failed pattern match
return false;
}
// All good
return true;
},
// Default message
message: 'Observable Pattern Match Failed.'
};
/**
* Adds a validation rule for alphanumeric field values.
* @public
* @class Alphanumeric value validation.
* @memberof koValidation
* @requires koValidate
* @name alphaNumeric
* @example
* viewModel.label.extend({required: true, alphaNumeric: true});
*/
ko.validation.rules['alphaNumeric'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.alphaNumeric
* @param {string} val The value to be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function(value) {
return !value || value.match(/^[A-Za-z0-9(\s)]+$/);
},
message: "Field may only contain alphanumeric characters"
};
/**
* Adds a validation rule for boolean field values.
* @public
* @class Boolean value validation.
* @memberof koValidation
* @requires koValidate
* @name bool
* @example
* myBooleanProperty.extend({bool: true});
*/
ko.validation.rules['bool'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.bool
* @param {string} val The value to be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function(value) {
return value === true || !value;
},
message: "Value must be true or false"
};
/**
* Adds a validation rule for alphanumeric field value assuming there will
* potentially be separator characters.
* <p>
* Separator characters can be dash "-", space or underscore "_".
* @public
* @class Alphanumberic validation, allowing separator characters.
* @memberof koValidation
* @requires koValidate
* @name alphaNumericWithSeperators
* @example
* viewModel.label.extend({required: true, alphaNumericWithSeperators: true});
*/
ko.validation.rules['alphaNumericWithSeperators'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.alphaNumericWithSeperators
* @param {string} val The value to be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function(value) {
return !value || value.match(/^[A-Za-z0-9_\-\s]+$/);
},
message: "Field may only contain alphanumeric characters and -, spaces, or _"
};
/**
* Adds a validation rule for alphanumeric field value with NO spaces and
* assuming there will potentially be separator characters.
* <p>
* Separator characters can be dash "-" or underscore "_".
* @public
* @class Alphanumberic validation, allowing separator characters, excluding space.
* @memberof koValidation
* @requires koValidate
* @name alphaNumericNoSpacesWithSeperators
* @example
* viewModel.label.extend({required: true, alphaNumericNoSpacesWithSeperators: true});
*/
ko.validation.rules['alphaNumericNoSpacesWithSeperators'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.alphaNumericNoSpacesWithSeperators
* @param {string} val The value to be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function(value) {
return !value || value.match(/^[A-Za-z0-9_\-]+$/);
},
message: "Field may only contain alphanumeric characters and - or _"
};
/**
* Adds a validation rule for a field value with NO forward or
* back slashes.
* @public
* @class No forward or back slash validation.
* @memberof koValidation
* @requires koValidate
* @name restrictSlashCharacters
* @example
* viewModel.label.extend({required: true, restrictSlashCharacters: true});
*/
ko.validation.rules['restrictSlashCharacters'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.restrictSlashCharacters
* @param {string} value The value to be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function(value) {
return !value || !value.match(/[\/\\]+/);
},
message: "Field may not contain forwardslash or backslash."
};
/**
* Validation rule to ensure a string is alpha numeric (A-Z,0-9,-,_) with
* no white space.
* @public
* @class Alphanumeric with no spaces validation.
* @memberof koValidation
* @requires koValidate
* @name alphaNumericNoSpaces
* @example
* myObservable.extend({ alphaNumericNoSpaces: true });
*/
ko.validation.rules['alphaNumericNoSpaces'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.alphaNumericNoSpaces
* @param {string} value The value to be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function(value) {
return !value || value.match(/^[A-Za-z0-9]+$/);
},
message: "Field may only contain alphanumeric characters and no spaces"
};
/**
* Adds specific cart picker validation to koValidate.
* For each cart picker item, if the product ID is populated, then the quantity
* must be a valid integer between 1 and 99.
* @private
* @class Cart picker validation.
* @memberof koValidation
* @requires koValidate
* @name cartPicker
* @example
* self.myObservable.extend({ cartPicker:});
* @example
* self.myObservable.extend({ cartPicker:{ message errorMsg } }
*/
ko.validation.rules['cartPicker'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.cartPicker
* @param {CartPickerItemViewModel[]} val The cart picker items to be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function (val) {
if (! val){
return true;
}
var i,
arraySize = val.length;
for(i = 0; i < arraySize; i++ ){
var intQuantity = parseInt(val[i].quantity, 10);
if ((val[i].productId != "") ){
if ((isNaN(intQuantity) ) || (intQuantity <= 0) || (intQuantity >= 100)) {
return false;
}
} else {
if (val[i].quantity != ""){
return false;
}
}
}
// check for duplicated in the array
var filledRows = 0, out=[],
obj={};
for (i=0;i<arraySize;i++) {
if (val[i].productId != "") {
obj[val[i].productId]=0;
filledRows = filledRows + 1;
}
}
for (i in obj) {
out.push(i);
}
if (filledRows > out.length){
return false;
}
return true;
},
message: "Please ensure each item has a quantity specified"
};
/**
* Adds cart picker unique product validation to koValidate.
* Ensures there are no duplicate products in the cart.
* @private
* @class Cart picker unique product validation.
* @memberof koValidation
* @requires koValidate
* @name cartPickerUniqueProducts
* @example
* this.editItem().skuIds.extend({
* cartPicker: {
* message: CCi18n.t('ns.design:resources.cartPickerNoQuantity')
* },
* cartPickerUniqueProducts : {
* message: CCi18n.t('ns.design:resources.cartPickerDuplicates')
* }
* });
*/
ko.validation.rules['cartPickerUniqueProducts'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.cartPickerUniqueProducts
* @param {CartPickerItemViewModel[]} val The cart picker items to be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function (val) {
if (! val){
return true;
}
var i,
arraySize = val.length,
filledRows = 0,
out=[],
obj={};
// check for duplicated in the array
for (i=0; i<arraySize; i++) {
if (val[i].productId != "") {
obj[val[i].productId]=0;
filledRows = filledRows + 1;
}
}
for (i in obj) {
out.push(i);
}
if (filledRows > out.length){
return false;
}
return true;
},
message: "Please ensure there are no Duplicate product IDs"
};
/**
* Adds a validation rule for price fields.
* @public
* @class Price field validation.
* @memberof koValidation
* @requires koValidate
* @name price
* @example
* this.price.extend({price: true});
*/
ko.validation.rules['price'] = {
// TODO THIS NEEDS TO BE UPDATED to follow the proper precision for
// localized currency
/**
* Validator function.
* @function validator
* @memberof koValidation.price
* @param {string} value Value to be validated.
* @param {boolean} validate Flag indicating if the price should be validated.
* @returns {boolean} Validation success - true or false.
*/
validator: function (value, validate) {
if (!validate) {
return true;
}
return value === null || typeof value === 'undefined' ||
value === "" || /^\d*\.?\d+$/.test(value);
},
message: 'Please enter a valid price'
};
/**
* Ensure the date entered is after the current date.
* @public
* @class Later date validation.
* @memberof koValidation
* @requires koValidate
* @name laterDate
* @example
* this.myDate.extend({laterDate: true});
*/
ko.validation.rules['laterDate'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.laterDate
* @param {string} value Value to be validated
* @param {params} params Validation parameters.
* @returns {boolean} Validation success - true or false.
*/
validator : function(value, params) {
var userDate, currentDate, difference;
if (value) {
userDate = new Date(value);
currentDate = new Date();
difference = currentDate - userDate;
return difference < 0;
} else {
return false;
}
},
message : 'Please enter a proper date'
};
/**
* Ensure the value is a valid URL.
* @public
* @class URL validation.
* @memberof koValidation
* @requires koValidate
* @name url
* @example
* this.myURL.extend({url: true});
*/
ko.validation.rules['url'] = {
/**
* Validator function.
* @function validator
* @memberof koValidation.url
* @param {string} value Value of the URL to be validated.
* @param {boolean} validate Flag indicating if the URL should be validated.
* @returns {boolean} Validation success - true or false.
*/
validator : function(value, validate) {
if (!validate) {
return true;
}
var urlregex = /^(ftp|http|https)?:\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!-\/]))?$/;
if (value) {
var initialArray = value.split("\n");
var finalArray = [];
for(var index = 0; index < initialArray.length; index++) {
var tempArray = initialArray[index].split(",");
for(var tempIndex = 0; tempIndex < tempArray.length; tempIndex++) {
finalArray.push(tempArray[tempIndex]);
}
}
for (var i = 0; i < finalArray.length; i++) {
if (!urlregex.test(finalArray[i].trim())) {
return false;
}
}
return true;
} else {
return true;
}
},
message : 'Please enter a valid URL'
};
/**
* Ensure the value is a valid URL.
* <p>
* Handles the case where the URL is a full, absolute URL, e.g. "http://www.company.com/path/to/mypage",
* or just the resource path of URL, beginning with forward slash, e.g. /aboutUs
* @public