Tips for implementing dropdown control validation using AngularJS validators

I am currently facing an issue with angular js dropdown validation. I have been trying to implement dropdown validation using the code from this website: https://github.com/turinggroup/angular-validator

However, upon checking the demo provided in this link: Demo, it seems that there is no specific information regarding how to validate a Dropdown control. If anyone has successfully implemented dropdown validation using the same code, I would greatly appreciate some guidance.

I have created a Plunker demonstrating my dropdown control:

MyDropdownControlFullDemo

Below is the code for my dropdown:

<select class="form-control m-b-sm" required ng-model="form.Obj" ng-options="c.Name for c in Obj track by c.Id">
</select>

$scope.Obj = [
{Id : '0', Name : 'Select' }, 
{Id : '1', Name : 'USA' },       
{Id : '2', Name : 'Canada' },
{Id : '3', Name : 'Russia' } ];

}

$scope.Obj = { Id: '0', name: 'Select' };

I am looking to display validation if the user does not select any option from the dropdown, similar to how validation appears for a textbox control. Your assistance on this matter would be highly appreciated.

Answer №1

To update your code, make the following changes:

In HTML for the select list-

    <select class="form-control m-b-sm" name="selectbox" required-message="'Yo! This field is required..'"
                            required ng-model="form.Obj" ng-options="c.Name for c in Objlist track by c.Id">
        <option value="">Select</option>
</select>

The controller will appear as follows-

 angular.module('angular-validator-demo').controller('DemoCtrl', function($scope){

 $scope.Objlist = [
    {Id: '0', Name: 'Select'},
    {Id: '1', Name: 'USA'},       
    {Id: '2', Name: 'Canada'},
    {Id: '3', Name: 'Russia'}
];

  $scope.Obj = { Id: '0', name: 'Select' };

    $scope.submitMyForm = function(){
        alert("Form submitted");
    };

    $scope.myCustomValidator = function(text){      
        return true;
    };


    $scope.anotherCustomValidator = function(text){
        if(text === "rainbow"){
            return true;
        }
        else return "type in 'rainbow'";
    };

    $scope.passwordValidator = function(password) {

        if(!password){return;}

        if (password.length < 6) {
            return "Password must be at least 6 characters long";
        }

        if (!password.match(/[A-Z]/)) {
             return "Password must have at least one capital letter";
        }

        if (!password.match(/[0-9]/)) {
             return "Password must have at least one number";
        }

        return true;
    };
});

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

express body parser is altering the date field within the body of a POST request

When a date field is sent in a POST request in the local time zone from the client side and received by the body parser, the date gets changed. The left side shows the client payload and the right side shows what is parsed on the server side: https://exa ...

Navigating through the Table using AngularJS

I was able to successfully loop through a table using AngularJS to retrieve values from a specified scope named {{rec}} as shown below. HTML <div id="AngularContainer" data-ng-app="myApp" data-ng-controller="myCtrl"> < ...

Is there a newline and colon in the req.body from the post router?

Can someone help me with extracting the post data from req.body? Here is my post data: { name:'asdf', completed: false, note: 'asdf' } However, when I try to console it using JSON.stringify, req.body ends up looking like this: {" ...

Ways to verify if a specific extjs panel has finished loading

After a specific panel has finished loading, I need to insert a JavaScript code (using the panel's ID). What is the best way to ensure that the panel has been fully rendered so that I can access its ID using document.getElementById? Thank you. ...

Sequentially animate objects with a fade-out and fade-in effect

Attempting to achieve a fade-out, fade-in animation using JavaScript and CSS. Here is the CSS animation code: @keyframes fade-in{ 0%{ opacity: 0%; } 100%{ opacity: 100%; } } @keyframes fade-out{ 0%{ opacity: 100%; } 100%{ opacity: 0%; } } Impleme ...

Using THREE.js in the pre-render function

I am encountering difficulties with updating the positions of my enemies before rendering them. Instead of creating a separate update() function, I attempted to use an onBeforeRender() function attached to each enemy object. However, nothing seems to be ...

A method for arranging an array of nested objects based on the objects' names

Recently, I received a complex object from an API: let curr = { "base_currency_code": "EUR", "base_currency_name": "Euro", "amount": "10.0000", "updated_date": "2024 ...

How to use jQuery to dynamically set the value of a column span in a

Struggling with setting the value for a table column span. Here is my jQuery code: $('tr td data-th=Name span').val('My span content...'); This is how my HTML appears: <tr> <td data-th="Name"><span class="edit-inpu ...

Example when a specific $scope.apply() is needed:

I am currently delving into the world of Angular and experimenting with different ways to learn how it functions. One of my projects involves creating a simple application where users can add new entries through an HTML interface, store them in a SQLite da ...

Autocomplete feature in JQuery-UI that automatically selects the word when there is only one choice remaining

My web form includes a text input field with an autocomplete feature implemented through jquery-ui. The form is actually contained within a jquery-ui dialog as well. I am searching for a solution to automatically accept the auto-complete choice when there ...

Encounter an error while attempting to store and retrieve an array of JavaScript objects in localStorage and parsing the data

I'm dealing with an array of JavaScript objects, like this: var objectList = [{phone: true},{name: 'room'}]. My goal is to store this array in localStorage, retrieve it later, and continue working with the objects it contains. Here is what ...

Create a "load additional data" button

I'm working on building my blog and I've run into a roadblock while trying to implement a load more button. Here's what I currently have: actions: { LOAD_ARTICLE_LIST: function ({ commit }) { axios.get('someurl/articles') ...

Setting cookies with NextJS Route API post middleware

@ Using NextJS 13.3 with App Dir and API Routes. I am currently working on implementing an authentication system using NextJS with my external NodeJS backend. The process involves the frontend sending credentials to the backend, which validates them and r ...

show button after the page has finished loading

I have a button similar to this: <input type="submit" id="product_197_submit_button" class="wpsc_buy_button" name="Buy" value="Add To Cart"> However, I am encountering an issue where if the user clicks the button before all scripts are loaded, an e ...

Ensuring the vue carousel component stops rendering once all of its data has been displayed

Is it possible to stop rendering a vue carousel component once its data is displayed or after a certain amount of time? I've searched for information on this, but haven't found anything relevant. Despite it being an unusual request, I still want ...

The input data from the MySQL database requires two page refreshes in order to be retrieved

Every time I input new values into the form fields and submit them, the page needs to be reloaded twice to display the updated values. For example, consider the $balance_2 in the 7th line. This line calculates the sum of all row values and updates the Bal ...

Can anyone provide a simple example of PDF.js that allows for text selection in a minimalist design?

Currently experimenting with PDF.js. An issue I'm facing is that the text selection feature is not supported in the Hello World demo. It simply renders everything onto a canvas without displaying the text layer. On the other hand, the official PDF.js ...

CORS headers present but AJAX request still fails

A request sent via AJAX from a locally hosted page to a remote server is encountering errors, despite the presence of CORS headers. The JavaScript code for this request is as follows: $.ajax({url: 'http://prox.tum.lt/420663719182/test-upload?Action=S ...

Invoke the C# function in the code-behind using an AJAX call

I have been attempting to encrypt two variables and return them as query strings using a code-behind function, but I have not been successful. This is my first time trying Ajax. Here is the method in my code-behind: [WebMethod] public static string Encri ...

Having trouble with the installation of react-burger-menu

While attempting to install the react-burger-menu library on a React project using npm install react-burger --save, I encountered an error that is preventing me from progressing with my project: npm ERR! While resolving: MYPROJECT npm ERR! Found: [email&# ...