0

I am trying to validate input control in Asp.Net MVC. The below code checks if Project name is null and throws validation. I will have some default text in textbox on page load. I have a scenario to check if project name is not null and project name is not "Test Project". Can I do in similar scenario?

[Required(ErrorMessage = "Select Project Name.")]
[DisplayName("Project Type: ")]
public string SelectedProjectName { get; set; }
tereško
  • 58,060
  • 25
  • 98
  • 150
Kurkula
  • 6,386
  • 27
  • 127
  • 202
  • I did not understand. I am trying to see if we can do validation for default text inside textbox. – Kurkula Feb 17 '14 at 07:28
  • 1
    I researched on google on placeholder and found some good suggestions. Thanks Zabavsky for pointing me to placeholder. It works only with HTML5 compatible browsers though. – Kurkula Feb 17 '14 at 07:59

1 Answers1

1

you can write your own Custom validator here is an example.

    public class ProjectNameValidation : ValidationAttribute
{

    public ProjectNameValidation()
    {

    }

    //private const string errorMsg = "{0} must at least {1} or not more than {2}";

    public override bool IsValid(object value)
    {
        if (value != null && value !="Test Project")
        {
            return true;
        }

        return false;
    }
}
Ammar
  • 686
  • 11
  • 28