2 Replies - 1711 Views - Last Post: 07 March 2011 - 03:23 PM

#1 flint0131   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 12-January 11

s is undefined

Posted 07 March 2011 - 01:53 PM

function getdigits(s) {
   return s.replace(/[^\d]/g, "");
}



This will code will give "s is undefined" error. I don't understand why..

Here's the whole code,
function getdigits(s) {
   return s.replace(/[^\d]/g, "");
}

function phone_get_type(val) {
    return (getdigits(val).charAt(0) == '6' || getdigits(val).charAt(0) == '0') ? 1 : 0;
}
function vp(val, type) {
    /*!
     * type 0 = telephone
     * type 1 = cellphone
     * */
    return (type == 0) ? /^[0-9]{3}[0-9]{4}$/.test(getdigits(val)) : /^(63|0)+[0-9]{10}$/.test(getdigits(val));
}
function vcc(cc) {
    return /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/.test(getdigits(cc));
}
function fcc(cc) {
    var str_builder = new String();
    for (var i = 0; i < cc.length; i++) {
        var c = cc.charAt(i);
        str_builder += (parseInt(c) % 1 == 0) ? c : '';
    }
    return str_builder;
}
function luhn(cc) {
    var sum = 0;
    var i;
    for (i = cc.length - 2; i >= 0; i -= 2) {
        sum += Array (0, 2, 4, 6, 8, 1, 3, 5, 7, 9) [parseInt (cc.charAt (i), 10)];
    }
    for (i = cc.length - 1; i >= 0; i -= 2) {
        sum += parseInt (cc.charAt (i), 10);
    }
    return (sum % 10) == 0;
}


function fp(p, t) {
	if (t == 1 && p.charAt(0) == '6') {
		return '+'+ p;
	} else if (t == 0 && /^[0-9]{3}[0-9]{4}$/.test(p)) {
		return p.substring(0, 3) +'-'+ p.substring(3);
	} else {
		return p;
	}
}



Can someone help? :)

Is This A Good Question/Topic? 0
  • +

Replies To: s is undefined

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: s is undefined

Posted 07 March 2011 - 02:24 PM

It seems that at some point in your code, the getdigits(s) function is called with an undefined value. The error is not in the function itself, but in the code that calls it. (Note that this includes the phone_get_type and vp functions, since they pass their parameters directly to the getdigits function.)

It's usually a good idea to make sure input parameters are in order before trying to use them so you don't get errors like these.
For example:
function getdigits(s) {
    if (typeof s != "string") {
        return null;
    }
    else {
        return s.replace(/[^\d]/g, "");
    }
}


Then you can check for null return value where you use the function, or you will get a null reference error there. (Which is still better than the current error, as it will give you a location closer to the actual problem.)

This post has been edited by Atli: 07 March 2011 - 02:25 PM

Was This Post Helpful? 1
  • +
  • -

#3 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

Re: s is undefined

Posted 07 March 2011 - 03:23 PM

PS. [^\d] = \D
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1