Verification of Radiobox Groups in AngularJS

Could you please help me with validating a radiogroup using AngularJS, which is instantiated within an additional directive?

The validation requirement is that the User must not be able to submit unless one of the radio buttons has been selected.

This is how I have set it up:

HTML document :

<ng-form name="salutationgroup">
      <radiotrigger model="mrms" name="gender" value='1' label="mr" ></radiotrigger>
      <radiotrigger model="mrms" name="gender" value='2' label="ms" ></radiotrigger>
</ng-form>

JS document:

Directives.directive('radiotrigger', function() {
    return {
        restrict: 'E',
        scope: {},
        compile: function(element, attrs) {
            var templateText = '<li><label><input type="radio" ng-model="' + 
                    attrs.model + '" value="' + 
                    attrs.value + '">' +
                    attrs.label + '</label></li>';

            element.replaceWith(templateText);
        }
    };
});

Thank you for your assistance.

Answer №1

Here is one approach:

Using HTML tags

<ng-form name="greetingsform">
    <radiocontrol model="hello" name="salutation" value='1' label="hi"></radiocontrol>
    <radiocontrol model="hello" name="salutation" value='2' label="hey"></radiocontrol>
    <input type="submit" ng-disabled="!valid"></input>
</ng-form>

Custom Directive (Pay attention to 'ng-change="valid=true"'):

Directives.directive('radiocontrol', function() {
    return {
        restrict: 'E',
        scope: {},
        compile: function(element, attributes) {
            var newTemplate = '<li><label><input type="radio" ng-model="' + 
                    attributes.model + '" value="' + 
                    attributes.value + '" ng-change="valid=true">' +
                    attributes.label + '</label></li>';

            element.replaceWith(newTemplate);
        }
    };
});

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

What is the best way to include extra parameters when using Upload.upload in AngularJS?

I'm facing an issue with uploading a file using AngularJS var requestData = { url: "/file_upload", data: {"name":"jack", file:file}, }; Upload.upload(requestData); This is the code in my controller. app.post("/file_upload", functio ...

Encountered an error while attempting to insert a new user into a MySQL database using the Node, Express, and MySQL package

Currently in the process of creating a CRUD API using NodeJS. My main goal at the moment is to execute a POST request to add a new user to the database. The issue I am facing is that although I can successfully add a new user to the database, my server cra ...

Unable to activate focus() on a specific text field

It's quite peculiar. I'm working with a Sammy.js application, and my goal is to set the focus on a text field as soon as the HTML loads. Here's the CoffeeScript code snippet I've written: this.partial('templates/my-template.jqt&ap ...

Learn how to utilize JavaScript produced by the `webpack output library` in a `nodejs` application

I'm currently utilizing webpack to bundle my JavaScript into a library that serves two purposes: one for browser usage and the other for integration into Node.js applications. Below is a snippet of my webpack configuration: output: { filename: ...

There seems to be an issue with Kurento-rtsp2webrtc functionality on AWS

After following the instructions of the kurento-rtsp2webrtc tutorial to stream RTSP on a website, I was able to successfully run the demo in a local network environment. However, when attempting to run it on AWS EC2 with VPN, everything worked fine. Unfor ...

The <select> element in AngularJS 1.5 is showing the dropdown at unexpected moments, but only on iOS 10 devices

From the title, it's evident that this bug is both intriguing and frustrating. The issue lies with a regular select element in an Angular partial containing the following code: <select ng-options="availweek.weekNumber as availweek.weekNumber for a ...

Clicking the button becomes impossible after entering text and using `evaluateJavascript`

I am currently working on finding a solution to input the receipt number and click the 'Check Status' button on this specific website: After successfully entering the receipt number: document.getElementById('receipt_number').value = &a ...

Request for a new login using credentials via ajax

We've set up a web application on a LAMP(PHP) system that makes around 40 Ajax calls. Users are tracked using PHPSessions with a 1 hour cookie lifespan (which we want to maintain for quick expiration). ISSUE: If a user is inactive for the full hour an ...

What do you want to know about Angular JS $http request?

My goal is to send a request using $http with angular js in order to retrieve a json object from google maps. $http.get('http://maps.googleapis.com/maps/api/geocode/json?address=' + data[ 'street' ] + ',' + data[ 'city&a ...

Helping JavaScript determine my current location on the website

One issue I am facing is with a single template file that renders pages that may look similar but have slightly different behaviors. The header and text boxes are filled by the template language, while the canvas content distinguishes the pages. Different ...

Chrome mistakenly identifying octet-stream as a .png image

My application incorporates the use of Google Maps API V3 and ASP.Net, utilizing OverlayView to customize icons on the map. The icon's image is configured within the onAdd event by dynamically adjusting the background CSS property using JavaScript. T ...

Updating the angular $resource method

I need to customize the query()-method of AngularJS $resource by using a custom $http-GET. However, it doesn't seem to be overriding the operation correctly. The result I am getting is an object with method, data, and headers, instead of data.rows[]. ...

Collecting information from JSON files

Within the cells of my calendar are placeholders for events, dates, participants, and descriptions. Now, I am looking to populate these placeholders with data previously stored using localStorage. Unfortunately, my current code is not achieving this. How ...

Experimenting with a custom AngularJS filter designed to extract numerical values from a chunk of text

I am working on creating a unique filter that can extract numbers from text input into a text box. For example: User enters: The cat went to 4 stores and bought 3 bags of cat litter for 1 dollar. The desired output would be: [4, 3, 1] This filter works ...

Tips for displaying HTML content in an AJAX success alert message with ASP.NET MVC and jQuery

I have an action result that sends content in the following format: public ActionResult MyAction() { string mystring = //doing something return Content(mystring , "html"); } Client Side: $.ajax({ url: "/MyController ...

jquery is showing up in the browserify bundle.js file, however, it is not functioning properly

Currently, I am trying to follow a brief tutorial on how to use Browserify. Despite following the instructions precisely, jQuery seems to not be working properly when bundled. Specifically, the button element in my app.js code is not appended to the body. ...

Attempting to retrieve the MongoDB data for the designated field (data.site.alerts.alert) and transfer it to the client side

*****My code ***** import logo from './logo.svg'; import './App.css'; import React, { Component } from 'react'; import axios from 'axios'; export default class App extends Component { state = { arraydata: ...

Utilizing a third-party API within the next/api endpoint

I've recently started working with NEXTJS and developed a weather application. I'm integrating the openweather API, but I'm unsure how to use it within the next/api. I attempted creating a file named today.js inside the next/api directory an ...

What are the applications of global variables in node.js?

global.test = "test"; console.log(global.test); //test but I want to accomplish this: console.log(test); //test without using: var test = global.test; Is there a way to achieve this? I am looking for a solution where any module in my project does not ...

The function designed to create in-line TailwindCSS classNames may work inconsistently in React and NextJS environments

Currently, I am utilizing TailwindCSS to style a web application. One frustrating aspect is that when attempting to add before: and after: pseudo-elements, it must be done individually. For instance, take the navigation drop-down button styling: <span ...