I want to create a method to validate data like C# syntax by using javascript. My C# code is like this:
public static class Validate
{
public static bool IsValid(this string modelState)
{
return !String.IsNullOrEmpty(modelState) ? true : false;
}
}
bool Method()
{
string modelState = "My model";
return modelState.IsValid();
}
In javascript, I want to convert the code to:
var IsValid = function (modelState) {
return $.trim(modelState).length > 0 ? true : false;
}
var method = function () {
var modelState = "My model";
// how to:
// return modelState.IsValid();
}
I had referenced this question, but it doesn't solve my problem.
One of the answers said:
function Foo() {};
Foo.talk = function() { alert('I am talking.'); };
Foo.talk();
A Foo
is an object, but in my example, a modelState
is a string.
Is there a way to do that in javascript?