Search code examples
asp.net-mvc

ASP.NET MVC - How to Specify the Value in TextBox


How do I specify the Value that should be entered into the Textbox. I dont want to use dropdownlist. I want to accept only these three (3) values Nigeria, Ghana and Togo

If any other value is entered it should not save or hide or disable the Save button.

View

<div class="col-md-12">
    <div>
        @Html.LabelFor(model => model.COUNTRY_NAME, "Country Name")                               
        @Html.TextBoxFor(model => model.COUNTRY_NAME, new { @style = "border-radius:3px;", @type = "text", @class = "form-control", @placeholder = "Country Name", @autocomplete = "on" })
        @Html.ValidationMessageFor(model => model.COUNTRY_NAME, null, new { @class = "text-danger" })
    </div>
</div>

Please how do I achieve this?


Solution

  • When you submit the form, you can validate textbox value with jQuery.

    <button onclick="validateData();"></button>
    

    Then write a validation method as follows.

    function validateData() {
        if (Nigeria, Ghana and Togo) {
            //ajax post to your controller
        } else {
            //return error message
        }
    }