Validation of md-datepicker and md-select in Angular MaterialAnguar Material validation

I am working on a form that includes input fields, datepickers, and dropdowns. Currently, if the required input fields are left blank upon submission, they are highlighted with a red line. However, I want the datepickers and dropdowns to also be highlighted if not selected.

Here is the code snippet:


  <div class="input-section-title">Information</div>

  <div layout="row"
       layout-align="start start">

    <md-datepicker id="date"
                   ng-model="vm.submissionDate"
                   md-placeholder="Date*"
                   required></md-datepicker>

    <md-input-container class="form-input-container padded-input md-block"
                        flex-gt-sm="">
      <label>Type*</label>
      <md-select id="information-type"
                 ng-model="vm.type"
                 required>
        <md-option ng-repeat="type in vm.dropdowns.types"
                   value="{{type}}">
          {{type}}
        </md-option>
      </md-select>
    </md-input-container>

    <md-input-container class="form-input-container"
                        flex="15">
      <label>NVRA*</label>
      <md-select id="information-nlba"
                 ng-model="vm.code"
                 required>
        <md-option ng-repeat="code in vm.dropdowns.codes"
                   value="{{code}}">
          {{code}}
        </md-option>
      </md-select>
    </md-input-container>

  </div>

  <div class="input-section-title">Personal Information</div>

  <div layout="row" layout-align="start start">

    <md-input-container class="form-input-container" flex>
      <label>Last Name*</label>
      <input id="personal-information-last-name"
             ng-model="vm.vitals.last" required>
    </md-input-container>

    <md-input-container class="form-input-container padded-input" flex>
      <label>First Name*</label>
      <input id="personal-information-first-name"
             ng-model="vm.vitals.first" required>
    </md-input-container>

    <md-input-container class="form-input-container padded-input" flex>
      <label>Middle Name</label>
      <input id="personal-information-middle-name"
             ng-model="vm.vitals.middle">
    </md-input-container>

    <md-input-container class="form-input-container" flex="15">
      <label>Suffix</label>
      <md-select id="personal-information-suffix"
                 ng-model="vm.vitals.suffix">
        <md-option ng-repeat="suffix in vm.dropdowns.suffixes"
               value="{{suffix}}">
          {{suffix}}
        </md-option>
      </md-select>
    </md-input-container>

  </div>

Answer №1

To utilize ng-messages with the Datepicker directive, you can include the following code snippet:

<md-datepicker id="date"
               name='date'
               ng-model="vm.submissionDate"
               md-placeholder="Date*"
               required>
</md-datepicker>
<div class="errors" ng-messages="newForm.date.$error">
    <div ng-message="required">Required</div>
</div>

The same approach can be taken for the select element as well.

<md-input-container class="form-input-container padded-input md-block"
                    flex-gt-sm="">
  <label>Type*</label>
  <md-select id="information-type"
             name="type"
             ng-model="vm.type"
             required>
    <md-option ng-repeat="type in vm.dropdowns.types"
               value="{{type}}">
      {{type}}
    </md-option>
  </md-select>
   <div class="errors" ng-messages="newForm.type.$error">
    <div ng-message="required">Required</div>
</div>
</md-input-container>

In order for validation to function correctly, it is important to enclose your elements within a form tag, assign names using the name attribute, and utilize those names for validation purposes across different elements.

If you're looking for a visual representation, you can view this CodePen example (please note that the UI may not be perfect): http://codepen.io/next1/pen/xVOMQB

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

Using NodeJS to perform asynchronous tasks with setImmediate while also incorporating private class

Today marks my first time experimenting with setImmediate. I've come to realize that it may not be able to run private class methods. Can someone shed some light on this? Why is that the case? Not Functioning Properly When trying to use a private cl ...

Issue with camera boundaries in Three.js when using EffectComposer with an orthogonal camera is currently presenting inaccuracies

In reference to a previous question on Three.js, I have successfully created a scene with a "minimap" using an orthogonal camera rendering into a viewport. The minimap is displayed properly in the standard renderer. Now, I wanted to add postprocessing eff ...

Updating a slider based on the values of two other sliders can be achieved by implementing a

I am working on a project that involves three input sliders. I need the third slider to display the product of the values from the first two sliders. Additionally, I want the value of the third slider to update dynamically whenever the values of the first ...

JavaScript asynchronous problem

