Return to Snippet

Revision: 38514
at January 4, 2011 11:58 by Activetuts


Updated Code
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);
    }
}

Revision: 38513
at January 4, 2011 11:57 by Activetuts


Initial Code
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;
    }
}

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

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

Initial Title
Validate ISBN with RegExp

Initial Tags
regexp, validation

Initial Language
ActionScript 3