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?

Sasha
  • 8,521
  • 23
  • 91
  • 174

4 Answers4

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");
   }
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • Well he could simply use `window[test_name]` if he wants to. – clentfort Nov 19 '12 at 22:06
  • There are a lot of ways to accomplish the same task in Javascript, yes. I would never recommend the above one. – Christian Mann Nov 19 '12 at 22:09
  • @Sasha you are resetting `error_count` to `0` every time `validation_test` is called, is that intentional? – Esailija Nov 19 '12 at 22:11
  • 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 Nov 19 '12 at 22:16
  • @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` – Esailija Nov 19 '12 at 22:18
  • Hmmm, my newbe JS mind never considerate this. So basically I return false, and if there is any false result, I can do magic :D ? – Sasha Nov 19 '12 at 22:24
  • @Sasha I have edited the code to show it.. basically `.test` already returns either `true` or `false` so there is no need for manual `return true` or `return false` either – Esailija Nov 19 '12 at 22:27
  • Nice one :). I find your help quite educational for me, and for that I thank you :). – Sasha Nov 19 '12 at 22:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19783/discussion-between-sasha-and-esailija) – Sasha Nov 19 '12 at 23:01
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

dmi3y
  • 3,482
  • 2
  • 21
  • 32
  • 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 http://stackoverflow.com/questions/874709/converting-user-input-string-to-regular-expression but looks like constructive – dmi3y Nov 19 '12 at 22:20
  • oh sorry, that's not case, it is just another thing:) – dmi3y Nov 19 '12 at 22:34
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.

Christian Mann
  • 8,030
  • 5
  • 41
  • 51
  • 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 :) – Sasha Nov 19 '12 at 22:14
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;
    }
}
Blazemonger
  • 90,923
  • 26
  • 142
  • 180