简体   繁体   中英

Jqueryvalidation and Asp.net C# codebehind

I am currently building a ASP.NET C# form. This form had many fields and many validations. Which included:

  • Validate when the required checkbox is checked
  • Validate a dynamic form (eg additional address form generate dynamically when add address button click)

At first, I was planning to use ASP.NET form control to create all the fields and validations. But later on I found there is a plugin call jqueryvalidation, which simply provided what I need through Jquery.

and my question is, if I am going to use this, would it be easier for me to create the form using standard HTML form tag instead of .NET control? Or can I still use .NET control?

I am quite struggle as I want to use the .NET control because I can obtain the value easily in code behind instead of Request.Form["fieldName"] but on the other hand, I feel not convenience to use the validation that .NET provided and I am not sure whether .net can create a dynamic form as easy as Jquery either.

Please advise. Thanks!

with validation plugin you can use "when required" case like this

 $('#formSubmit').validate({ 
    rules: {
        ControlAspNetorSmtin: {
            required: {
                depends: function (element) {
                    if ($('#chkglobal').is(':checked')) {
                        return false;
                    } else {
                        return true;
                    }
                }
            }


        }.... etc

If you are using ASP.NET MVC (which you probably should be) then your best bet would be to use auto-magic validation using model validation attributes and unobtrusive-validation. This would take care of client- and server-side validation with minimal effort (apart from defining the attributes).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM