I am coding to make simple validator for form.
In order to validate form dynamically, I’ve planned to use eval function.
As you see in the javascript coding, var funcCall becomes a name of function with a parameter of jquery selected dom.
But that makes error. (Uncaught SyntaxError: Unexpected identifier)
What I’d like to know is that should I only use String in eval() or is there a better way to do this?
var Submit = {
validateFilters : ["Submit.emptyCheckFilter"], // This is validator filters
emptyCheckFilter : function(element)
//this is filter to validate if it's empty or not.
console.log("this has been called");
},
doValidate : function(form) {
var children = $(form).children();
var filters = Submit.validateFilters;
$.each(children, function(key, value) { // Loop over form element
for(var i = 0; i < filters.length; i++) {
//Here's error comes the value is not String. it's selected item by jQuery.
//Uncaught SyntaxError: Unexpected identifier <= This occurs.
var funcCall = filters[i] + "(" + value + ");";
// Call the filter
eval(funcCall);
}
return false;
});
},
...
}
You should not use eval for this. It is much simpler and cleaner to just use function instances directly instead of their “names”.