0

I am working on an MVC project and I am new in MVC. I need to validate texts entered in the textbox. The value of this textbox can be different (file, text, and URL) in different cases. In other words, I have a enum class FileTypeEnum with three different values. So, the input for this textbox needs to validate 3 times i.e. 3 different cases. I did validate this textbox when it is a file. I need to validate the text entered when FileTypeEnum is text i.e. 7.

//validate for file i.e. ResourceTypeId == 8
if (resource.ResourceTypeId == 8)
        {
            //already done work for it.
        }

        else if (resource.ResourceTypeId == 7)
        {

    // Trying to validate in here for texts 
    //  ^[a-zA-Z]+ ^.+\@.+\..+$

        }

  //validate for url i.e. ResourceTypeId == 9   
        else
        {
    // TODO
        }

Above code is for the controller where I need to do validation. Please help.

2 Answers 2

1

If you want to validate on front end you do this with JavaScript.

On the back end you can derive your model from IValidatableObject and enforce all kinds of business rules. Sample is here: Custom validation in MVC

Sign up to request clarification or add additional context in comments.

Comments

0

Ultimately you need to use RegularExpressionAttribute. Check here for more information. But you need to create your own expression to suit your all FileTypeEnum three different types.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.