What's wrong with my Javascript code?

2019-08-12 05:25发布

问题:

These two javascript functions are supposed to convert serial numbers (2-9999) for example into a number , but the functions below are not working for some reason .. they were originally written in PHP ... Works in PHP but not for Javascript.

<html>
<head>
<script type="text/javascript">

    function my_isnum(str, negative=false, decimal=false)
{
    var has_decimal = false;
    var len = strlen(str);
    if (len > 0) {
        var valid = true;
        for (var i=0; valid && i < len; i++) {
            if (!(str[i] >= "0" && str[i] <= "9")) {
                if (str[i] == "-") {
                    if (!negative || i != 0) {
                        valid = false;
                    }
                } else if (str[i] == ".") {
                    if (!decimal || has_decimal) {
                        valid = false;
                    }
                } else {
                    valid = false;
                }
            }
        }
    } else {
        valid = false;
    }
    return valid;
}

function esn_to_num(esn) {
    var tmp = [];
    if ((tmp = esn.split("-")) {
        if (tmp.length == 2
            && my_isnum(tmp[0])
            && my_isnum(tmp[1])
        ) {
            esn = ((tmp[0] << 23) | tmp[1]);
        } else {
            esn = -1;
        }
    } else {
        esn = -1;
    }
    return esn;
}

alert(2-9999);

</script> </head>
</html>

Original PHP functions

<?php
function my_isnum($str, $negative=false, $decimal=false)
{
    $has_decimal = false;
    $len = strlen($str);
    if ($len > 0) {
        $valid = true;
        for ($i=0; $valid && $i<$len; $i++) {
            if (!($str[$i] >= '0' && $str[$i] <= '9')) {
                if ($str[$i] == '-') {
                    if (!$negative || $i != 0) {
                        $valid = false;
                    }
                } else if ($str[$i] == '.') {
                    if (!$decimal || $has_decimal) {
                        $valid = false;
                    }
                } else {
                    $valid = false;
                }
            }
        }
    } else {
        $valid = false;
    }
    return $valid;
}

function esn_to_num($esn)
{

    if (($tmp = explode('-', $esn))) {

        if (sizeof($tmp) == 2
            && my_isnum($tmp[0])
            && my_isnum($tmp[1])
        ) {
            $esn = (($tmp[0] << 23) | $tmp[1]);
        } else {
            $esn = -1;
        }
    } else {
        $esn = -1;
    }

    return $esn;
}




?>

回答1:

There is no such thing as strlen in Javascript. Use str.length instead.

Also, as Jason Sperske suggested below, change this:

function my_isnum(str, negative=false, decimal=false)

To this:

function my_isnum(str, negative, decimal)
{
    if (typeof negative == "undefined") negative = false;
    if (typeof decimal == "undefined") decimal = false;
    ....
}


回答2:

These two javascript functions are supposed to convert serial numbers (2-9999) for example into a number.

Why not just get rid of the - and parse as a decimal number?

function padToFourDigits(_, digits) {
  return "0000".substring(digits.length) + digits;
}

function serialToNum(serialNumStr) {
  return +(serialNumStr.replace(/-(\d{1,4})/g, padToFourDigits));
}

Then

serialToNum('2-9999') === 29999

serialToNum('2-999')  === 20999