210 likes | 402 Views
JavaScript 4. User Input Validation. Input Validation. User enters data. 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
E N D
JavaScript 4 User Input Validation
Input Validation User enters data • 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 "oops!" input Validator checks format not ok ok input Server processes it
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
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; }
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
Credit Card Numbers • The last digit of a 16-digit 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
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
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
Note that MOD 11 can result in 10. So the last digit may be X = 10. (Roman numbers live!)
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.
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
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.
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
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) C is checksum Treat DDMMYYPPP as a large number and MOD by 31 MOD is mapped to a single character from 0123456789ABCDEFHJKLMNPRSTUVWXY So if MOD 31 is 15 checksum is E Finland's Sosiaaliturvatunnus or SOTU You are not expected to know this for exam purposes
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 Exercises
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