How can we make sure a checkbox is only checked when there is corresponding data inputted?

I am facing an issue with my ng-repeater, where each row contains a checkbox and 3 text fields. I want to ensure that the user can only select the checkbox if they have entered data in all 3 text fields. If the user tries to select the checkbox without entering any data, I need to show a warning message.

I believe I need to check if the ng-models for the text fields are null or undefined before allowing the checkbox to be selected, but I'm not sure how to implement this in HTML.

This is a snippet of my HTML code:

<div ng-repeat="o in objects">
    <input type="checkbox" class="myClass" ng-click="doSomething(argument)>
    ....
    ....
    <input ng-model="model1">
    <input ng-model="model2">
    <input ng-model="model3">

EDIT: After some research, I found a solution on this Stack Overflow post about AngularJS form custom validation.

Answer №1

To ensure all three text inputs have a value, you can disable them and use the ng-change directive to keep track.

Here is a sample HTML snippet:

<input type="text" ng-model="data.item3"  ng-change="update()"/>

<input type="checkbox" ng-model="data.model1" ng-disabled="checksDisabled"/>

Example of a controller:

.controller('MainCtrl',function($scope){
     $scope.checksDisabled=true;
     var data = $scope.data ={ };
     $scope.update=function(){
        $scope.checksDisabled = !(data.item1 && data.item2 && data.item3);
     }         
});

Check out the demo here!

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

Resulting from a factory, a resolved promise is making its way back

My Angular app features a menu defined in an MVC view that is separate from the app's routes. Within the menu controller, I am looking to inject an object containing information about the current user. This data will be used to determine which menu it ...

What is the best way to decrease the border width of a chartjs doughnut chart?

I have a vision for my chart based on the mockup: However, here is what I've been able to achieve using chartjs so far: This is the code I'm working with: datasets: [ { data: [3, 8, 13, 9, 2], backgroun ...

Attempting to grasp the correct method for understanding For loops

Lately, I've been diving into teaching myself Node.JS and it has been quite an enjoyable experience. However, I've hit a frustrating roadblock that is really causing me some grief. For some reason, I just can't seem to grasp the concept of F ...

Position the table next to the textarea using HTML

Incorporating both a textarea/button and a table within the body of my page, I face an issue where the table fails to expand to match the div's full height despite aligning it next to the textarea/button. Attempts to set its height using various attri ...

Inserting line breaks in the data retrieved through AJAX

Utilizing ajax to transfer data from a sophisticated custom field wysiwyg editor. Within the markup provided, I am specifically addressing the div with the 'bio' class. However, upon receiving the data, it is consolidated into one large paragraph ...

Run the setInterval function immediately on the first iteration without any delay

Is there a way to display the value immediately the first time? I want to retrieve the value without delay on the first load and then refresh it in the background. <script> function ajax() { ...

What is the best way to handle variables in javascript?

After making an AJAX request, I expected the results to display when I select from the drop down menu. However, I encountered a problem where only a table is being displayed. I am confused as to why the data is not coming from the variables $grade (calcula ...

Monitoring the "signed in" status within an AngularJS platform

I'm feeling a bit lost as I navigate my way through Angular. As a newcomer, I find myself writing quite a bit of code to keep track of the logged-in state. However, this approach seems fragile and unreliable. Currently, I rely on a global variable to ...

What is the best way to pass the value of a variable from a controller to a config function

My code... Config function config.$inject = ['$routeProvider', '$locationProvider']; function config($routeProvider, $locationProvider ) { $routeProvider .when('/', { controller: 'LoginControll ...

Derive the property type based on the type of another property in TypeScript

interface customFeatureType<Properties=any, State=any> { defaultState: State; properties: Properties; analyzeState: (properties: Properties, state: State) => any; } const customFeatureComponent: customFeatureType = { defaultState: { lastN ...

Using Node.js to retrieve child processes associated with a daemon and terminate them

I am attempting to create a node application that allows me to send the command kill -9 to all child processes of a single daemon. Just to clarify, there is one daemon running on our server. Upon startup, it initiates a process for communicating with clie ...

Error: Unable to locate module: Could not find '@/styles/globals.scss'

I'm encountering an error message with my import statement for the SCSS file in my _app.tsx. Can someone help me find a solution? I'm working with Next.js and have already exhausted almost every resource available online to fix this issue. ...

Troubleshooting: Issue with AngularJS Image onload directive - "this" reference not functioning properly?

I have a custom directive that looks like this: .directive('ngImageOnLoad', function () { return { restrict: 'A', link: function(scope, element, attrs) { element.bind('load', function() { ...

Troubleshooting issues with JavaScript progress bar functionality

I have implemented a progress bar using HTML5, JavaScript and Ajax to showcase file uploads in PHP. The issue I am facing is that the progress bar is not displaying the progress correctly. In addition to that, the echo statements in the PHP code are no ...

Can you confirm if this is the most efficient method for loading google-analytics and jQuery?

It's not necessary for jQuery to be loaded immediately on page load: Here is what I currently have: <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', '...']); _gaq.pus ...

streamlined method for accessing page parameters in nested components using the next.js application router

In my next.js application, I have a deep hierarchy of nested components. I currently use the params.lang parameter for translations, but I find myself passing it down to every nested component. Although there are hooks available, I prefer rendering them ...

Reorganizing data with JSON using JavaScript

I have a JSON data that I am parsing with NodeJS and I need to restructure it into a different JSON format. The original JSON includes multiple "pages" objects within the "rows" object, each containing similar keys and values except for the "values" and "d ...

Using an ng-repeat in AngularJS to filter items based on a route

I'm fairly new to Angular and I have implemented an ng-repeat feature in a list that can be filtered by clicking on tabs. Below is an example of what a tab looks like: <a href="javascript:;" ng-click="$parent.zonefilter=zone.name" class="ng ...

Employ Mustache.js for displaying a series of data points retrieved from a JSON reply

Utilizing Mustache.js to format the response from a jQuery getJSON() request has been quite helpful. The getJSON request is responsible for obtaining a list of image names, which I plan to display at the end of my content once the call is made. The JSON d ...

Implementing conditional statements in Puppeteer web scraping

Attempting to extract data from a table... Each country name is wrapped in an <a> tag, however some are not. Unfortunately, when the structure of the HTML changes, the program crashes Code => https://i.sstatic.net/zW3Cd.png Output => https: ...