Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 JavaScript 4 User Input Validation. 2 Input Validation One of the most useful applications of JavaScript is input validation Scripts in the page can.

Similar presentations


Presentation on theme: "1 JavaScript 4 User Input Validation. 2 Input Validation One of the most useful applications of JavaScript is input validation Scripts in the page can."— Presentation transcript:

1 1 JavaScript 4 User Input Validation

2 2 Input Validation 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 Validator checks format User enters data Server processes it input ok not ok "oops!"

3 3 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

4 4 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 "9")) valid=false; }

5 5 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

6 6 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

7 7 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

8 8 4499228308017422 81841600144 1+81+61+4 8499427308015442 = 70 70 mod 10 = 0 so checksum is valid Luhn Formula for Credit Cards

9 9 CardPrefixLength Visa413 / 16 American Express 34 / 3715 MasterCard51- 5516 Discover601116 Diners Club300-305 / 36 / 3814

10 10 Every book has a unique number that identifies the publisher and other information The last digit of the ISBN is a MOD 11 checksum International Standard Book Number ISBN

11 11 ISBN 0787951625 x10987654321 0636449542541845 +286 MOD 11 = 0 so it's valid

12 12 ISBN 156478214X x10987654321 454828424083810 +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!)

13 13 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.

14 14 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

15 15 PPSN 6059922H x8765432 48030453664 Total = 169 169 MOD 23 = 8 = H ABCDEFGHIJKLMNOPQRSTUVW 123456789101112131415161718192021220 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.

16 16 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 Euro banknote serial numbers

17 17 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) 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 You are not expected to know this for exam purposes

18 18 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

19 19 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


Download ppt "1 JavaScript 4 User Input Validation. 2 Input Validation One of the most useful applications of JavaScript is input validation Scripts in the page can."

Similar presentations


Ads by Google