AngularJS iu.mask is a powerful tool for validating user input, especially when

Currently, I am learning Angular and have created an input field with type 'text' to display a date in the format "99/99/9999" using ui.mask. I have also implemented validation in the module to prevent submission (unblocking button) if the entered date is incorrect, such as 00/00/0000 or 12/12/1700. However, even when the date is invalid, the input field does not show a red frame to indicate the error. How can I modify it to display a red frame based on the module's validation?

HTML
<input
    id="dob"
    type="text"
    class="form-control   cell-height  form-input"
    ng-model="createAccount.dob"
    ui-mask="99/99/9999"
    placeholder="D.O.B. (mm/dd/yyyy)"
    required/>

CONTROLLER
 var validateDob = function () {
        try {
            var date = moment.utc($scope.createAccount.dob, "MM/DD/YYYY");
            if ($scope.patient == null) $scope.patient = {};
            if (!date.utc().isValid()) return false;
            if (date.utc().date() == 0 || date.utc().year() == 0) return false;
            if (date.utc().isAfter(moment().utc())) return false;
            if (date.utc().isSame(moment().utc())) return false;
            if (!date.utc().isAfter(moment.utc().subtract(150, 'years'))) {
                return false;
            }
            $scope.createAccount.dateOfBirth = date.utc();//.format("YYYY/MM/DD");
            return true;

        }
        catch (err) {
            return false;
        }
    };

Answer №1

I appreciate the guidance provided by the angular documentation on creating custom validations using directives instead of controllers.

If the validator detects a false response, angular will apply an ng-invalid class to your DOM Object, giving you the flexibility to customize the border color or style of the invalid input field.

Answer №2

One simpler approach is to use ng-class to verify if validateDob returns false, and if so, display the validation border.

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

Error encountered with Ajax client-side framework while executing HTML code

When I run my project from Visual Studio using an aspx page that utilizes ajax modal popup extender, everything works fine with IE and Firefox as the default browsers. However, when I create an HTML file containing the code and open it by double-clicking, ...

Combining Rxjs map and filter to extract countries and their corresponding states from a JSON dataset

I have a unique dataset in JSON format that includes information about countries and states. For example: { "countries": [ { "id": 1, "name": "United States" }, { "id": 2, "name": "India" }], "states": [ { ...

The form failed to be submitted even after undergoing ajax validation

Although this question has been asked many times before, I am still confused about where I went wrong. My goal is to validate a form using ajax and submit it if everything checks out. Below is the code snippet that I have been working with: ...

Ways to reach component method via router

When making an Ajax request and displaying the data in a table component, I encounter an issue where I need to extract the clicked data for use in another Ajax request within a different component that is called through React-Router. The challenge lies in ...

Tips for distinguishing between DOM elements using jQuery

I have a piece of code that iterates through each row in a table and generates a json object. The rows can contain one of the two elements below: <input type="text" id="myelem"/> or <p id="myelem">foo</p> It's worth noting that b ...

Returning a Response in HapiJS Routes from Incoming Requests

Currently, I am facing an issue with the request module where I am able to successfully make a request and receive a response. However, I am struggling to figure out how to pass this response back to my route in order to return it to my client. Here is th ...

Ways to iterate over $firebaseArray

After creating an array of users using the following code - var ref = new Firebase(FIREBASE_URL + '/users'); var users = $firebaseArray(ref); I populated this array with objects and now I need to iterate through each user in the controller. Wh ...

What is the method for binding context on a click event in Vue?

Can't access context function on click in Vue Example HTML with click function: <li v-on:click="itemClick($event, this)"></li> My Vue.js code snippet: var app = new Vue({ el: '#mainApp', methods: { ite ...

Understanding the application of JSON data can be simplified by following these

I am looking to manipulate dates by either adding or subtracting them, but I am unsure of how to extract the dates from the other data present. var fetch = require('node-fetch'); fetch('https://api.nasa.gov/planetary/earth/assets?lon=100.7 ...

The autocomplete feature in Codemirror seems to be failing specifically when writing JavaScript code

I am having trouble implementing the autocomplete feature in my JavaScript code editor using Codemirror. Can someone please help me figure out what I'm doing wrong? Here is the snippet of code : var editor = CodeMirror.fromTextArea(myTextarea, { ...

Enhancing Images: A guide to updating uploaded images using jQuery File Upload

Currently, I have implemented jQuery File Upload for PHP and it seems to be functioning effectively. However, I am facing a slight issue where if I open two tabs of the same page (index.html) in my browser and upload a photo on tab 1, the uploaded photo is ...

Arrange the menu items in a column layout based on a given condition

Can someone assist me with displaying the menu subitems? I have created a plunker. Take a look at it to understand what I need (open plunker in full screen) https://plnkr.co/edit/IMEJFPfl5kavKvnUYaRy?p=preview In the plunker above, there are two dropdown ...

Unable to reset session with JavaScript on JSP page

Created a session from the login.jsp page using a servlet String msg = ""; HttpSession sess = request.getSession(); // if(sess != null) //sess.invalidate(); if (sess.getId() != null) { sess.setAttribute("uname", ...

Discovering the Vue instance within Vuex

In the Vue.js project's main.js file, I have defined a global variable. Vue.prototype.$API = "myapihere" I am able to access this variable from anywhere in my project using this.$API. However, when trying to access it in Vuex, it is returning as un ...

Encountering an issue with executing Google API Geocode in JavaScript

I'm having trouble printing an address on the console log. Every time I run the code, I encounter an error message that reads: { "error_message" : "Invalid request. Missing the 'address', 'components', 'latlng' or &ap ...

Arranging an upgrade within a task management app

I'm currently in the process of developing a task management app with AngularJS, Node, Express, and MongoDB. Everything seems to be falling into place except for the update functionality. I'm struggling with implementing this feature, especially ...

Address NPM vulnerabilities through manual fixes

I downloaded a repository and ran an npm install, but encountered an error at the end. Now, every time I run npm audit, I receive the following message: found 18 vulnerabilities (5 low, 12 moderate, 1 high) in 15548 scanned packages 9 vulnerabilities requ ...

Access various results from a jQuery function

Is there a way to efficiently extract the values of petKeys and employeeKey using the jQuery functions provided below? var whenSelectDateFromCalendar = function () { initKeyValues(); petKeys = ? employeeKey = ? }; var initKeyValues = function ...

Shifting Icon to the Right within the Drawer Navigator Toolbar

While modifying the example code for Material UI's drawer navigator, I decided to enhance it by adding a notification icon and a checkout icon with the Admin Panel typography in the toolbar. However, I encountered an issue where the checkout icon app ...

Adding and Removing Attributes from Elements in AngularJS: A Step-by-Step Guide

<input name="name" type="text" ng-model="numbers" mandatory> Is there a way to dynamically remove and add the "mandatory" class in Angular JS? Please note that "mandatory" is a custom class that I have implemented. Thank you. ...