Question

i have an android program where i have successfully restricted the user to input only value after decimal.. the problem is if user inputs two decimal points like "1.." then the app crashes. i have allowed only one special character to be used i.e decimal point itself. so how can i restrict the user from entering two decimal points or else show some validation.

i need something like this

  else if(txtLdays.getText().toString().trim().equals(".."))
             {


                txtLdays.requestFocus();    
                txtLdays.setError("Double decimal ?");  

                 return false;
             }
Was it helpful?

Solution

Try this..

Use contains like below

else if(txtLdays.getText().toString().trim().contains(".."))
         {


            txtLdays.requestFocus();    
            txtLdays.setError("Double decimal ?");  

             return false;
         }

EDIT

String to double

double result = Double.parseDouble(txtLdays.getText().toString().trim());

int to double

double result = (double) 12;

If user enters 12.

then check endsWith

double result;
if(txtLdays.getText().toString().trim().endsWith("."))
   result = Double.parseDouble(txtLdays.getText().toString().trim().replace("\\.", ""))

OTHER TIPS

There are two possibilitites

  1. if you want to restrict when any button pressed.. then replace all ".." characters with empty chars like..

    String data=View.getText().toString().replaceAll("..", "");

2.if you restrict when the user is typing then write a TextWatcher listener for the Edittext and you need to peform some validations there...

Did you tried setting decimal input type ?

<EditText
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:inputType="numberDecimal|number"    
/>

Try this :

int count = StringUtils.countOccurrencesOf((txtLdays, ".");

if (count > 1){

    txtLdays.requestFocus();    
    txtLdays.setError("Double decimal ?");  


}

Please downlaod this jar file Commons Lang

The right way would be to match the value against a regular expression.

Here is an example that allows a string that starts/end with zero or more spaces and contains a double decimal point number:

//First make sure you avoid null pointer exception
if (txtLdays != null && txtLdays.getText() != null) {
    String daysString = txtLdays.getText().toString();
    String regex = "^\s*\d+\.\.\d+\s*$"

    if (daysString.matches(regex)) {
        txtLdays.requestFocus();    
        txtLdays.setError("Double decimal ?");  

        return false;
    }
}

You can youse some kind of online regular expression tool to verify your matcher.

equals will check Object. You need to check equalsIngnoreCase(); this ll check all the character specially u need

else if(txtLdays.getText().toString().trim().indexOf("..") != -1 ){
             txtLdays.requestFocus();    
             txtLdays.setError("Double decimal ?");  
             return false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top