Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

detecting a string value entered into a integer field

Status
Not open for further replies.

dexter195

Programmer
Jun 18, 2003
220
EU
hi,
im taking in an integer from the user and passing this to another jsp page and i was wondering how to detect if a user has entered a string or a double instead of an integer into this field.

ive tried using the pattern and matcher objects, but these dont work with integers.

ive also inserted the if statement, that checks to see if the user has entered a negative integer, into a try and catch but i still get the error:

Code:
org.apache.jasper.JasperException: For input string: "sd"

thanks for your help
 
Hi,

Below method checks for positive int and retuns a boolean. If its positive int it is ture else false.


Code:
 public static boolean isPositiveInt(String str) {
        if (str == null) {
            return false;
        }
        int size = str.length();
        // check if its empty
        if(size == 0)
          return false;

        for (int i = 0; i < size; i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        // check if positive or not
        if(Integer.parseInt(str) < 0)
          return false;

        return true;
    }

Cheers
Venu
 
This is a slightly shorter version:-
Code:
public static boolean isPositiveInt(String str) {
    if ( str == null )
        return false;
    
    try {
        if ( Integer.parseInt(str) >= 0 )
            return true;
    } catch (NumberFormatException ex){
        //do nothing
    }

    return false;
}


Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
cheers for the reply venu,

my appologies about the above post, i forgot to mention that the value entered by the user is passed to a bean as an integer.
when a character is entered into this integer field and the next page is opened up it spits up the error saying its not expecting a string to be entered.

is there a way to determine if the value stored in the bean is an integer when the second page accesses the bean?
or is this better solved using java script to determine if the user has entered an interger using the onChange="" method on the text box?
 
Hi,

It depends, if you want to do the validation on the client side use JavaScript or for server side validation you can use the above code to validate and see if its integer or not. But its better to have both client side as well as server side validations.

For Server side before passing it to bean
Code:
if(isPositiveInt(texfieldValue))
{
  bean.setIntValue(Integer.parseInt(texfieldValue);
}else{
  // forward to input page and display the error saying need to enter the integer value
}

For Client Side use JavaScript onSubmit of the form to validate the text value.

Cheers
Venu
 
cheers venur,

i got it working using javascript.

Code:
            function testInteger(field)
		{
						
			num = parseInt( field.value );

			if ( isNaN(num) )
				alert( "Please insert a positive number for the rules priority");
				
			else if ( num != parseFloat(field.value) )
				alert( "Please insert a positive number for the rules priority. This cannot have a decimal point" );
			
			else if( num <= 0 )
				alert( "Please insert a positive number for the rules priority"); 
		}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top