JAVASCRIPT: Convert text to string/int [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
is it possible to convert a text from an input to a string or integer?
I have an input field, in which you can search a company:
<input type="text" id="company" name="company" />
Now I need the value as a string or int.
Thanks for you help.

Value of the input field is a string by default. You can convert it to an int by
parseInt(document.getElementById("company").value,10);

It is Simple stuff. You can convert by using parseInt() Function.
It will convert your string to Integer.
function convert()
{
var x=document.getElemenyById("company").value;
x=parseInt(x);
}

jquery version:
parseInt($( "#company" ).val(), 10);
plain javascript:
parseIntdocument.getElementById('company').value, 10);

Related

Using regex with str.split JavaScript and parsing them separately [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
I want to be able to separate this one by one
Sephora26:theactor424#gmail.com:shpk335$A:what’s your name?:actor Dawg4075:dawg648#yahoo.com:Thoiland3:what’s your favorite pet?:cat Thrive65:evergreen45#hotmail.com:gcap1078!:what’s your favorite artist:Ed sheeran
I tried using JavaScript str.split() but it only parsed the first character (sephora26). What I want is to be able to parse the email and password out of it
Does it look like something you're looking for?
const strings = `Sephora26:theactor424#gmail.com:shpk335$A:what’s your name?:actor
Dawg4075:dawg648#yahoo.com:Thoiland3:what’s your favorite pet?:cat
hrive65:evergreen45#hotmail.com:gcap1078!:what’s your favorite artist:Ed sheeran`
const lines = strings.split('\n');
lines.forEach((line) => {
[name,email,password,question,answer] = line.split(":");
console.log(`name=${name}, email=${email}, password=${password}`);
});

Javascript parseFloat() change number [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a strange problem with JavaScript .
I have text value in a textbox '25000000' when i am
using parsefloat(txt.value), the returned value is 25000 !??
Why is 25000000 changed to 25000 ?
I think there is an alphabet somewhere in your numbers. Check the example below.There is an error in your code.
parseFloat(" 250000000 ") = 250000000
parseFloat("2018#geeksforgeeks") = 2018
parseFloat("geeksforgeeks#2018") = NaN
Thanks for all Responses.
The problem was for ',' in price.
Of course we replace ',' with '' ,but replace method
only change first one like "25,000,000,000"=>"25000,000,000".
I used str.split(',').join('') and problem solve and parsefloat returned correctly.
regards
ali

if current field value greater than in javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Trying to add a second condition to a js function
original condition
if (content.length==elmnt.maxLength)
new condition
if (content.length==elmnt.maxLength && current form field value > 1400)
how do I code "current form field value > 1400" properly in javascript?
Full original code:
<SCRIPT TYPE="text/javascript">
function jumpField(elmnt,content)
{
if (content.length==elmnt.maxLength)
{
next=elmnt.tabIndex
if (next<document.forms[0].elements.length)
{
document.forms[0].elements[next].focus()
}
}
}
</SCRIPT>
Thanks!
Not sure if I fully understand what you are trying to achieve.
Are you looking for parseInt() to convert a string value? Like:
parseInt(document.forms[0].elements[next].value) > 1400

getting all true keys from object using angular and/or underscore [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have an object in Angular looking like this:
$scope.addEmployeeDepartments={ 5066549580791808=true, 6192449487634432=true, 7192449487634432=false}
How do I generate a comma-separated string like this
var ids="5066549580791808, 6192449487634432"
containing all the keys which are true ?
Btw, I am using underscore.js in other parts of my solution, so I don't know if that makes it easier.
thanks
Thomas
You can do this with reduce in one pass:
var collect_trues_in = function(a, v, k) {
if(v)
a.push(k);
return a;
};
var csv = _($scope.addEmployeeDepartments).reduce(collect_trues_in, [ ]).join(', ');
Demo: http://jsfiddle.net/ambiguous/6CEZW/

How to validate in Jquery? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to jQuery. I have a text field and I want to validate this field so that it only takes character and alphanumeric values, however if you put in a numeric value it will show a message. How should I implement this?
$('#textfieldid').on('change', function(event) {
var text = $('#textfieldid').text();
//some validation code here
});
By the way, JQuery is a javascript library, not Java (which you have tagged).
Using regex, validations can be done.
var password = document.getElementB;
var letter = /[a-zA-Z]/;
var number = /[0-9]/;
if(number.test(password) && letter.test(password)){
//success
}else{
//error
}
Here password is the value which is returned from the field

Categories