Is your Jquery validation malfunctioning?

I am currently facing a challenge with required validation on an asp.net page. In order to validate the inputs, I have implemented multiple controls which can be hidden or displayed based on certain conditions. These controls include checkboxlists, dropdownlists, and multiselectedlistboxes. To perform the validation, I have assigned a CSS class called "required" to these controls.

However, my code is checking each option within each control for value instead of just validating if the control itself has any value. As someone who is not an expert in jQuery, but rather a novice, I am struggling to find a solution.

Here is the code that I have tried so far. Any suggestions or ideas would be greatly appreciated.

$("input[type='submit']").click(function () {

    if ($(this).val() != 'Back') {

        var names = [];
        var info=" ";
        
        $('.required input').each(function () {
            var control = $(this);
            
            if (control.is(':enabled')) {
                names[$(this).attr('name')] = true;
            }
        });
        
        $('.required option').each(function () {
            var control = $(this);

            if (control.is(':enabled')) {
                names[$(this).attr('name')] = true;
            }
        });  
        
        for (name in names) {
            var radio_buttons = $("input[name='" + name + "']");
            
            if ((radio_buttons.filter(':checked').length == 0) ||(radio_buttons.filter(':selected').length == 0)) {
                info += radio_buttons.closest("table").find('label').html()+"</br>";
            }
        }
        
        if (info != " ") {
            $("#validation_dialog p").html(info);
            $("#validation_dialog").dialog({
                title: "Validation Error!",
                modal: true,
                resizable: false,
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                }
            });
            
            return false;
        }
    }
});

For a working example, you can check out the following fiddle:

http://jsfiddle.net/bDmgk/35/

Answer №1

Here's a possible alternative solution:

To achieve the desired outcome, you can utilize the following code snippet:

$(".required input[type='radio']:checked").each(function(){

});

It is recommended to use this code block instead of:

When dealing with options under ".required", it is advisable to iterate through them using $(".required option").each(function(){ ... });

Answer №2

Greetings! I have made some modifications to your fiddle, specifically by implementing a check for the inputs within each column. Afterwards, I have added these inputs to your names array.

In order to achieve this, I utilized the following code snippet:

$('table.required:eq(0) input:checked')

By using this code, you can retrieve all the checked inputs from the first column. If the resulting array has a length of 0, it implies that no input is checked. The same procedure applies to the other columns as well.

Oh, and by the way, those input names are quite unconventional!

Please feel free to check out the updated fiddle through this link: JSFiddle

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

ObjectArray in Node.js

Building an object array in my node app involves transforming another object array. Let's assume the initial object array is structured like this... levels: [ { country_id: 356, country_name: "aaa", level0: "bbbb", level1: "cccc", level2: "dddd", le ...

Creating an AJAX data form in a JSP scenario

I want to correctly set up the data parameter for the ajax call. <script type="text/javascript"> $(document).ready(function() { $('#call').click(function () { $.ajax({ type: "post", ...

What is the most effective way to toggle the visibility of these rows within a table?

Initially, my plan was to incorporate collapsible rows into my table. However, considering the spacing and margins, I realized that approach wouldn't work. After some thought, I concluded that all I really need is a way to hide and show information on ...

Guide on displaying the value of an element in a Vue modal

I am working with a list of items displayed as <li> elements in a loop. When one of these elements is clicked, I would like a modal box to open up displaying specific content related to that particular element. The following data represents the item ...

Is the Site Header displayed depending on the scroll position and direction of scrolling?

On my website, I have a header that I want to hide when the user scrolls down 100px and show again when they scroll up 50px. I attempted to write a script for this functionality, but it doesn't seem to be working as expected. CSS /* This CSS rule w ...

display elements in indexed alphabetical order

Is it possible to format the $index output in alphabetical order instead of numerical? <div ng-repeat="name in names">{{$index}}</div> I am wondering if this can be achieved. ...

What is the best way to position a background image at the top of a div without overlapping the

I have this HTML code: <div class="page"> <div id="header"> ... </div> </div> Below is my CSS: .page { width: 1028px; background-color:#103250; margin-left: auto; margin-right: auto; } ...

Utilize a method in Vue.js to filter an array within a computed property

I have a question regarding my computed property setup. I want to filter the list of courses displayed when a user clicks a button that triggers the courseFilters() method, showing only non-archived courses. Below is my current computed property implement ...

There seems to be an issue with the status being passed correctly in the WCF JSON

My question is straightforward: how do I ensure the correct status code is passed to my success or error methods when calling a WCF service through JSONP? When WCF handles a jsonp response and the status code is not 200, it sends the status code as the se ...

What is the best way to display AdSense ads exclusively on webpages with a height exceeding 2400 pixels?

Looking to gather data on page height in order to display adsense only on pages with over 2400px height. Any suggestions would be greatly appreciated! <script> var height = $(document).height(); if (document > 2400 ) { echo '<!--adsense ...

What is the best way to resize a primefaces inputTextArea to match the length of the

I am currently working on improving the appearance of <p:inputTextArea/>. When the page loads, I notice: And when I click on that TextArea: Here is the code: <p:inputTextarea rows="6" value="#{bean.object.value}" style="width: 100%;" /> Is ...

Unable to access the newly created object's properties following the instantiation of a new resource in AngularJS

Currently, I am in the process of developing a new Resource utilizing AngularJS that falls under the category of Person. After successfully creating this resource, my goal is to retrieve the id associated with the new resource from the server. it('sh ...

Having trouble retrieving information from the weather website

Hello, I am interested in extracting weather information from a specific website: On this website, there is a search bar that allows users to enter the name of any city in India. Upon pressing 'Go', the user is directed to a page displaying the ...

changing the css transformation into zoom and positioning

My goal is to incorporate pinch zoom and panning using hardware-accelerated CSS properties like transform: translate3d() scale3d(). After completing the gesture, I reset the transform and switch to CSS zoom along with absolute positioning. This method ensu ...

Exploring the concept of utilizing named arguments within Express.js routing

I've searched extensively, but can't seem to find any information on this topic. My goal is to create requests like the following: url/list/message=hello?id=1234 Despite my efforts, I have not come across any resources on how to achieve this us ...

Insert some padding above and below every line of text that is centered

I am looking to achieve a specific layout for my website. https://i.stack.imgur.com/dKT52.jpg Here is what I have attempted: <h2>Make search awesome using Open Source</h2> .banner .section_title h2 { font-family: "Oswald", Arial, sans- ...

Link together a series of AJAX requests with intervals and share data between them

I am currently developing a method to execute a series of 3 ajax calls for each variable in an array of data with a delay between each call. After referring to this response, I am attempting to modify the code to achieve the following: Introduce a del ...

Leveraging the power of the map function to cycle through two arrays

Right now in React, I am utilizing array.map(function(text,index){}) to loop through an array. But, how can I iterate through two arrays simultaneously using map? UPDATE var sentenceList = sentences.map(function(text,index){ return <ListGr ...

Utilize Jquery to hide radio buttons, while allowing users to click on an image to display a larger version

My current HTML structure is restricted to SAAS, so I only have control over jQuery implementation. https://i.stack.imgur.com/OHB9K.jpg I am attempting to achieve a similar layout as shown in the image below. The main issue lies in how to (1) conceal ...

Exploring the Dynamic Display of Nested JSON Objects in Angular 6

Struggling with a complex issue and feeling lost on how to approach it. I'm currently working with Angular 6. The challenge at hand involves handling JSON data in my project. While accessing simple data like "id" or "certificate" is easy, I'm en ...