1

I have the following function:

   function checkRegexp(o, regexp, n) {

            if (!(regexp.test(o.val()))) {                   
                return false;
            } else {
                return true;
            }
        }

This code correctly validates email addresses:

 checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "incorrect email ");

This is supposed to check for numbers:

checkRegexp(numberId, "^[0-9]", "enter only number");

But I get this error: regexp.test is not a function

Jeff B
  • 29,943
  • 7
  • 61
  • 90
Ma Eb
  • 53
  • 7

8 Answers8

5

You're passing a string, not a regex, it should be

checkRegexp(numberID, /^[0-9]/, "enter only number");

Without the /, JS has no way of knowing you want that to be a regex.

Marc B
  • 356,200
  • 43
  • 426
  • 500
2

You are passing in a string instead of a regular expression.

Try:

checkRegexp(numberId, /^[0-9]/, "eneter only number");
Henry
  • 824
  • 4
  • 7
2

First, you passed a string, not a regexp:

"^[0-9]"  // string
/^[0-9]/  // regexp

Second, I guess you mean ^[0-9]+$ since e.g. "1a" will pass now, which is not only a number.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 1
    +1 for the `/^[0-9]*$/`, but `/^[0-9]+$/` also could be an option. The first also allows the empty string, the latter does not. – Arjan Mar 05 '12 at 19:17
2

Regex are the slowest kind there is Cached regex are actually faster than this solution. However, I'm leaving it here for reference :-). To test if it is a number, you can use this function:

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

Source: https://stackoverflow.com/a/1830844/851498

P.S: other answers will tell you why your code is wrong, I prefer to show you a better solution.

Community
  • 1
  • 1
Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
  • Unless you are doing this lots of times, the performance difference is negligible. It looks like he is doing it a couple of times maybe for form validation. However, I agree that using a built-in function is probably a better way to go. – Jeff B Mar 05 '12 at 19:21
  • cached regex literal is faster (in chrome 17) even if you leave out `parseFloat`. http://jsperf.com/numeric-checks – Dagg Nabbit Mar 05 '12 at 19:40
  • Well, I'm surprised by this as I thought regex were always the slowest thing in JS. Good to know there can be worse. – Florian Margaine Mar 05 '12 at 20:01
1

Something delimited by forward slashes (/^[0-9]/) is a RegExp object. "^[0-9]" is just a string, which you cannot call .test() on.

Try this instead:

checkRegexp(numberId, /^[0-9]/, "enter only numbers");

Also, if you want to make sure numberId only contains digits, you actually want:

checkRegexp(numberId, /^[0-9]+$/, "enter only numbers");

Basically, make sure there is only one or more digits [0-9]+, between the beginning ^ of the value and the end $.

Jeff B
  • 29,943
  • 7
  • 61
  • 90
0

You enter a String instead of Regexp. This will work:

checkRegexp(numberId, /^[0-9]/, "eneter only number");
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
0

You are using double quotes in the last sentence.

Try this:

checkRegexp(numberId, /^[0-9]/, "eneter only number");
erickzetta
  • 681
  • 5
  • 3
0

You are missing / / or use new RegExp("^[0-9]"). It has to be a RegExp object to invoke .test function

Change as below,

checkRegexp(numberId, /^[0-9]/, "eneter only number");
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134