Multiple angularjs validations applied to a single textbox

Hello, I am currently working on an angularjs application where I have a textbox with a custom directive attached to it. This directive only accepts numbers within a certain range, for example between 100 to 200. The validation for this is working perfectly. However, in addition to this range validation, I also want a required field validator to be applied when the form is submitted. I need to ensure that only one validation message is displayed at a time. Below is the code snippet for my textbox:

<div class="inputblock" ng-class="{ 'has-error' : ((form5.$submitted && form5.rangeNumber.$invalid )|| (form5.rangeNumber.$invalid && form5.rangeNumber.$dirty))}">
    <label class="inputblock-label">{{'Down Payment' | translate}}</label>
    <div>
        <span class="ang-error" style="color:#fff" ng-show="form5.rangeNumber.$dirty && form5.rangeNumber.$invalid">
                                     <span ng-show="!(form5.$submitted && form5.rangeNumber.$error.required)&&form5.rangeNumber.$invalid && form5.rangeNumber.$dirty">
                                         {{ 'Value should be between' | translate }} {{min}} {{'and' | translate}} {{max}}
                                     </span>
        </span>
    </div>
    <input class="" type="text" name="rangeNumber" ng-attr-placeholder="{{ 'DownPayment' }}" ng-model="DownPayment" range-number="range" required>
</div>

https://i.sstatic.net/sWMTZ.png Can this be implemented successfully?

Answer №1

Below are some tips for handling directive errors:

   <span ng-show="form5.rangeNumber.$invalid && form5.rangeNumber.$dirty">
        {{ 'Value should be between' | translate }} {{min}} {{'and' | translate}} {{max}}</span>
       </span>

Dealing with the required error when submitting the form:

<span ng-show="form5.$submitted && form5.rangeNumber.$error.required">
          Required*
</span>

Avoiding both errors from showing up simultaneously:

   <span ng-show="!(form5.$submitted && form5.rangeNumber.$error.required) && form5.rangeNumber.$invalid && form5.rangeNumber.$dirty">
        {{ 'Value should be between' | translate }} {{min}} {{'and' | translate}} {{max}}</span>
       </span>

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

Can anyone provide a solution for determining the number of active intervals in Javascript?

Similar Question: How to View All Timeouts and Intervals in JavaScript? I've been working on an HTML5 game that includes a lot of graphical effects using intervals created by the setInterval function. However, I've noticed that my game is ru ...

iOS fixed positioning and scrolling

One of the current challenges we are facing involves implementing an action button similar to the one found in GMail for adding new content. This button needs to remain fixed during scroll. Here is the HTML code: <div class="addButton actionButton" on ...

Using onChange input causes the component to re-render multiple times

As I delve into learning React, I've encountered some challenges. Despite thinking that I grasped the concept of controlled components, it appears that there's a misunderstanding on my end. The issue arises after an onChange event where I notice ...

Learn how to creatively style buttons with dynamic effects using tailwindcss

My Desired Button: I have a Button component that can accept a variant prop. My goal is to have the button's className change dynamically based on the prop passed to it. Instead of using if/else statements for different buttons, I want to use a sing ...

Leveraging jQuery to target elements with a minimum of two child elements

How can I use jQuery to specifically target .sortable <ul> elements that have 2 or more children (<li> elements)? Is there a way for jQuery to select only <ul> elements with at least 2 <li> children? Thanks in advance! The current ...

Nuxt - Vue - Utilizing middleware on a layout in a few simple steps

Recently, I developed a middleware for authentication in my Nuxt application and now I want to utilize it within a layout. However, when trying to call the middleware using the following code: export default { middleware: 'auth', I encounte ...

Refreshing the Date Display with AJAX Response

Within a view, there is a DisplayFor connected to a DateTime field in the Model. After an AJAX call returns a Date to update the field, I am able to convert the AJAX date into a MM/DD/YYYY format. However, using .val to set the DisplayFor does not reflect ...

Using Javascript to read a JSON string displayed in a URL

I'm working on developing a straightforward web application that extracts latitude and longitude data stored in a JSON format and places markers on a Google map based on this information. Currently, there is a program running on a server that generate ...

Tips for implementing a loop within the file input change event

Hello everyone, I need assistance with the code below. function fileValidation() { var fileInput = document.getElementById('filech'); var filePath = fileInput.value; var allowedExtensions = /(\.jpg|\.jpeg|\.png|\.gif)$ ...

Using SignalR for lengthy processes: transmitting progress updates

Utilizing SignalR to initiate lengthy computations on the server and notify the client when the results are ready. The input bindings consist of an HTTP request. Desire to send multiple messages to update the client on various stages of the process (e.g., ...

The query in the Mongo shell is not functioning as expected when used with mongoose

I have crafted a shell query that functions flawlessly when executed on mongo shell, but does not yield any results when run using mongoose in nodejs + typescript; Mongo shell db.userworks.aggregate([ { $match: { user: ObjectId("6 ...

Invoke the parent's function within the Local-Registration component of VueJs

I have a Vue object: var app = new Vue({ el: '#my-id', data() { return { example: 1 } }, methods: { exampleMethos(data) { console.log('data', data); } }, ...

What features come with a Themeforest template?

Searching for ways to enhance my UI using an HTML/CSS template. Currently utilizing AngularJS with Bootstrap and have some custom functions integrated into my web/mobile app. I am in need of professional-looking html/css that can easily be copied and paste ...

Determine the mean values to showcase at the center of a D3 donut graph

Check out this plunkr where I'm utilizing angularjs and d3.js. In the plunkr, I've created several donut charts. I'm interested in learning how to display the average data in the center of the arc instead of the percentage. Additionally, I& ...

How to verify the existence of a YouTube video ID using JavaScript

In this code snippet, I am using the video ID 'TOwd30wXc-0' from YouTube: $.ajax({ url: "http://gdata.youtube.com/feeds/api/videos/"+realurl+"?v=2&alt=json-in-script", dataType: "jsonp", contentType: "application/json ...

Is the Google Maps Javascript API compatible with Ionic 2?

Hi everyone, I hope you're all doing well :) In my development of a basic application using Ionic Framework with Google Maps (Map + Places SearchBox), everything is working smoothly until I encounter an issue when trying to search for an address (e.g ...

Solving the issue of jump in vertical motion after a jQuery slideDown animation

Although I am new to Jquery, I managed to create a simple vertical accordion. It fulfills my requirements, but I noticed a slight jerk at the end of the slide down animation. If someone could take a look and provide a solution, it would save me from losin ...

Looking for consistent vertical and horizontal scrolling behavior in a React project

I am in need of assistance as I lack the necessary expertise in ReactJs to transform this design. Currently, it is written in Jquery but I would like guidance on how to recreate it in ReactJs. Specifically, I want the left div with the topic for each row t ...

Material UI - Array of Chips

I have been working on creating a ReactJS component that displays an array of chips with unique styling for each one. To achieve this, I created individual makeStyles classes for the chips. However, I encountered difficulties in dynamically changing the cl ...

Challenges with implementing singleSelect feature in MUI-X DataGrid

Encountering an issue with the singleSelect type on the community version of x-data-grid. The problem arises when attempting to edit a row, where my singleSelect consists of the following data set. Here is how I have configured my DataGrid setup. Although ...