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

1
  • regular expression is not a string Commented Mar 5, 2012 at 19:18

8 Answers 8

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Try:

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

Comments

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.

3 Comments

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.
cached regex literal is faster (in chrome 17) even if you leave out parseFloat. jsperf.com/numeric-checks
Well, I'm surprised by this as I thought regex were always the slowest thing in JS. Good to know there can be worse.
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.

1 Comment

+1 for the /^[0-9]*$/, but /^[0-9]+$/ also could be an option. The first also allows the empty string, the latter does not.
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 $.

Comments

0

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

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

Comments

0

You are using double quotes in the last sentence.

Try this:

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.