Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doing a Integer.parseInt within javascript in a JSP

 
Moined Mogul
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is it that you can change within javascript in a JSP a value you get using something like this:
document.Check.validate.value
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.
PLEASE HELP....
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Author
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
See http://developer.netscape.com/docs/manuals/js/client/jsref/toplev.htm#1064173
S
 
Moined Mogul
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PLEASE HELP!!!
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;
}
}
</script>
<tr>
<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;
try
{
m_Statement = m_Connection.createStatement();
m_Results= m_Statement.executeQuery (m_strBarcode);
while (m_Results.next())
{
vResourceList.addElement(new String(m_Results.getString("scancode")));
}
m_Statement.close();
}
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
vResourceList.toArray(alResourceList);
//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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
try using
var a=parseInt(document.Check.Barcode.value,10)
var b=parseInt(document.Check.largestBarcode.value,10)
this should work!!
bye
shri
 
koushik konar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U can try out this
if(Integer.parseInt(document.Check.Barcode.value.trim()) == 2000)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic