How to validate a number entered in the edittext? - android

12,034

Solution 1

You can do something like that:

final int value = Integer.valueOf(a.getText().toString());  
if (value < 15 || value > 25) {  
    // do what you want  
}

Solution 2

Parse the value to Integer or Float and do your validation.

Integer.parseInt(a.getText())

Solution 3

If you know that the value entered is a number you can first parse it using either:
int val = Integer.parseInt(a.getText());
or if it is a float:
float val = Float.parseFloat(a.getText());.

Then you can just do a comparison: if( val < min || val > max ) //show message;

Share:
12,034
kumareloaded
Author by

kumareloaded

Amateur android application developer. Now getting my hands dirty on AngularJS.

Updated on June 05, 2022

Comments

  • kumareloaded
    kumareloaded almost 2 years

    well am able to validate an edittext when its empty using the below code

    EditText a = (EditText) findViewById(R.id.edittext1); 
    
    if ((a.getText().toString().equals(""))
    {
        Toast.makeText(getApplicationContext(), "value is empty", 0).show();
    }   
    

    the edittext input type is set as number.

    now i want to validate the number entered.

    for example the range of number to be entered should be between 15 to 25.

    if the number is below 15 or above 25 it should show Out of Range

    help me out! :)