Skip to content

Commit 890894d

Browse files
committed
Merge pull request victorjonsson#408 from simivar/master
Fixed bug in uk utr validator, added tests
2 parents ec929cb + 1f3d15e commit 890894d

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/modules/uk.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,20 @@
9797
*/
9898
$.formUtils.addValidator({
9999
name: 'ukutr',
100-
validatorFunction: function (val)
100+
validatorFunction: function (utr)
101101
{
102-
var weights = [0, 6, 7, 8, 9, 10, 5, 4, 3, 2, 0],
102+
var weights = [0, 6, 7, 8, 9, 10, 5, 4, 3, 2],
103103
checkDigits = [2, 1, 9, 8, 7, 6, 5, 4, 3, 2, 1],
104-
checkSum = 0,
105-
utr = val + 'K';
104+
checkSum = 0;
106105

107-
if( /\d{10}K/.test( utr ) ){
106+
if (/\d{10}/.test(utr) && utr.length === 10)
107+
{
108108
for (var i = 0; i < 10; i++) {
109109
checkSum += utr[ i ] * weights[ i ];
110110
}
111111

112-
if( utr.charAt( 0 ) === checkDigits[ checkSum % 11 ]){
112+
if (parseInt(utr.charAt(0)) === checkDigits[checkSum % 11])
113+
{
113114
return true;
114115
}
115116
}

test/qunit.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,32 @@
984984
});
985985
});
986986

987+
/*
988+
* UK VALIDATION
989+
*/
990+
test("UK UTR validation", function(){
991+
992+
clearForm();
987993

994+
var links = [
995+
{val:'9300960004', isValid:true},
996+
{val:'7795475945', isValid:true},
997+
{val:'2010335150', isValid:true},
998+
{val:'3317055678', isValid:true},
999+
{val:'2444339765', isValid:true},
1000+
{val:'3867839121', isValid:true},
1001+
{val:'38678391210', isValid:false},
1002+
{val:'386783912', isValid:false},
1003+
{val:'1779560363', isValid:false},
1004+
{val:'8648200373', isValid:false},
1005+
{val:'9300648179', isValid:false}
1006+
];
1007+
1008+
$.each(links, function(i, obj) {
1009+
runTest(obj, 'ukutr');
1010+
});
1011+
1012+
});
9881013
// TODO: Write more tests...
9891014

9901015
}

0 commit comments

Comments
 (0)