1 / 19

JavaScript 4

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

edna
Download Presentation

JavaScript 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JavaScript 4 User Input Validation

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

  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. 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; }

  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. 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. 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. Luhn Formula for Credit Cards

  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. Note that MOD 11 can result in 10. So the last digit may be X = 10. (Roman numbers live!)

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

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

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

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

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

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

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

More Related