Ma Eb
Ma Eb

Reputation: 53

JavaScript RegExp error

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

Upvotes: 1

Views: 2237

Answers (8)

pimvdb
pimvdb

Reputation: 154818

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.

Upvotes: 2

Florian Margaine
Florian Margaine

Reputation: 60717

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.

Upvotes: 2

Jeff B
Jeff B

Reputation: 30099

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 $.

Upvotes: 1

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

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");

Upvotes: 0

erickzetta
erickzetta

Reputation: 691

You are using double quotes in the last sentence.

Try this:

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

Upvotes: 0

Ondra Žižka
Ondra Žižka

Reputation: 46796

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

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

Upvotes: 0

Marc B
Marc B

Reputation: 360572

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.

Upvotes: 5

Henry
Henry

Reputation: 824

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

Try:

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

Upvotes: 2

Related Questions