View Full Version : Using parseInt() to convert textbox strings
Millywoo
10-16-2002, 11:33 AM
I am starting to learn JavaScript, and am having a problem with parseInt() and intergrating it into a very basic addition calculator. Although, using eval() would be more efficient, it is a requirement on my course that i create one calulator with parseInt() and one with eval().
Well the exercise says, use parseInt() to ensure a number has been entered, which i've assumed means it needs to convert text box string into a number, so that 1 + 2 doesn't equal 12. Here's what i've written:
<!--
function addNumbers()
{
var number1 = document.form1.number1.value
var number2 = document.form1.number2.value
var total = alert("The answer is: " + number1 + number2);
}
-->
(where number1 and number2 are the name of the text boxes.)
This obviously adds the strings together instead of the numbers, so i tried:
function addNumbers()
{
var number1 = document.form1.number1.value
var number2 = document.form1.number2.value
int1 = parseInt(number1)
int2 = parseInt(number2)
var total = alert("The answer is: " + int1 + int2);
}
The only examples of parseInt() i've seen are either browser detection, or in a decimal conversion function.
Any help would be appreciated. And i hope my error isn't too basic, :o
Regards
:)
Bosko
10-16-2002, 11:57 AM
ParseInt returns the first found integer in the string,so "I am 12 years old." will return "12".
var int1 = parseInt(number1);
var int2 = parseInt(number2);
var int3=int1+int2;
var total = alert("The answer is: "+ int3);
Roelf
10-16-2002, 12:00 PM
Well the exercise says, use parseInt() to ensure a number has been entered.
if thats the case, then you should use parseInt like:
var number1 = document.form1.number1.value
if (parseInt(number1)==number1) {
// a integer number is entered
} else {
// some other characters are entered
// like 1a2 or something like that
}
Millywoo
10-16-2002, 12:41 PM
Originally posted by Roelf
Well the exercise says, use parseInt() to ensure a number has been entered.
if thats the case, then you should use parseInt like:
var number1 = document.form1.number1.value
if (parseInt(number1)==number1) {
// a integer number is entered
} else {
// some other characters are entered
// like 1a2 or something like that
}
So should that be exactly how it's typed or should their be some editing on that?
I've now got:
function addNumbers()
{
var number1 = document.form1.number1.value
if (parseInt(number1)==NaN || (parseInt(number1)=="");
{
alert("Please Enter a Number!");
}
var number2 = document.form1.number2.value
if (parseInt(number2)==Nan) || (parseInt(number2)=="");
{
alert("Please Enter a Number!");
}
}
Which isn't giving a result at all.
I'm trying to put that if it isn't a number that's entered, or nothing's entered into either text box (number1 or number2) then the alert will show, using Roelf's validity using parseInt theory.
Roelf
10-16-2002, 12:52 PM
function addNumbers() {
var number1 = document.form1.number1.value ;
if (parseInt(number1)==number1){
alert("Please Enter a Number!");
}
var number2 = document.form1.number2.value ;
if (parseInt(number2)==number2){
alert("Please Enter a Number!");
}
}
should work
Millywoo
10-16-2002, 01:45 PM
Now when i enter a number into the text boxes, "number1" and "number2", the alert window pops-up telling me to enter a number!
:confused:
Roelf
10-16-2002, 01:49 PM
right, == should be != (is not)
Millywoo
10-16-2002, 02:04 PM
That works when i type letters or nothing in the boxes, thank you.
Now i'm missing the:
var total = number1 + number2 ;
Are the text boxes still definded as that or are they now called parseInt(number1), parseInt(number2)?
Thank you for your patience :thumbsup:
Roelf
10-16-2002, 02:10 PM
Originally posted by Millywoo
Now i'm missing the:
var total = number1 + number2 ;
Are the text boxes still definded as that or are they now called parseInt(number1), parseInt(number2)?
When doing the calculation using parseInt, it should be:
var total = parseInt(number1) + parseInt(number2) ;
otherwise the variables will be handled as strings and are being concatenated in the result
Millywoo
10-16-2002, 02:21 PM
Thank you for your help, much appreciated...
One more thing..
When one box isn't a number, but the othe one is, i get the alert saying to "Enter a number", but i still get the result alert box which says the answer is NaN.
Should return false be put in so that it stops the function continuing, if so where?
Roelf
10-16-2002, 02:36 PM
then you should alert only if the result si a number, like
if (isNaN(result)){
// do nothing or
alert("any message you like");
} else {
alert(result);
}
BrainJar
10-16-2002, 03:57 PM
Another thing to remember when using parseInt: it takes two arguments. The second is the number base.
It can make a difference if you have an input string like "0123". parseInt will accept octal, decimal and hex strings. Octal strings start with "0" (zero) and hex strings start with "x". So
parseInt("0123")
returns 83, ("123" in octal), while
parseInt("123") and
parseInt("0123", 10)
return 123, as expected.
It's a good idea to always specify the base 10 argument so you don't get any surprises if a user types a leading zero.
mordred
10-16-2002, 06:21 PM
I'll second what BrainJar wrote. Always use the radix argument when employing parseInt(), and you won't have any strange behaviour. Consider also that if you feed "09" to a parseInt() without the radix, it won't get a number, since there is no "09" number in octal notation (same applies to "08").
I freely admit that I once had the pretty stupid idea to use parseInt() to validate if a date string entered could be used as a number... luckily, I found out about the radix.
so
parseInt("08", 10) rules. :)
glenngv
10-17-2002, 03:45 AM
Originally posted by Bosko
ParseInt returns the first found integer in the string,so "I am 12 years old." will return "12".
var int1 = parseInt(number1);
var int2 = parseInt(number2);
var int3=int1+int2;
var total = alert("The answer is: "+ int3);
just a correction on what Bosko said.
In parseInt, if the first non-whitespace character is not numeric, the function returns the Not-a-Number value NaN.
so in his example, since the first character is "I" which is not numeric, it will return NaN not 12.
for reference, see http://www.devguru.com/Technologies/ecmascript/quickref/parseint.html
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.