Search code examples
asp.net-mvc-3client-side-validation

How do I client validate a razor view with input tags


I am new to MVC. Is there any way to client validate a razor view having general inputs like

<input type-"text" id="txtFirstName" />

I have seen there are some ways to do it using DataAnnotation attribute in model and @Html.Textbox or @Html.TextBoxFor. But could not find something which can validate a pure HTML form element. I am using Html.BeginForm to render my form on page.


Solution

  • you want to do some validation on text box without using razor.you can come up with jquery validation.(rules and message.)

    $(document).ready(function () {

    $("#account_info").validate({
        rules: {
            phone_number: {
                required: true
            },
            recipient_name: {
                required: true,
                minlength: 6  // <-- removed underscore
            }
        },
        messages: {
            phone_number: {
                required: "this field is required"
            },
            recipient_name: {
                required: "Enter recipient name",
                minlength: "Name should be at least {0} characters long" // <-- removed underscore
            }
        },
        submitHandler: function (form) { // for demo
            alert('valid form');  // for demo
            return false;  // for demo
        }
    });
    

    });