Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Angular JS form

Posted on 2016-11-07
8
Medium Priority
?
89 Views
Last Modified: 2016-11-14
Hi,   I am new to Angular but am trying to learn!
I have developed a simple form to capture user contact details.  

The form Submit button should only work when all the required fields of the form are validated but it does not work.

I'm hoping that someone can point me in the right direction.

Here's a picture of the form:

Form
Here's my code:

  <ng-form name="frmEm" class="row" ng-if="IsEmail">
        <div class=" col-lg-6 col-md-6">
            <h3>Send Result as Email</h3>

            <div class="form-group">
                <select class="form-control" required name="title" ng-model="returnValues.title">
                    <option value="">Select</option>
                    <option value="">Mr</option>
                    <option value="">Mrs</option>
                    <option value="">Miss</option>
                    <option value="">Dr</option>
                </select>
            </div>

            <div class="form-group">
                <input class="form-control" placeholder="Firstname" type="text" required name="firstname" ng-model="returnValues.firstname" />
            </div>

            <div class="form-group">
                <input class="form-control" placeholder="Lastname" type="text" required name="lastname" ng-model="returnValues.Lastname" />
            </div>

            <div class="form-group">
                <input class="form-control" placeholder="Company name" type="text" name="companyName" ng-model="returnValues.companyName" />
            </div>

            <div class="form-group">
                <input class="form-control" placeholder="Enter Email Address" type="email" required name="em" ng-model="returnValues.Email" />
            </div>

            <div class="form-group">
                <input class="form-control" placeholder="Telephone Number" type="tel" required name="TelNo" ng-model="returnValues.TelephoneNo" />
            </div>

            <div class="form-group">
                <input class="form-control" placeholder="Address 1 (optional)" type="text" name="Address1" ng-model="returnValues.Address1" />
            </div>
            <div class="form-group">
                <input class="form-control" placeholder="Address 2 (optional)" type="text" name="Address2" ng-model="returnValues.Address2" />
            </div>
            <div class="form-group">
                <input class="form-control" placeholder="Address 3 (optional)" type="text" name="Address3" ng-model="returnValues.Address3" />
            </div>
            <div class="form-group">
                <input class="form-control" placeholder="Postcode (optional)" type="text" name="Postcode" ng-model="returnValues.Postcode" />
            </div>

            <div class="form-group">
                <input class="form-control" placeholder="Are you a robot?  What is 8+1?" type="text" name="Robot" ng-model="returnValues.Robot" />
            </div>

            <div class="form-group">
           <input type="checkbox" name="UserInfo" ng-model="returnValues.UserInfo" /> I agree to the  
                
                <a href="" data-toggle="tooltip" title="Metsa Wood Terms Of Service go here.">
                   Metsa Wood terms of service.
                </a>
            </div>
</div>
        
        <div class="col-lg-12">
            <input ng-disabled="frmEm.$invalid" type="button" class="btn btn-success" ng-click="SendEmail()" name=" name" value="Send" />
            <img style=" width: 34px !important;" class="" ng-if="IsLoadingEmail" src="~/Content/loader.gif" />
        </div>

    </ng-form> 
</div>

Open in new window

0
Comment
Question by:SmashAndGrab
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 59

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 41877119
The problem is you have a required on your <select> but none of the options have values

You want to change this
<option value="">Select</option>
<option value="">Mr</option>
<option value="">Mrs</option>
<option value="">Miss</option>
<option value="">Dr</option>

Open in new window

To something like this
<option value="">Select</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Miss">Miss</option>
<option value="Dr">Dr</option>

Open in new window

Without values that control is ending up being empty irrespective of what you select so the form will be invalid.
1
 

Author Comment

by:SmashAndGrab
ID: 41877218
*slaps hand on forehead*   School girl error!!
0
 

Author Comment

by:SmashAndGrab
ID: 41877257
May I also ask - is there a fancy Angular way of showing which of the inputs are required?

something like..

example
0
Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

 

Author Comment

by:SmashAndGrab
ID: 41877260
I can obvioulsy add an image but was wondering if Angular had anythong special ups its sleeve?
0
 

Author Comment

by:SmashAndGrab
ID: 41877753
If you get a chance - could you look at my other question also?

https://www.experts-exchange.com/questions/28981499/Show-confirmation-alert-in-Jquery.html
0
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 41878196
(Thought this was posted already)
You can create a directive to prepend code to the front of them.

modulename.directive("required", function() {
   return {
       restrict: 'A', 
       compile: function(element) {
           element.prepend('<span class="required-field">*</span>');
       }
   }
}

Open in new window


However, if you know your required fields up front might as well add the code yourself when you create the form - can't see any benefit to doing it with a directive.

The input / select are both accessible via CSS as well
0
 

Author Comment

by:SmashAndGrab
ID: 41878313
Thanks.

Youre probably right - I might as well do it with the form layout.

I thought there was a clever way of doing it as the controls have the "REQUIRED" property.

 <input class="form-control" placeholder="Firstname" type="text" required name="firstname" ng-model="returnValues.firstname" />
0
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 41878323
Meant the last line to say

The input / select are available via CSS like so

input:required, select:required {
}

Open in new window


Angular is a framework for creating applications - interactions like form styling etc you need to add yourself. One option is to just have your required boxes a different colour rather than putting an asterisk there.
0

Featured Post

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This video teaches users how to migrate an existing Wordpress website to a new domain.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question