Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9134541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:40:49+00:00 2026-06-17T08:40:49+00:00

I have the following form in an Angular partial: <form name=submit_entry_form id=submit_entry_form ng-submit=submit() ng-controller=SubmitEntryFormCtrl

  • 0

I have the following form in an Angular partial:

<form name="submit_entry_form" id="submit_entry_form" ng-submit="submit()" ng-controller="SubmitEntryFormCtrl" novalidate >
    <input type="text" name="first_name" ng-model="first_name" placeholder="First Name" required/><br />
    <input type="text" name="last_name" ng-model="last_name" placeholder="Last Name" required/><br />
    <input type="text" name="email" ng-model="email" placeholder="Email Address" required/><br />
    <input type="text" name="confirm_email" ng-model="confirm_email" placeholder="Confirm Email Address" required/><br />
    <span ng-show="submit_entry_form.$invalid">Error!</span>
    <input type="submit" id="submit" value="Submit" />
</form>

The trouble I’m having is with the span at the bottom that says “Error!”. I want this to show ONLY if one of the inputs is both “ng-dirty” and “ng-invalid”. As it is above, the error will show until the form is completely valid. The long solution would be to do something like:

<span ng-show="submit_entry_form.first_name.$dirty && submit_entry_form.first_name.$invalid || submit_entry_form.last_name.$dirty && submit_entry_form.last_name.$invalid || submit_entry_form.email.$dirty && submit_entry_form.email.$invalid || submit_entry_form.confirm_email.$dirty && submit_entry_form.confirm_email.$invalid">Error!</span>

Which is UGLY. Any better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:40:50+00:00Added an answer on June 17, 2026 at 8:40 am

    Method 1: Use a function on $scope set up by your controller.

    So with a better understanding of your problem, you wanted to show a message if any field on your form was both $invalid and $dirty…

    Add a controller method:

    app.controller('MainCtrl', function($scope) {
    
      $scope.anyDirtyAndInvalid = function (form){
        for(var prop in form) {
          if(form.hasOwnProperty(prop)) {
             if(form[prop].$dirty && form[prop].$invalid) {
               return true;
             }
          }
        }
        return false;
      };
    });
    

    and in your HTML:

    <span ng-show="anyDirtyAndInvalid(submit_entry_form);">Error!</span>
    

    Here is a plunker to demo it

    Now all of that said, if someone has entered data in your form, and it’s not complete, the form itself is invalid. So I’m not sure this is the best usability. But it should work.


    Method 2: Use a Filter! (recommended)

    I now recommend a filter for this sort of thing…

    The following filter does the same as the above, but it’s better practice for Angular, IMO. Also a plunk.

    app.filter('anyInvalidDirtyFields', function () {
      return function(form) {
        for(var prop in form) {
          if(form.hasOwnProperty(prop)) {
            if(form[prop].$invalid && form[prop].$dirty) {
              return true; 
            }
          }
        }
        return false;
      };
    });
    
    <span ng-show="submit_entry_form | anyInvalidDirtyFields">Error!</span>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following form: <form id=testForm action=country_Save> Country Name:<input type=text id=countryName /> <input type=submit
I have the following form: <input type=text name=name1 id=names1 class=names value= placeholder=1) Name Here
I have the following form: <label>One</label> Product ID:<input type=text name=productid[] value=> Product Quantity: <input
I have the following form input field: <input type=text value= title=Enter item description name=description>
I have the following form. <form name=cookieform id=login method=POST> <input type=text NAME=username id=username class=text
I have a following form: <form action=doThis.php?warehouse=12 method=post> <input name=field1 type=text /> <input name=field2
I have the following form: <input name=q value= class=qa-search-field> <input type=submit value=Search class=qa-search-button> I
I have the following form : <input name=linkColor type=text id=colorpickerField0 > <input name=linkColor type=text
I have following html form: <form method=post action=> <input type=hidden name=userid id=user value=<?php echo
I have the following form: <form action=download.php method=get> <input type=checkbox name=file1 /> File1 <br/>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.