Skip to content

Instantly share code, notes, and snippets.

@LSTANCZYK
Forked from DaveHogan/MustBeEmptyAttribute.cs
Created September 25, 2017 00:08
Show Gist options
  • Save LSTANCZYK/41bbf8e8d12ddd07d458ff4de036a8ce to your computer and use it in GitHub Desktop.
Save LSTANCZYK/41bbf8e8d12ddd07d458ff4de036a8ce to your computer and use it in GitHub Desktop.
MVC ValidationAttribute to ensure property is empty. Sounds strange but I create for an anti-bot field to which is hidden in css but bots still try filling in.
public class MustBeEmptyAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
if (value is string)
{
if (((string) value).Length == 0)
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment