Transcript WWW2

JavaScript 4
User Input Validation
1
Input Validation
User enters data
"oops!"
input
Validator checks format
ok
input
Server processes it
not ok
• One of the most useful
applications of JavaScript
is input validation
• Scripts in the page can
check that inputs conform
to particular formats before
sending them to the server
• This can make for a better
user experience, save time
and other resources
2
Rep. Ireland Mobile Numbers
• 08 + 3/5/6/7 + 7 digits
• Irish mobiles start with
08 followed by either
3, 5, 6, 7 (088 is
obsolete now),
followed by 7 digits
3
var mobilenum;
var valid = new Boolean(true);
mobilenum =prompt("Please enter your mobile phone number",
"0879996666");
if ((mobilenum==null) || (mobilenum==""))
valid= false;
if (mobilenum.length != 10)
valid=false;
if (mobilenum.slice(0,2) != "08")
valid=false;
var c = mobilenum.slice(2,3);
if (! ((c=="3") || (c=="5") || (c=="6") || (c=="7")) )
valid=false;
for (n=3;n<10;n++)
{
c = mobilenum.slice(n,n+1);
if ((c < "0") || (c > "9"))
valid=false;
}
4
Checksum
• Very often 1 digit of an input is used to detect
errors
• Imagine if the last digit of a phone number
was required to be the number of even digits
• 0879996665
• If someone made a mistake it might be
detected if the numbers didn’t add up
• 0879996662 - something's wrong!
• Checksums are not usually so simple and
typically applying a multiplier to some of the
digits
5
Credit Card Numbers
• The last digit of a 16digit credit card number
is a check sum
• It doesn't tell you if the
number is valid, or the
account has enough
money
• But if the checksum is
wrong it can't be correct
6
Luhn Formula for Credit Cards
• Starting with the second last digit of the
number, multiply every second digit by 2
• Add all the resulting digits, together with the
unmodified digits, and the check sum
• If the resulting number is evenly divisible by
10 then the checksum is valid
7
Luhn Formula for Credit Cards
4 4 9 9 2 2 8 3 0 8 0 1 7 4 2 2
8
18
1+8
4
16
1+6
0
0
14
4
1+4
8 4 9 9 4 2 7 3 0 8 0 1 5 4 4 2
= 70
70 mod 10 = 0 so checksum is valid
8
Card
Prefix
Length
Visa
4
13 / 16
American
34 / 37
Express
MasterCard 51- 55
15
Discover
16
6011
16
Diners Club 300-305 / 36 / 38 14
9
International Standard Book
Number ISBN
• Every book has a
unique number that
identifies the
publisher and other
information
• The last digit of the
ISBN is a MOD 11
checksum
10
ISBN
0
7
8
7
9
5
1
6
2
5
x
10
9
8
7
6
5
4
3
2
1
0
63 64 49 54 25
4
18
4
5
+
286 MOD 11 = 0 so it's valid
11
ISBN
1
5
6
4
7
8
2
1
4
X
x
10
9
8
7
6
5
4
3
2
1
10 45 48 28 42 40
8
3
8
10
+
242 MOD 11 = 0 so it's valid
Note that MOD 11 can result in 10. So the last digit
may be X = 10. (Roman numbers live!)
12
Modulus Checksums
• In general the modulus chosen needs to
be
– Greater than the number digits
– Greater than the range of any single digit
• Prime numbers work especially well
• Random ISBN errors have 90% (10/11)
chance of being caught.
13
R. Ireland PPS Numbers
• Every person working in the Republic of
Ireland has a PPS number
• Children are now issued them at birth
• Organizations that may request and store
them are specifically controlled by
legislation
• The number is 7 digits followed by a
checksum letter
14
PPSN
6
0
5
9
9
2
2
x
8
7
6
5
4
3
2
48
0
30
45
36
6
4
H
Total = 169
169 MOD 23 = 8 = H
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
0
Some woman in Ireland have PPS numbers identical to their husband's, but followed by a W. So there may be 2 letters - a check sum and a W. It is
co-incidence that W is the 23rd letter. This W is for "wife". This somewhat sexist practice has been discontinued.
15
Euro banknote serial numbers
•
•
•
•
•
•
X04135981862
Convert first letter to a number (A=1, B=2, … Z=26)
Add up all the digits 24+0+4+1+3+5+9+8+1+8+6+2 =71
Add those together (as often as required)
7+1 = 8
Final result will always be 8
16
Finland's Sosiaaliturvatunnus or SOTU
• DDMMYYSPPPC
• DDMMYY - date
• S is separator
– + for people born in 1800's
– - for people born in 1900's
– A for people born in 2000's
• PPP is a person number.
Even for females an odd for
males
• This means that at most 500
men can be born in Finland
is any 1 day! (currently <100)
You are not expected to know this for exam purposes
• C is checksum
• Treat DDMMYYPPP as
a large number and
MOD by 31
• MOD is mapped to a
single character from
0123456789ABCDEFHJK
LMNPRSTUVWXY
• So if MOD 31 is 15
checksum is E
17
Exercises
• Test ISBNs and
random 10 digit
numbers with the
ISBN checksum
• Verify your PPSN
checksum
• Verify your credit
card checksum
• Write JavaScript
code to validate
ISBNs and PPSNs
18
Other Examples
• P.R. China (mod 11), UK, Italy (mod 26) all
use checksums
• The U.S. and Canada share a numbering
system, but only Canadian numbers use a
checksum
• Many barcode systems use a checksum too
• R. Ireland CAO student numbers use a
checksum but the method used is a secret
19