1

I use jquery validation in my MVC application and after selecting and leaving an input, it is automatically validated. On the other hand, if user does not select an input, they are not validated even if submitting the form. So, I want to validate all the related fields after pressing submit button on MVC Razor page. How can I do this?

Sparky
  • 98,165
  • 25
  • 199
  • 285
Jack
  • 1
  • 21
  • 118
  • 236
  • 1
    Try using jquery form validation, see this link: http://stackoverflow.com/questions/15060292/a-simple-jquery-form-validation-script – Lin Dec 02 '13 at 01:04
  • @Lin: That is not the answer of my question. Actually I followed the example on that page geektantra.com/2009/09/jquery-live-form-validation and all I have followed is true according to that page. Is there any mistake? On the other hand, I looked at your demo, but I need a validation after moving from an input and submitting the form as on the example geektantra.com/2009/09/jquery-live-form-validation. Could you please give an example like on that page containing a hidden field? Thanks in advance. – Jack Dec 08 '13 at 23:13
  • 1
    hi @H.Johnson, There are a lot of examples online. I recommend you take a look at following link: https://github.com/borisyankov/DefinitelyTyped/blob/master/jquery.validation/jquery.validation-tests.ts and http://jqueryvalidation.org/documentation/ . Try some examples, let's see if they work for you. – Lin Dec 09 '13 at 01:50
  • You're having all these problems because [the online example you're trying to follow](http://geektantra.com/2009/09/jquery-live-form-validation) is **not** using the jQuery Validate plugin! For real working code examples using jQuery Validate, [simply look at Stack Overflow](http://stackoverflow.com/questions/tagged/jquery-validate). – Sparky Dec 09 '13 at 06:00
  • @Lin: Thanks for help. Sorry, but I have never used such a kind of validation plugin before and as "jquery.validate.js" is used the approach I followed, I thought that I have used "the jQuery Validation plugin". Anyway, I need an example performing these requirements below: 1) Of course I would like to use jQuery Validation plugin. I want the fields to be validated after switch to another control (live validation). 2) In addition to this, if the user directly press the Submit button, all the necessary fields should be validated as explained on the first step. Thanks in advance... – Jack Dec 09 '13 at 22:10

1 Answers1

7

I created a simple example. It's just to give you a start, you still have to modify this code to your needs. This example is using jquery form validation.

Model

  public class Vehicle
    {
        public int Id { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
    }

Controller & Action

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

View

@model JqueryValidationDemo.Models.Vehicle

@{
    ViewBag.Title = "Jquery Form Validation Demo";
}
<script src="~/Scripts/jquery-2.0.3.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script>
    $(document).ready(function () {
            $('#myform').validate({ // initialize the plugin
                rules: {
                    Make: {
                        required: true,
                        minlength: 2
                    },
                    Model: { required: true }
                },
                messages: {
                    Make: {
                        required: "Make is required!",
                        minlength: "Make Name at least 2 characters long!"
                    },
                    Model: {
                        required: "Modl is required!"
                    }
                },
                submitHandler:
                    $("#myform").on('submit', function () {
                        if ($("#myform").valid()) {
                            //Do something here
                            alert("Validation passed");
                        }
                        return false;
                    })
            })
    });
</script>

@using (Html.BeginForm("Index", "Home", FormMethod.Get, new { @id = "myform" }))
{
    <fieldset>
        <legend>Vehicle</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Make)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Make)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Model)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Model)
        </div>
        <p>
            <input type="submit" value="Submit" />
        </p>
    </fieldset>
}

Note: Be careful with your jquery reference, you don't need to use jquery.validate.unobtrusive.js.

Hope it will give you a good start.

Update: Remove keyup or keypress

Remove keypress

$("#youElementId").bind('keypress', function (e) {
    if (e.keyCode == 13) {
        return false;
    }
    return true;
});
Lin
  • 15,078
  • 4
  • 47
  • 49
  • Thank you very much for your good explanations. As you said, it was a good start for me and now I am improving my validation. I hope your answer will be a good example fo those who are not willing to help. Regards... – Jack Dec 11 '13 at 20:15
  • The only problem is not validating on keyup. I have look at the documentation and used onsubmit, onkeyup, onclick parameters of validate method. But it does not make any sense. What I want to do : I click a textbox and do not type anything. Then I click another texbox and at this time the first clicked texbox should be validated and as it was empty a message appears. Is it possible? Thanks in advance... – Jack Dec 11 '13 at 20:58
  • 1
    @H.Johnson, I updated my answer for remove keypress using jquery. Remove keyup is same to keypress. – Lin Dec 11 '13 at 21:05
  • Dear Lin, I tried but it still the same behaviour. On the other hand, is there another way i.e. using a parameter in the validation method? Any idea? Thanks again. – Jack Dec 11 '13 at 21:20
  • I tried to use the mentioned js by adding it to jquery.validate.js, but still the same (the error messages shown better, but not displayed after keyup a texbox. On the orher hand I am not sure if I explained my problem properly: For example this scenario >>> I click Textbox A and then click Textbox B without typing nothing in Textbox A. In tat case Textbox A should shown a validation message because of leving it empty. Is it impossible? I used onfocusout parameter in validate method, but I am not sure if it is good idea. BR – Jack Dec 11 '13 at 21:40
  • 1
    @H.Johnson, what you said is jquery unobtrusive validation. it's not jquery form validation. Jquery form validation is after you click the submit button, the validation will be triggered. Below the link is using jquery form validation, you can take a look. http://www.jquery4u.com/demos/basic-jquery-validation-form/ – Lin Dec 11 '13 at 21:42
  • 1
    I guess you need to decide which validation method you want to use. The unobtrusive validation is common validate methods. But in some cases like you said before, you want to validate every fields after click the submit button, you should use form validation. – Lin Dec 11 '13 at 21:48
  • Thank you very much for this important information. In that case using jquery validation plugin and jquery unobtrusive validation "at the same time" is not good idea and I should edit my code so that I can use jquesry-validation-plugin. What can you day? Thanks... – Jack Dec 11 '13 at 21:51