Angular JS - Instead of sum values are getting concatenated - javascript

I'm new to AngularJS, just created a simple form in order to understand. I tried multiplying 2 input values, I'm good here, but when I use same code to sum those 2 input values it is getting concatenated instead of sum.
My code:
<div ng-app ng-init="fval = 1;sval = 2">
<div>
First Value:
</div>
<div>
<input type="text" ng-model="fval" />
</div>
<br />
<div>
Second Value:
</div>
<div>
<input type="text" ng-model="sval" />
</div>
<br />
<div>
<label id="lblResult">{{fval * sval}}</label>
</div>
Here I have given hardcoded values for my inputs, initially we will get result as 6. Also when we change the inputs we will get correct result for multiplying 2 values.
I changed my code for addtion as below:
<label id="lblResult">{{fval + sval}}</label>
After running the application I got the correct value as 3, but when I change my input values I'm getting concatenated values.
Like if I change my text box values, for firstTextBox = 12 & secondTextBox = 3, then I'm getting result value as '123'.
Hence, I'm landing with correct value when I run the application first time, but changing inputs on client side is concatenating.
Sorry for my English, since it is not my first language. Can anyone please help me where I'm going wrong.

Try Changing
<input type="text" ng-model="fval" />
To
<input type="number" ng-model="fval" />

That happens because the type of the ng-model is declared as text.
<input type="text" ng-model="fval" />
<input type="text" ng-model="sval" />
So when you add them using {{fval + sval}} you get a string since the sum of two string is the result of concationation of these two strings.
In order for them to work as expected you should replace them like below:
<input type="number" ng-model="fval" />
<input type="number" ng-model="sval" />
Hope this saves your time.

This is just a JavaScript thing. Your numbers are strings, and + is the concatenation operator. One way to solve this:
parseInt(fval) + parseInt(sval)
Edit: This is not allowed within Angular expressions (see below). Answer is valid for use in 'normal' JS code though.

Related

Angular only putting attribute of ng-minlength if parsed value is not nil

I want to only set my attribute of ng-minlength when the value I parse from data.Validation['minlength'] is not nil.
First I attempted using ng-switch; I had no problem doing this when handling ng-show=true/false. However I was unable to get it working when it was beyond just the value but also the whole declaration of ng-minlength="...". Second attempt was using ng-if but again I was unable to get it working in the "middle" of the input.
Below is some code that works - I want the whole ng-minlength="data.Validation['minlength']" to only be set if the value is not nil.
<input type="text" name="foo" ng-model="item.foo"
ng-minlength="data.Validation['minlength']" required/>
I discovered a simpler approach in another question of mine.
Solution by Karaxuna here.
<input type="text" name="foo" ng-minlength="myvar || 0" required/>
You can try ng-switch-on
<span ng-switch on="data.Validation>
<input ng-switch-when="null" type="text" name="foo" ng-model="item.foo" ng-minlength="data.Validation['minlength']" required/>
<input ng-switch-default type="text" name="foo" ng-model="item.foo" required/>
</span>
It will create input depending on data.Validation values
However I am not sure if it will work with NULL values. It is still a good way forward.

An AngularJS directive to validate a field when a non-zero sum has been entered into another

I have two form fields - an employee count and a gross payroll amount.
<div ng-repeat="payroll in report.payrolls">
<input type="number" min="0" class="input-mini" sister-value="{{payroll.grossPayrollAmount}}" ng-model="payroll.employeeCount" type="text">
<input ng-blur="payroll.grossPayrollAmount = Math.round(payroll.grossPayrollAmount)" type="number" min="0" class="input-small" ng-model="payroll.grossPayrollAmount" type="text">
</div>
The user does not have to enter a non-zero value for either. HOWEVER, if they enter a non-zero value for one then they must do so for the other, and that is what I want to validate against.
These fields repeat in sets - so a pair for each payroll, so I'm not sure if getting them by an ID or class will work.
I've written a few custom validation directives before, but never one that checks for a value in another, related field.
You could try to make a directive surrounding both inputs:
<directive>
<input type="number" min="0" ng-model="payroll.employeeCount" type="text">
<input ng-blur="payroll.grossPayrollAmount = Math.round(payroll.grossPayrollAmount)" type="number" min="0" class="input-small" ng-model="payroll.grossPayrollAmount" type="text">
</directive>
So both values will be available from it, you can access them with element.find().
You use element.find() just like you would use jQuery. For example, setting an id="employeeCount" to the first input, you can access it with element.find('#employeeCount'), so you get the first input element, finally you access to its value with element.find('#employeeCount').val().
note: if you're not used to jQuery, remember to add the dot (.) before a class name and a hash (#) before an id name.
Here, you have a list of methods that you can use with any element, including "find()": https://docs.angularjs.org/api/ng/function/angular.element
Update:
This will also work for multiple pairs of inputs, you just have to repeat the directive tag, for example:
<directive>
<input [...] >
<input [...] >
</directive>
<directive>
<input [...] >
<input [...] >
</directive>
The directive will work independently for each instance you create.

