Posted By

Activetuts on 01/04/11


Tagged

regexp ISBN validation as3 ISBN-10 ISBN-13


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

chus
elderhsouza
dubogii


Validate ISBN with RegExp


 / Published in: ActionScript 3
 

URL: http://active.tutsplus.com/tutorials/actionscript/validating-various-input-data-in-flash/

From the tutorial, Validating Various Input Data in Flash (http://active.tutsplus.com/tutorials/actionscript/validating-various-input-data-in-flash/)

  1. public function validateISBN10(isbn10:String):Boolean
  2. {
  3. isbn10 = isbn10.replace(/[ -]/g, '');
  4.  
  5. if (isbn10.length != 10)
  6. {
  7. return false;
  8. }else
  9. {
  10. var valid:Boolean;
  11. var weights:Array = [10, 9, 8, 7, 6, 5, 4, 3, 2];
  12. var digits:Array = isbn10.split('');
  13. var control:String = digits.pop();
  14. var result:uint = 0;
  15.  
  16. for (var i:uint = 0; i < 9; i++)
  17. {
  18. digits[i] = digits[i] * weights[i];
  19. result += digits[i];
  20. }
  21. result = (result%11==0)?0:(11 - result % 11);
  22. switch(result)
  23. {
  24. case 10:
  25. valid = (control.toLowerCase() == 'x');
  26. break;
  27. default:
  28. valid = control == String(result);
  29. break;
  30. }
  31. return valid;
  32. }
  33. }
  34.  
  35. public function validateISBN13(isbn13:String):Boolean
  36. {
  37. var digits:Array = isbn13.match(/\d/g);
  38. var control:uint = digits.pop();
  39. var result:uint;
  40. var weight:uint;
  41. if (digits.length != 12)
  42. {
  43. return false;
  44. }else {
  45. for (var i:uint = 0; i < 12; i++)
  46. {
  47. weight = (i % 2 == 0)?1:3;
  48. digits[i] = digits[i] * weight;
  49. result += digits[i];
  50. }
  51. result = (result % 10 == 0)?0:(10 - result % 10);
  52. return (result == control);
  53. }
  54. }

Report this snippet  

You need to login to post a comment.