0

I want to prevent a user from entering a TimeZoneId of type string. Currently, the validation is not catching this issue. How can I prevent a user from entering a string value?

Validator in my Provider:

Currently, my AddValidator prevents a user from entering a null value

  AddValidator(p => p.TimeZoneID != null && projectValidator.IsTimeZoneIdInvalid(p.TimeZoneID.Value), "TimeZoneId", "Invalid time zone ID");

I've tried adding the following, but it doesn't work. It returns an error stating Cannot apply operator '!=' to operands of type 'System.Nullable':

    AddValidator(p => p.TimeZoneID != null && p.TimeZoneID != typeof(string) && projectValidator.IsTimeZoneIdInvalid(p.TimeZoneID.Value), "TimeZoneId", "Invalid time zone ID");
ChaseHardin
  • 2,189
  • 7
  • 29
  • 50

1 Answers1

1

Its a little unclear what you want exactly so here are some options:

Quoting from @Dan Tao from the thread check content of string input:

Well, to check that an input is actually an object of type System.String, you can simply do:

bool IsString(object value)
{
return value is string;
}

To check that a string contains only letters, you could do something like this:

bool IsAllAlphabetic(string value)
{
foreach (char c in value)
{
    if (!char.IsLetter(c))
        return false;
}

return true;
}

If you wanted to combine these, you could do so:

bool IsAlphabeticString(object value)
{
string str = value as string;
return str != null && IsAllAlphabetic(str);
}

Using these methods you could do something like:

AddValidator(p => p.TimeZoneID != null && !IsString(p.TimeZoneID) && projectValidator.IsTimeZoneIdInvalid(p.TimeZoneID.Value), "TimeZoneId", "Invalid time zone ID");
Community
  • 1
  • 1
psoshmo
  • 1,490
  • 10
  • 19