Validating input in Angular UI Bootstrap Datepicker

I have integrated a datepicker from angular-ui/bootstrap. However, I am facing an issue where the user can freely type in the input field instead of selecting a date.

Here is a snippet of my code:

session.js

  session.getDateFormat = function() {
            return 'yyyy-MM-dd';
        };

        session.formatDate = function(date) {
            return $filter('date')(date, session.getDateFormat());
        };

Controller.js

vm.dateFormat = session.getDateFormat();
function submit() {
date : session.formatDate(vm.data.date)
 }

The code related to date handling is more extensive, but this is the gist of it. Also, below is the HTML snippet:

<input type="text" class="form-control" uib-datepicker-popup="{{vm.dateFormat}}" placeholder="{{vm.dateFormat}}" ng-model="vm.data.date" is-open="vm.dateOpened" ng-required="true" ng-disabled="disabled" />

Admins have the option to choose date formats like yyyy-mm-dd or dd-mm-yyyy.

My requirements are:

  1. Verify that the user inputs a date in the correct format before submission, clearing the field automatically and displaying an error message with the correct format as a hint.
  2. Restrict users from entering non-numeric characters.
  3. Avoid using hardcoded HTML regex and instead check if the input matches the selected format.
  4. Exclude the use of JQuery in the project.
  5. If you are aware of any other datepicker compatible with Angular and Bootstrap without JQuery, please suggest it.

Answer №1

After updating to angular-ui 1.3.3, I made some adjustments. I assigned the name 'datepicker' to the date input and 'frm' to the form. Additionally, I added the following code snippet:

<div ng-if="!frm.datepicker.$error.required"">
<div class="datepicker-error" ng-if="frm.datepicker.$invalid">
some error message
</div></div>

I chose not to clear the field immediately as it may be considered invalid if the user hasn't finished typing. If you have a better solution, please share it with me.

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

Tips for managing numerous Axios requests in JavaScript

I have a scenario where I need to send 3 axios calls simultaneously from the frontend to the backend using the axios.all function to modify a MONGO DB database. The challenge I am facing is ensuring that if one of the requests fails, none of the other requ ...

Numerous Controllers in AngularApp failing to function properly

One issue I'm encountering is that only one out of the two controllers within my app seems to be registered: var app = angular.module('MyModule', []); app.controller('FruitCtrl', function($scope) { $scope.fruits = ['appl ...

Create a binding between a keyboard shortcut and a button's click event in Angular

I am working on a div that has an ng-click event: <div class="modalWindowClose" ng-click="closeSettings(user, users);"><i class="fa fa-times"></i></div> I am trying to trigger a click on this element when the escape key is pressed ...

Steps for implementing a conditional rendering in your codeHere is a

I've encountered an issue while attempting to implement conditional rendering. The error I'm getting is Element implicitly has an 'any' type because expression of type 'number' can't be used to index type 'types&apos ...

When invoking the jQuery ".load('pageName')" method, HTML pages are not loaded from the application cache

I have been working on incorporating the html5 offline caching functionality into our html pages, and everything seems to be running smoothly with jQuery version 1.4. However, I encountered a problem when I upgraded to jQuery 1.8. Specifically, issues aro ...

Sorting nested objects in AngularJS using a customized `orderBy` function

I'm struggling to sort my array of objects by a nested property called validUntil. Can anyone guide me on how to achieve this? I'm currently using angular along with ng-repeat for iterating over the objects. It would be helpful to implement a cu ...

Having trouble retrieving the HTML content after deploying my application on Heroku

I encountered an issue with my index.js file in Express Node.js once I deployed the app on Heroku. In production, I'm getting an error that seems to indicate that the server is unable to find my index.html file. *-src ---index.html ---index.js http ...

Can you explain the role of the %GetOptimizationStatus function within a JavaScript file executing in Node.js?

Currently, I am delving into an article that discusses optimization strategies and includes the following code snippet: //Function that contains the pattern to be inspected (using an `eval` statement) function exampleFunction() { return 3; eval(&a ...

The Next JS build process exclusively creates server-side pages

I have noticed that some of my pages like /contact, /about, and /rules are server-side generated even though I am not using getServerSideProps(). Since these pages only contain static images and text without any API requests, I want them to be treated as s ...

Excluding node modules when not included in tsconfig

Within my Angular project, there is a single tsconfig file that stands alone without extending any other tsconfigs or including any additional properties. Towards the end of the file, we have the following snippet: "angularCompilerOptions": { ...

Using Angular 10 to make an HTTP POST request, with the goal of appending a string

Whenever I try to send a post request to an api endpoint, I keep encountering an error with status code 500. name: "HttpErrorResponse" ok: false status: 500 statusText: "Internal Server Error" Below is the code I am using: var selected ...

Vite terminal not executing Npm commands

Here is what I entered: npm run dev Error: npm ERR! Missing script: "dev" npm ERR! npm ERR! To see a list of scripts, run: npm ERR! npm run npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\andre\AppData&bs ...

The notify.js fails to display notifications in the event of an error

Why am I not receiving any error notifications even when there is an error message in the response object? $.ajax(settings).done(function (response) { if ( "error_message" in response ) { console.log(response); $.notify(" ...

Delving into XFN data parsing using Jquery, part two

I need some assistance with correctly parsing the REL attribute on A tags. I have already figured out how to match most XFN values, but there are two that I'm struggling with: "co-worker" and "co-resident". The hyphen seems to be causing an issue with ...

The Vue/Nuxt application displays content duplication on each page, rendering the content twice without duplicating the components

I recently delved into Vue/Nuxt programming and worked through a tutorial on adding a blog, which I then customized for my website. Everything functions perfectly except that the content is rendering twice. It goes from rendering NavPage (component) > c ...

The jQuery slider's next button is not functioning as intended

jQuery('#slider-container').bjqs({ 'animation' : 'slide', 'width' : 1060, 'height' : 500, 'showControls' : false, 'centerMarkers' : false, animationDuration: 500, rotationS ...

Associating models using a many-to-many relationship in Sequelize unexpectedly restricts the ability to modify attributes in the join table

I am in the process of incorporating a voting feature into my app, allowing users to vote on responses from other users. The backend of my Node app is built using Express and Sequelize. Currently, I am utilizing a SQLite database for testing convenience. ...

Looking for a seamless way to convert jsp code to html using sightly in AEM 6.1?

I need help transforming JSP code to HTML using Sightly in AEM. In the JSP code, we have a scriptlet that stores a value from the dialog into a JSP variable called "coltype" using the code pageContext.setAttribute("coltype", xssAPI.filterHTML(properties. ...

In AngularJS, what is the best way to add a filter inside another filter?

Struggling to properly unit test a filter that relies on another filter. Despite attempts, I have been unsuccessful in mocking the filter. While I've come across methods for mocking filters in controllers and testing them in isolation, this particular ...

I encountered a problem while attempting to update the browser URL using pushState() in my Angular application

Whenever I try to change the browser URL while a bootstrap modal is open in my AngularJS application, I utilize the pushState() function. This does successfully change the URL, but unfortunately, it results in the following error being thrown: Uncaught ...