File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes doing a Integer.parseInt within javascript in a JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "doing a Integer.parseInt within javascript in a JSP" Watch "doing a Integer.parseInt within javascript in a JSP" New topic

doing a Integer.parseInt within javascript in a JSP

Moined Mogul
Ranch Hand

Joined: Jul 11, 2001
Posts: 33
How is it that you can change within javascript in a JSP a value you get using something like this:
From a string to an integer. I have tried using the Integer.parseInt(document...)
This gives me a runtime error stating that Integer is not defined in the javascript I am trying to run.
Thank you!
if(Integer.parseInt(document.Check.Barcode.value) == 2000)
This is the line exactly that gives me the error.....says that Integer is not defined....
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

It's been some time since I coded Javascript, but Integer.parseInt(xxx) is a *Java* object, not a Javascript one.

If you want to check for numeric in javascript use isNaN(varName) and if you want to check if it's equal to a numeric value, just do a string comparison.
Sam Dalton
Ranch Hand

Joined: Jul 26, 2001
Posts: 170
Integer.parseInt has not relation to JavaScript. It is a Java function.
You can use the JavaScript function parseInt to do the same thing however.

<a href="" target="_blank" rel="nofollow">Sam Dalton</a>,<br />Co-author of []Professional JSP 2.0[/URL] (October 2003)<br />Co-author of <a href="" target="_blank" rel="nofollow">Professional SCWCD Certification</a><br />Co-author of <a href="" target="_blank" rel="nofollow">Professional Java Servlets 2.3</a>
Moined Mogul
Ranch Hand

Joined: Jul 11, 2001
Posts: 33
This is the code in my JSP that I am trying to get to work:
<script language="JavaScript1.2">
var a = parseInt(document.Check.Barcode.value);
var b = parseInt(document.Check.largestBarcode.value);
if(a > b)
alert("Sorry, you must enter a barcode that is greater than the one you just entered");
return false;
<input type="hidden" name="largestBarcode" value="<%=(adminFunctions.lastBarcode(libraryBook))%>">
This is my bean method that gets the largest number form my database:
public synchronized int lastBarcode (Resource resource)
Vector vResourceList = new Vector();
String[] alResourceList = null;
int nArraySize = 0;
double fValue = 0;
Double tempDouble;
int nBarcode = 0;
m_Statement = m_Connection.createStatement();
m_Results= m_Statement.executeQuery (m_strBarcode);
while (
vResourceList.addElement(new String(m_Results.getString("scancode")));
catch (SQLException queryFails)
System.out.println("Exception retrieving resource list " + queryFails.getMessage());
alResourceList = new String[vResourceList.size()];
//Moving into array to use in JSP page
//Getting the size of the array
nArraySize = (alResourceList.length);
tempDouble = Double.valueOf(alResourceList[nArraySize-1]) ;
fValue = tempDouble.doubleValue() ;
nBarcode = (int)fValue;
return (nBarcode);

Moined Mogul
Ranch Hand

Joined: Jul 11, 2001
Posts: 33
With this code I am always getting prompted to add a number that is great than the one I entered. Even if i enter 1000000 and the largest number in the database is 76.
shri ganesh

Joined: Jul 24, 2001
Posts: 4
try using
var a=parseInt(document.Check.Barcode.value,10)
var b=parseInt(document.Check.largestBarcode.value,10)
this should work!!
koushik konar

Joined: Jul 29, 2001
Posts: 3
U can try out this
if(Integer.parseInt(document.Check.Barcode.value.trim()) == 2000)
I agree. Here's the link:
subject: doing a Integer.parseInt within javascript in a JSP
It's not a secret anymore!