# Posted By

Activetuts on 01/04/11

# Statistics

Viewed 442 times
Favorited by 1 user(s)

# Validate ISBN with RegExp

/ Published in: ActionScript 3
Save to your folder(s)

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

Copy this code and paste it in your HTML
`public function validateISBN10(isbn10:String):Boolean{    isbn10 = isbn10.replace(/[ -]/g, '');     if (isbn10.length != 10)    {        return false;    }else    {        var valid:Boolean;        var weights:Array   = [10, 9, 8, 7, 6, 5, 4, 3, 2];        var digits:Array    = isbn10.split('');        var control:String  = digits.pop();        var result:uint     = 0;         for (var i:uint = 0; i < 9; i++)        {            digits[i] = digits[i] * weights[i];            result += digits[i];        }        result = (result%11==0)?0:(11 - result % 11);        switch(result)        {            case 10:                valid = (control.toLowerCase() == 'x');                break;            default:                valid = control == String(result);                break;        }        return valid;    }} public function validateISBN13(isbn13:String):Boolean{    var digits:Array    = isbn13.match(/\d/g);    var control:uint    = digits.pop();    var result:uint;    var weight:uint;    if (digits.length != 12)    {        return false;    }else {        for (var i:uint = 0; i < 12; i++)        {            weight = (i % 2 == 0)?1:3;            digits[i] = digits[i] * weight;            result += digits[i];        }        result = (result % 10 == 0)?0:(10 - result % 10);        return (result == control);    }}`