jQuery or JS: Select input fields with 2 level brackets?

I have a dynamic form that adds users to the site, made so you can duplicate the fields to add several users on one go.
So, my looks like
<input name="user[1][name]" value="" />
<input name="user[1][username]" value="" />
<input name="user[1][password]" value="" />
And then the number is changed on the duplicated fields, eg:
<input name="user[2][name]" value="" />
<input name="user[2][username]" value="" />
<input name="user[2][password]" value="" />
and so on.
On PHP I can handle each user since it has it's own array.
But I would like to validate, for example, the username on each user via jQuery.
The closest I got to is
$(this).find('input[name="user[][username]"]').each(function() {
But for it to work I need to explicitly write the number on the first [], eg:
$(this).find('input[name="user[1][username]"]').each(function() {
Is there a way to select ALL of them? I tried putting * and * between the [] but it didn't work.
Thanks for your help!
You can use the ends with selector
You could use a for loop and not have to write the numbers yourself really easily:
var number_of_forms = 3
for(var i=1;i<=number_of_forms;i++){
$(this).find('input[name="user[' + i + '][username]"]').each(function() {
}

three text input into one POST name

If I have three text input and I want to combine the values in these three text input into one POST name, how can I do that?
UPDATE:
a good example would be if I have a phone number field, and I have three fields for the phone number... I wanted this to be posted as one so back in the server side I can just access it as $POST['phone']
Would be nice if something like jQuery can help me out here.
Have them as an array:
<input type="text" name="inputs[]" />
<input type="text" name="inputs[]" />
<input type="text" name="inputs[]" />
Then you can access them in the POST array.
You have not indicated the programming language, but in PHP it would be, $_POST['inputs'][0], $_POST['inputs'][1], $_POST['inputs'][2]...
Since you want to have only one phone input which contains the full phone number appear on server side, not parts of it, and you are using jQuery in your project, this will make things easy for you:
1. Sample Markup
<form id="my_form" method="post">
<input type="text" name="phones[]" />
<input type="text" name="phones[]" />
<input type="text" name="phones[]" />
<input type="hidden" name="phone" />
<input type="submit" name="send" value="Send It" />
</form>
2. jQuery
$(document).ready(function(){
var $phones = $('#my_form input[name="phones[]"]'),
$phone = $('#my_form input[name="phone"]');
$('#my_form').submit(function(){
// join all the phone parts together
var phone_number = '';
$phones.each(function(){
phone_number += this.value;
});
// change the hidden input element's value
$phone.val(phone_number);
// remove the phone parts input elements
$phones.remove();
});
});
I wonder why you would need to do that.
In php, you can do as Shef said.
In simple single word cases, you can concatenate them with some separator (e.g. # or $) and process the same on the server side.

bean.getValue() only pulling in part of the value not entire value

I am currently using a javaBean to save/load values to and from a jsp. Here what is happening:
I have the following line in my jsp:
<td><input class="partDescription" name="partDescription" style="width: 250px" type="text" readonly="readonly" value=<%=affectedBean.getPartDescription().get(i) %> /></td>
When I debug through the code this is value stored in the affectedBean.getPartDescription at index 0: BK SLOPE CONTROLLER
However, when it the page finishes loading the only thing it shows is in that location is: BK
Other examples:
Value: ROLF REDESIGNED UL SUPPLY; Displays: ROLF
Value: 18 FUEL SENSOR; Displays: 18
Anybody have any ideas
In HTML elements, spaces are a special separator character for HTML element attributes like type, value, etc.
When printing attribute values without doublequotes like follows
<input type="text" value=<%=affectedBean.getPartDescription().get(i) %> />
then the generated HTML ends up as
<input type="text" value=BK SLOPE CONTROLLER />
All parts beyond the space are been interpreted as another attributes. A bit intelligent syntax highlighter should already have turned on some light above your head. It's colored differently!
So, you need to put quotes around the value
<input type="text" value="<%=affectedBean.getPartDescription().get(i) %>" />
so that the generated HTML ends up as
<input type="text" value="BK SLOPE CONTROLLER" />
Unrelated to the concrete problem, using scriptlets is discouraged, you should prefer using taglibs/EL. Assuming that you've put the affectedBean as attribute of the request, do so:
<c:forEach items="${affectedBean.partDescription}" var="partDescription">
<input type="text" value="${partDescription}" />
</c:forEach>

Categories

Resources