When developing a simple Javascript function to validate user input for City and State, it encounters an issue with asynchronous behavior. The function checks if the user has entered values in the specified input fields and then proceeds to geocode the l ...

What is the significance of using a double arrow function in Javascript?

Can someone explain the double arrow notation used in the code snippet below? How does the second arrow function get executed if the first one's response is true? And in what scenarios is this notation typically used? async check({ commit }) { ...

I am looking to incorporate a dropdown feature using Javascript into the web page of my Django project

According to the data type of the selected column in the first dropdown, the values displayed in the columns of the second dropdown should match those listed in the JavaScript dictionary below, please note: {{col.1}} provides details on the SQL column data ...

Having issues with importing momentjs by reference in TypeScript with amd configuration

I'm puzzled by the difference in behavior between these two snippets: import * as moment from "../Typings/moment"; One works, while this one doesn't: /// <reference path="../Typings/moment.d.ts" /> import * as moment from "moment"; It t ...

Tips for emphasizing specific sections of text in CodeMirror utilizing substring positions

I am currently utilizing CodeMirror () as a text editor with additional functionalities. One of these features includes highlighting specific words or groups of words based on their positions within the original string. I have an external structure that st ...

The Data Table experiences intermittent hanging issues (Table is empty) when sorting or loading data with Knockout binding

While working on a project, I encountered an issue with binding data to a table using Knockout JS and the JQuery/Bootstrap based; Data Table API. The problem was that the table would become unresponsive at times when sorted or loaded, without any errors be ...

What is the best way to incorporate Express following an asynchronous function?

After setting up my Firebase and initializing it, I managed to retrieve data from it. However, I encountered an issue when trying to use express.get() and json the data into the server. I'm unsure of what the problem could be. let initializeApp = requ ...

Include a sub-component N times within the main component, depending on the current state value

I need assistance in adding a child component called ColorBox to the parent component named ColorBoxContainer based on the value stored in state as noOfBoxes: 16. I've tried using a for-loop but it seems like my code is incorrect. Can someone guide me ...

Reactjs and Isotope offer the ability to expand and collapse on click. In this unique feature, only one item can be expanded at

Currently, I am working on refining an isotope application where each item expands upon clicking it and collapses when another item is clicked. However, the issue I am facing is that multiple cells can be opened simultaneously. I am looking for the most ef ...

Adjustable Cursor - modify size while in motion at high speeds

I've designed a unique custom cursor and I am looking to enhance its functionality by adjusting its scale dynamically during fast movements. Similar to the cursor on this website: Example.com Here is my custom cursor for reference: CodeSandBox What ...

Attempting to activate cookies, however receiving a message indicating that cookies are not enabled

When trying to log in to a page using request in my node.js server, I set 'jar' to true like this: var request = require('request'); request = request.defaults({jar: true}); After that, I make a post request with the login details: r ...

What is the best way to ensure the constant rotation speed of this simple cube demo?

Currently delving into the world of Three.js. I'm curious about how to make the cube in this demo rotate at a consistent speed rather than depending on mouse interactions. Any tips on achieving this? ...

Unable to retrieve the updated array value in NodeJs when accessed outside the function scope

I recently started working with node and I'm attempting to retrieve the Twitter id of a group of users. The module takes an array of screen names, iterates over them to obtain the userId, and then adds them to an array. However, I'm facing an iss ...

Incorporate a hyperlink into a React Material-UI DataGrid

While utilizing the DataGrid component from Material-UI, I am trying to add a link to the end of each row. However, the output is currently displaying as: ( [object Object] ). https://i.stack.imgur.com/2k3q2.png I would like for it to show the record ID, ...

Locate a specific sequence of characters within an array of objects using JavaScript

I am working with an array of objects, where each object contains a string value. My task is to search for a specific substring within the string. [ { "link": "https://www.sec.gov/Archives/edgar/data/1702510/000170251022000084/00 ...

JavaScript Regular Expression Assistance Domain Snatcher

I am in the process of developing a JavaScript Regex and I am struggling to find the right pattern to match a specific domain. Let me provide an example for better understanding. I need a regex that can identify any domain that exclusively contains (text. ...

Is there a way to update the data on a view in Angular 9 without the need to manually refresh the page?

Currently, I am storing information in the SessionStorage and attempting to display it in my view. However, there seems to be a timing issue where the HTML rendering happens faster than the asynchronous storage saving process. To better illustrate this com ...