I am trying to develop a method in JavaScript that can validate data in a similar way to C# syntax. Here is an example of my C# code:
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();
}
Now, I want to convert this code to JavaScript:
var IsValid = function (modelState) {
return $.trim(modelState).length > 0 ? true : false;
}
var method = function () {
var modelState = "My model";
// How can I achieve something like:
// return modelState.IsValid();
}
Although I looked at this question for reference, it did not provide the solution I was looking for.
One of the answers suggested:
function Foo() {};
Foo.talk = function() { alert('I am talking.'); };
Foo.talk();
However, the object in their example is Foo
, whereas in my case, modelState
is a string.
Is there a way to achieve this functionality in JavaScript?