Solved

Jquery Validate Html.DropDownListFor Not Working?

Posted on 2013-11-29
8
603 Views
Last Modified: 2013-12-08
I use the following code to successfully validate Html.EditorFor and Html.TextBoxFor. I cannot validate the Html.DropDownListFor. It gets ignored on submit button click, Help.

$("#form1").validate(
            {
                rules: {
                    'Vendor.Name': {
                        required: true
                    }
                },
                'Vendor.VendorTypeID':
                    {
                        notEqual: "-select-"
                    },

                messages:
                    {
                        'Vendor.Name': { required: " Required!" },
                        'Vendor.VendorTypeID': " Required!"
                    },

                success: function (label) {
                    label.html('OK').removeClass('error').addClass('ok');
                    setTimeout(function () {
                        label.fadeOut(500);
                    }, 1000)
                }
            });

Also noticed something strange about Jquery and the Html.DropDownListFor. The following code either returns 'undefined' for blank alert box.

 $('#form1').bind('submit', function ()
 {
     alert($('#Vendor.VendorTypeID').val()); //returns 'undefined'

     alert($('#Vendor.VendorTypeID').text()); //returns ''
  });


[Form - View Source]
<form action="/Vendor/New" id="form1" method="post">                
       <select  id="Vendor_VendorTypeID"
               name="Vendor.VendorTypeID">
                    <option value="">-select-</option>
                    <option value="1">Auto Service</option>
                    <option value="12">Bank</option>

      <input type="submit" value="Save" />
</form>
0
Comment
Question by:WorknHardr
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:Jitendra Patil
ID: 39686464
hi

i think you need to put a value to option value of select as below.
<option value="-select-">-select-</option>

as per the jquery code you are checking the value as "-select-" which is not there in your option value and that's why it may be not working.

hope this helps
0
 

Author Comment

by:WorknHardr
ID: 39686886
Q. Why does .text return a blank alert window?
0
 
LVL 12

Accepted Solution

by:
Jitendra Patil earned 500 total points
ID: 39687777
your are trying to acces the control by its name,why don't your try with the id you are specifiying i mean to say "Vendor_VendorTypeID".
0
 

Author Comment

by:WorknHardr
ID: 39693082
Okay, you're correct about using _ underscore with a Html.DropDownListFor

Note: I still need to use the . for the Html.EditorFor

Anyway, new problem: I cannot see error message for DropDownListFor

I do think the validation is working because it halts the Action from being called and the JQuery ' submitHandler' returns it's alert.

[Current Code]
 $(function () //onload
    {
        jQuery.validator.addMethod('NonEmpty', function (value) {
            return ($("#Vendor_VendorTypeID option:selected").text() != '-select-');
        }, '');

      $("#form1").validate(
            {
                rules: {
                    'Vendor.Name': {
                        required: true
                    }
                },
                'Vendor_VendorTypeID':
                    {
                        required: true,
                        NonEmpty: false
                    },

                messages:
                    {
                        'Vendor.Name': { required: " Required!" },
                        'Vendor_VendorTypeID': { NonEmpty: " Required!" }
                    },

                submitHandler: function (form) {
                    alert('valid form submitted');
                    return false;
                },

                success: function (label) {
                    label.html('OK').removeClass('error').addClass('ok');
                    setTimeout(function () {
                        label.fadeOut(500);
                    }, 1000)
                }
            });
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:Jitendra Patil
ID: 39697710
hi

if you have added   <option value="--select--">-select-</option> in the markup then the option value will never be empty. you need to check it with the value of the dropdown
if it says --select-- then say its false  and the value is required else ok to go.

try to change the validation code for
 'Vendor_VendorTypeID':
                    {
                        required: true,
                        NonEmpty: false
                    },

HTH!
0
 

Author Comment

by:WorknHardr
ID: 39698037
I was over-thinking the DropDownFor validation. Here's my simple solution:

[New Code]
 $(function () //onload
    {
      $("#form1").validate(
            {
                rules: {
                    'Vendor.Name' : {
                        required: true
                    }
                },
                'Vendor.VendorTypeID' : {
                        required: true
                    },

                messages:
                    {
                        'Vendor.Name': {
                                required: " *"
                         },
                        'Vendor.VendorTypeID': {
                                required: " *"
                         }
                    },

                success: function (label) {
                    label.html(' ').removeClass('error').addClass('ok');
                    setTimeout(function () {
                        label.fadeOut(500);
                    }, 1000)
                }
            });
0
 
LVL 12

Expert Comment

by:Jitendra Patil
ID: 39703968
so i think you managed to get it work, good.
0
 

Author Closing Comment

by:WorknHardr
ID: 39704243
Thx for helping me along
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now