0

I have function like this:

var name_regex = /^[a-zA-Z0-9_ -]{3,32}$/,
body_regex = /^[a-zA-Z0-9_ -]$/,
email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/,
phone_regex = /^[0-9-]{3,32}$/,
error_count;

function validation_test (name, value) {
    var test_name = name + '_regex';
    console.log(test_name);
    error_count = 0;
    if(!test_name.test(value)){
        error_count += 1;
    }
}

And if I try to run it (on submit) I get following error:

test_name.test is not a function

console.log(test_name) is giving me the proper name for the variable (for example name_regex). How can I make this variable work?

4
  • So test_name contains the name of a function you want to run? Commented Nov 19, 2012 at 22:01
  • 2
    You could try window[test_name] if it is defined in the global scope. Else namespace[test_name] where namespace is the namespace where the variable to test are defined in. Commented Nov 19, 2012 at 22:02
  • 1
    test is one of the methods of regular expression object, not the string. Commented Nov 19, 2012 at 22:02
  • Added the rest of the code to be clear what I want Commented Nov 19, 2012 at 22:04

4 Answers 4

5

Just use an object:

var regexes = {
    name: /^[a-zA-Z0-9_ -]{3,32}$/,
    body: /^[a-zA-Z0-9_ -]$/,
    email: /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/,
    phone: /^[0-9-]{3,32}$/
};



function isValid(name, value) {
    return regexes[name].test(value);
}

In your other code:

   if( !isValid( "phone", 123 ) ) {
       alert("Invalid phone");
   }
Sign up to request clarification or add additional context in comments.

9 Comments

Well he could simply use window[test_name] if he wants to.
There are a lot of ways to accomplish the same task in Javascript, yes. I would never recommend the above one.
@Sasha you are resetting error_count to 0 every time validation_test is called, is that intentional?
Yes, because if there is there is an error, error_count will be 1, but if all goes well error_count will be 0 (there is JQuery each function which checks each required field). Is this good method to do, or I am doing it wrong?
@Sasha well it's very confusing because the whole error_count is useless then. You might as well return true or false because you are not keeping any count, the result is always either 0 or 1 which is better expressed by false and true
|
1

Are you trying to call the function e.g.

validation_test('phone', '9184079201');

If so, I'd recommend putting your regular expressions into an object:

var regex = {
    'name':  '...',
    'body':  '...',
    'email': '...',
    'phone': '...',
};

function validation_test(name, value) {
    if(!regex[name].test(value)) {
        //...
    }
}

That way you can look them up by string.

1 Comment

This method is also working, but Esailija was first to give an answer, so I can only give you an upvote :(. Nevertheless, thank you for your help. There is nothing wrong with more knowledge :)
1

variable test_name should be proper regExp object https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/test

consider

...
function validation_test (name, value) {
    var test_name = name + '_regex';
    console.log(typeof(test_name)); // string
...

Than happen because of concatenation on the line above console log

2 Comments

you are welcome, and I guess correct answer would be because you pass string name as well. So probably string conversion is another option did not really looked details on this stackoverflow.com/questions/874709/… but looks like constructive
oh sorry, that's not case, it is just another thing:)
0

test_name is a string, not the regex it represents, so !test_name.test(value) won't do what you want.

Since you're not using name anywhere else in the function, why don't you just pass the regex in as an argument?

function validation_test (test_name, value) {
    error_count = 0;
    if(!test_name.test(value)){
        error_count += 1;
    }
}

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.