Error validation not working in AngularJS - javascript

I am doing error validation in Angular as follows.
<select ng-model = "$parent.color" class = "form-control"
ng-options = "color as color for color in $parent.colors" required>
<option value="">Choose an option</option>
</select>
<span ng-show="serviceForm.$parent.color.$error.required"> My custom error message </span>
The error validation message never shows up. How to fix this?

Did you include the ngRequire module ?
ngRequire
<script>
angular.module('ngRequiredExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.required = true;
}]);
</script>
<div ng-controller="ExampleController">
<form name="form">
<label for="required">Toggle required: </label>
<input type="checkbox" ng-model="required" id="required" />
<br>
<label for="input">This input must be filled if `required` is true:</label>
<input type="text" ng-model="model" id="input" name="input" ng-required="required" /> <br>
<hr>
required error set? = <code>{{form.input.$error.required}}</code><br>
model = <code>{{model}}</code>
Try this if it works.

Your code can be like to this to work properly.
<form name="form1">
<select name="select" ng-model = "$parent.color" class = "form-control"
ng-options = "color as color for color in $parent.colors" required>
<option value="">Choose an option</option>
</select>
<span ng-show="form1.select.$error.required"> My custom error message </span>
</form>
You are not flowing FormController. View this link

Related

Select option change the text of a Textarea with JavaScript

