0

using javascript to check if string is numbers only this is what i have but its not running any suggestions would the appreciated thanks much in advance. also if it is a string of numbers only then all numbers after the first two digits should be masked.

 var start = function RenderRC(CodeOwner) {

     var pattern = /^\d+$/;
     var Rcode = CodeOwner.toString();

     if (Rcode.valueOf.match(pattern)) {
        if (Rcode.length > 2) {
            var newcode = Rcode.substr(0, 2) + Array(Rcode.length - 2 + 1).join("*");
            return newcode;
        }
     } else {
        return Rcode;
     }
 }; 
mguymon
  • 8,946
  • 2
  • 39
  • 61
chloe
  • 69
  • 2
  • 10

3 Answers3

0
function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

See here: Validate decimal numbers in JavaScript - IsNumeric()

Community
  • 1
  • 1
RyanS
  • 3,964
  • 3
  • 23
  • 37
0
function IsNumeric(sText) {
    var Reg = new RegExp('^\\d+$');
    var Result = sText.match(Reg);
    if (Result)
        return true;
    else
        return false;
}
Pankaj
  • 9,749
  • 32
  • 139
  • 283
0

Remove valueOf. and it should run fine

if (Rcode.match(pattern))
...

Or add the parenthesis to it to actually call the function:

if (Rcode.valueOf().match(pattern))
...

But I don't think that function call is needed.

Nick Rolando
  • 25,879
  • 13
  • 79
  • 119