Javascript Code To Convert Serial Numbers Converted From Php
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 orig
Solution 1:
There is no such thing as strlen
in Javascript. Use str.length
instead.
Also, as Jason Sperske suggested below, change this:
functionmy_isnum(str, negative=false, decimal=false)
To this:
function my_isnum(str, negative, decimal)
{
if (typeof negative == "undefined") negative = false;
if (typeofdecimal == "undefined") decimal = false;
....
}
Solution 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?
functionpadToFourDigits(_, digits) {
return"0000".substring(digits.length) + digits;
}
functionserialToNum(serialNumStr) {
return +(serialNumStr.replace(/-(\d{1,4})/g, padToFourDigits));
}
Then
serialToNum('2-9999') === 29999
serialToNum('2-999') === 20999
Post a Comment for "Javascript Code To Convert Serial Numbers Converted From Php"