I need help. I have this form:
<form name="user_cause" method="post">
<div class="form-group">
<select id="subject" required="required" class="form-control" onchange="showHide()">
<option value="subject">Subject</option>
<option value="test_1">Test_1</option>
<option value="test_2">Test_2</option>
</select>
</div>
<div class="form-group">
<textarea id="subject_message" required="required"
class="form-control" rows="5" disabled>
</textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
I wish I could change the text in the disabled textarea according to the choice. how to do it with javascript?
var ourArea = document.getElementById('subject_message');
var selectSubject = document.getElementById('subject');
ourArea.textContent = selectSubject.value; // omit this if you don't want initial value to be reflected in the text area
function showHide(){
ourArea.textContent = selectSubject.value;
}
Using jQuery, $("#subject").change(function(e) { $("#subject_message").val($(this).val())});
A jQuery way:
$("#subject").change (function() {
$("#subject_message").text($("#subject option:selected").text();
});

Change form action based on form select field

I can't get the form action to change based on a user's drop-down selection. What I want to do is have the form redirect to a different page, then the default for the form if a user selects "Student Loans" from the select field named "agency".
In other words, what I want to do is that if someone select "student loans" for the agency, I want the page to submit to /quote/quotes-sl.php instead of quote/quotes.php (the default of the form).
Here is my form code:
<form id="tdhcustom-pre-1-form" accept-charset="UTF-8" onsubmit="submit_function(this)" action="/quote/quotes.php" method="post" name="tdhcustom-pre-1-form">
<input id="edit-lead-source-description" name="lead_source_description" type="hidden" value="test" />
<label class="edit-owed" for="edit-owed"> Amount Owed: <span class="form-required" title="This field is required.">*</span>
</label><select id="owed" class="form-select required" name="owed">
<option selected="selected" value="">Select...</option>
<option value="$10,000 to $14,999">$10,000 to $14,999</option>
<option value="$15,000+">$15,000+</option>
</select>
<label class="edit-agency" for="edit-agency"> Problem With: <span class="form-required" title="This field is required.">*</span>
</label><select id="agency" class="form-select required" name="agency">
<option selected="selected" value="">Select Agency...</option>
<option value="Student Loan" data-action="/quote/quotes-sl.php">Student Loan</option>
<option value="FEDERAL">Federal Taxes</option>
<option value="STATE">State Taxes</option>
</select></div>
<input id="edit-submit" class="form-submit" height="31" name="submit" src="test.com/test.jpg" type="image" value="Submit" width="214" />
<input id="form-5ezy7kqpVIYFiVUgKIyxbp4n6MQ7ZqHuo33GJbq0QZE" name="form_build_id" type="hidden" value="form-5ezy7kqpVIYFiVUgKIyxbp4n6MQ7ZqHuo33GJbq0QZE" />
<input id="edit-tdhcustom-pre-1-form" name="form_id" type="hidden" value="tdhcustom_pre_1_form" />
</form>
Here is the javascript:
<script>
function submit_function(form) {
var selected = document.getElementById('Agency');
var dataset = selected[selected.selectedIndex].dataset;
if (dataset.action) {
form.action = dataset.action;
}
return true;
};
</script>
Add trigger on selected option val = "Student Loan" , then change attribute "action" form to url "/quote/quotes-sl.php"
$('#agency').on('change',function(){
var selected = $(this).val();
if(selected == "Student Loan")
$('#tdhcustom-pre-1-form').prop('action',"/quote/quotes-sl.php");
});
CMIIW :D
You want to change the form action based on an onchange event from your <select> ... but you'll also want to remove the onsubmit attribute from your form, like so:
<form id="tdhcustom-pre-1-form" accept-charset="UTF-8" action="/quote/quotes.php" method="post" name="tdhcustom-pre-1-form">
Assuming you're going to have data attributes on all the options like this:
<select id="agency" class="form-select required" name="agency">
<option selected="selected" value="">Select Agency...</option>
<option value="Student Loan" data-action="/quote/quotes-sl.php">Student Loan</option>
<option value="FEDERAL" data-action="/quote/quotes-s2.php">Federal Taxes</option>
<option value="STATE" data-action="/quote/quotes-s3.php">State Taxes</option>
</select>
You can just replace your <script> block with:
<script>
(function() {
var theForm = document.getElementById('tdhcustom-pre-1-form');
var theSelector = document.getElementById('agency');
theSelector.onchange = function() {
theForm.action = theSelector[theSelector.selectedIndex].getAttribute('data-action');
}
})();
</script>
That's pure JavaScript - so without jQuery (or any other framework).

Drop down menu changes email in form

<FORM METHOD="POST" ACTION="mailto:mariamcsorley#hotmail.com">
<p> Name: <INPUT TYPE="text" NAME= "name" SIZE="30"> </p>
<p> Email: <INPUT TYPE="text" NAME= "email" SIZE="30"> </p>
<p> Phone: <INPUT TYPE="text" NAME= "telephone" SIZE="30"> </p>
<p> Practice: <SELECT NAME= "practice" SIZE="1">
<OPTION SELECTED>*Select Practice*
<OPTION>Dungannon
<OPTION>Cookstown
<OPTION>Coalisland
<OPTION>Portnaglone
<OPTION>Aughnacloy
</SELECT> </p>
<p> Species: <SELECT NAME= "species" SIZE="1">
<OPTION SELECTED>*Select Species*
<OPTION>Small Animal
<OPTION>Equine
<OPTION>Farm
<OPTION>Pig
<OPTION>Poultry
</SELECT> </p>
<p> Preferred Date: <SELECT NAME= "day" SIZE="1">
<OPTION SELECTED>*Day* <OPTION>01 <OPTION>02 <OPTION>03 <OPTION>04 <OPTION>05 <OPTION>06 <OPTION>07 <OPTION>08 <OPTION>09 <OPTION>10 <OPTION>11 <OPTION>12 <OPTION>13 <OPTION>14 <OPTION>15 <OPTION>16 <OPTION>17 <OPTION>18 <OPTION>19 <OPTION>20 <OPTION>21 <OPTION>22 <OPTION>23 <OPTION>24 <OPTION>25 <OPTION>26 <OPTION>27 <OPTION>28 <OPTION>29 <OPTION>30 <OPTION>31 </SELECT>
<SELECT NAME="month" SIZE="1">
<OPTION SELECTED>*Month* <OPTION>Jan <OPTION>Feb <OPTION>Mar <OPTION>Apr <OPTION>May <OPTION>Jun <OPTION>Jul <OPTION>Aug <OPTION>Sep <OPTION>Oct <OPTION>Nov <OPTION>Dec </SELECT>
<SELECT NAME="year" SIZE="1">
<OPTION SELECTED>*Year* <OPTION>2016 <OPTION>2017 <OPTION>2018 <OPTION>2019 <OPTION>2020 <OPTION>2021 <OPTION>2022 <OPTION>2023 <OPTION>2024 <OPTION>2025 <OPTION>2026 <OPTION>2027 </SELECT> </p>
<p> Preferred Time: <SELECT NAME="hour" SIZE="1">
<OPTION SELECTED>*Hour* <OPTION>08 <OPTION>09 <OPTION>10 <OPTION>11 <OPTION>12 <OPTION>13 <OPTION>14 <OPTION>15 <OPTION>16 <OPTION>17 <OPTION>18 <OPTION>19 <OPTION>20 </SELECT>
<SELECT NAME="minute" SIZE="1">
<OPTION SELECTED>*Minute* <OPTION>00 <OPTION>15 <OPTION>30 <OPTION>45 </SELECT> </p>
<p> Details: <TEXTAREA NAME="details" ROWS=6 COLS=40>
</TEXTAREA> </p>
<INPUT TYPE="submit">
</form>
Hi there, Is there any way at all that I can use php or a function in order to change the email as the practice is changed. For example when Dungannon is selected from the practice drop down menu that dungannon#dungannon.com is emailed and when Coalisland is selected it emails the form to coalisland#coalisland.com?
I know nothing about this and so have been trying to figure it out and googling solutions but I cant seem to get anything to work - any help is appreciated greatly... thank you!
Change your html code
<INPUT TYPE="text" id="email" NAME= "email" SIZE="30"> <br>
and
Practice: <SELECT NAME= "practice" SIZE="1"onChange="getData(this);">
<OPTION SELECTED>*Select Practice*
<OPTION>Dungannon
<OPTION>Cookstown
<OPTION>Coalisland
<OPTION>Portnaglone
<OPTION>Aughnacloy
</SELECT>
Javascirpt code
function getData(title)
{
var selectedText = title.options[title.selectedIndex].text
var emailValue="";
if(selectedText=="Dungannon")
{
emailValue = "dungannon#dungannon.co.uk";
}
else if(selectedText=="Cookstown"){
emailValue= "cookstown#cookstown.co.uk";
}
document.getElementById("email").value = emailValue;
}
Here is working codepen
To set the email address in your form element, modify the html to include the onchange event handler as below and then add the javascript below.
<p> practice: <select name="practice" onchange='setemail(this.value)'>
<option selected hidden='true'>*select practice*
<option value='dungannon'>dungannon
<option value='cookstown'>cookstown
<option value='coalisland'>coalisland
<option value='portnaglone'>portnaglone
<option value='aughnacloy'>aughnacloy
</select> </p>
<script>
function setemail(value){
/* to change form action */
document.querySelectorAll('form[name="frmhot"]')[0].setAttribute('action','mailto:'+value+'#'+value+'.co.uk' );
/* to set value of text field called `email` */
document.querySelectorAll('form[name="frmhot"] input[type="text"][name="email"]')[0].value=value+'#'+value+'.com';
}
</script>
I notice that the form method has been changed to your email address - I'm not 100% sure that this will work as you expect but if you wish to change the form action instead of / or as well as changing the value of the email field in the form then a slight modification to the javascript function is required.
Setting the form action as a mailto will most likely invoke the mail client on the client computer ( not sure how it would work on a mobile ) - is that the intended method of operation or is it supposed to send the email directly?
From #OP:
It's for an app so I have one page of code to do this on so would have to put the tag in if I was to do a function. I am clueless but when you click submit I want it to be able to change to email the practice.. am i explaining this correctly?

How can I hide form if first form is not validated properly

JavaScript:
function myFunction() { //creating the onlick function
var locationFrom = document.getElementById("LocationFrom").value;// Getting both "from" and "to" Ids fromt he homepage.hmtl
var locationTo = document.getElementById("LocationTo").value;
if (locationFrom === locationTo) { // If both have same values then give an error as below
document.getElementById("errorLocationFrom").textContent = 'Destination cannot be the same.';
}
else {
document.getElementById("errorLocationFrom").textContent = ''; // if not the same then give no error
}
$("#Submit").click(function(){
$("#Form-2").show(); //Display form when Submit button is clicked
});
function SecForm(){
var ErrorLocation = document.getElementById("errorLocationFrom");
if(ErrorLocation == false) //Hide form2 if ErrorLocation is false
$("#Form-2").hide();
return false;
}
HTML:
<div class="full-col">
<label for="FDestination">From</label> <!---Label for select element--->
<select name="Location" id = "LocationFrom"> <!---Select element to give user options to select from-->
<option value="" disabled selected>Please Select </option> <!---Options for departing location-->
<option value="Newport">Newport</option>
<option value="Mahdi">London</option>
<option value="Cardiff">Cardiff</option>
<option value="Cilo">Brazil </option>
</select>
<label for="FDestination">To</label>
<select name="LocationTo" id = "LocationTo" >
<option value="" disabled selected>Please Select </option>
<option value="Cardiff">Cardiff</option>
<option value="Mahdi">London</option>
<option value="Newport">Newport</option>
<option value="Cilo">Brazil</option>
</select>
<!---Hidden Error message only shown when there is a validation error for departing and arrival--->
<label id="errorLocationFrom"></label>
</form>
<Form action="#" class="group" name="form2" id="Form-2" method="post" hidden = "true">
<legend id="fixed"><span class="number">2</span>Tickets</legend>
<div class="half-col">
<label for="Adult-ticket" class="center-label">Adults(+16)</label>
<input type="number" id="adult" name="user_adult">
<label id="AdTickError"></label>
</div>
<div class="half-col">
<label for="child-ticket" class="center-label">Child</label>
<input type="number" id="child" name="user_child">
<label id="ChildTickError"></label>
</div>
<input type="checkbox" id="Standard" name="Type" value="Standard">
<label class="light" for="Standard">Standard</label><br>
<input type="checkbox" id="First-Class" name="Type" value="First-Class">
<label class="light" for="First-Class">First Class</label><br><br>
<p id="total-cost"></p>
<button type = "button" value="checkout" id="checkoutbtn" onclick="AdultNumber(); calculateFare(); " >CHECKOUT</button>
</Form>
I am making a railway ticketing system and what I need, is the form to do is first be validated then after clicking the Submit button it will display the other form both forms are on the same page.
You could change the submit of form1 to input type button. When button1 is clicked you do your validation as before and if it is passed you show the form2.
In form2 you could use the onsubmit event to copy the fields you need from form1 to form2; <form id="Form-2" onsubmit="CopyData()">
In copy function you can do:
$('#Form-2').append( '<input type="hidden" name="LocationFrom" value="' + $('#LocationFrom').val() + '">' );
$('#Form-2').append( '<input type="hidden" name="LocationTo" value="' + $('#LocationTo').val() + '">' );
UPDATE - fiddle: https://jsfiddle.net/blocknotes/copyuwyd/

AngularJS Dropdown required validation

Following is my code snippet. I want to validate my dropdown using angular.
<td align="left" width="52%">
<span class="requiredSmall">*</span>
<select class="Sitedropdown" style="width: 220px;"
ng-model="selectedSpecimen().serviceID"
ng-options="service.ServiceID as service.ServiceName for service in services">
<option value="" ng-selected="selected">Select Service</option>
</select>
</td>
Valid means:
Valid values can be anything but "Select Service", it is my default value. Like other ASP.net Require field validator DefaultValue="0" for dropdown, so here My dropdown will be bound from the services and I want to select all other values except "Select Service".
You need to add a name attribute to your dropdown list, then you need to add a required attribute, and then you can reference the error using myForm.[input name].$error.required:
HTML:
<form name="myForm" ng-controller="Ctrl" ng-submit="save(myForm)" novalidate>
<input type="text" name="txtServiceName" ng-model="ServiceName" required>
<span ng-show="myForm.txtServiceName.$error.required">Enter Service Name</span>
<br/>
<select name="service_id" class="Sitedropdown" style="width: 220px;"
ng-model="ServiceID"
ng-options="service.ServiceID as service.ServiceName for service in services"
required>
<option value="">Select Service</option>
</select>
<span ng-show="myForm.service_id.$error.required">Select service</span>
</form>
Controller:
function Ctrl($scope) {
$scope.services = [
{ServiceID: 1, ServiceName: 'Service1'},
{ServiceID: 2, ServiceName: 'Service2'},
{ServiceID: 3, ServiceName: 'Service3'}
];
$scope.save = function(myForm) {
console.log('Selected Value: '+ myForm.service_id.$modelValue);
alert('Data Saved! without validate');
};
}
Here's a working plunker.

Categories