Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a default value show in a Text View ,as i enter a number in an Edit Text i need a validation saying the Entered number shouldn't be greater than or equal to it, any help here?? So far I've used >= and > but didn't work ......

What I have tried:

Java
@Override
            public void onClick(View v) {
                if (amt.getText().toString().trim().isEmpty()){
                    Toast.makeText(myView.getContext(), "Enter Valid Amount", Toast.LENGTH_LONG).show();
                    //isValidDecimalNumber(amt);
                }
                else if ( (amt.getText().toString().trim().length() >=  txt1.getText().toString().trim().length())){

                }
                else {
                     tot = Double.parseDouble(amt.getText().toString());
                     tt=(tot / 100.0f) * 3+tot;
                    loadProducts2();
                }

            }
Posted
Updated 23-Oct-18 22:56pm
v4
Comments
David Crow 23-Oct-18 15:38pm    
Have you considered calling setFilters() on the EditText controls? This would allow you to easily control both length and range.

Java
else if ( (amt.getText().toString().trim().length()   txt1.getText().toString().trim().length())){

What are these two lines supposed to be doing?

Once you have checked that there is some text in your textbox, then you should try to convert it. You can then test that it lies within the range of acceptable values. You should also wrap the conversion in a try/catch block to capture invalid input.

[edit]
Java
public void onClick(View v) {
string strAmount = amt.getText().toString().trim();
if (strAmount.isEmpty()){
    Toast.makeText(myView.getContext(), "Enter Valid Amount", Toast.LENGTH_LONG).show();
}
else {
    try {
        tot = Double.parseDouble(strAmount);
        if (tot < 0 || tot > maxAmount) {
            // show invalid number
        }
        tt=(tot / 100.0f) * 3+tot;
        loadProducts2();
    catch (NumberFormatException ne) {
        Toast.makeText(myView.getContext(), strAmount + " is not a valid number", Toast.LENGTH_LONG).show();
    }
}

[/edit]
 
Share this answer
 
v3
Comments
Member 13818240 23-Oct-18 7:52am    
else if ( (amt.getText().toString().trim().length() >= txt1.getText().toString().trim().length())){

Sorry this is the code which checks the greater or equal between both.
Richard MacCutchan 23-Oct-18 8:46am    
Yes but that does not check whether the text is a valid number, or whether it is within the acceptable range of values. If txt1 is "347" and amt is "foo" then the test will pass, but not in the way you want.
Richard MacCutchan 23-Oct-18 11:12am    
See my updated solution.
You should write clearer code like
C++
public void onClick(View v) {
    String text = amt.getText().toString().trim();

    if (text.isEmpty()){
    } else {
        int value = Double.parseDouble(text);
        // deals with value
This helps a lot in finding better code.

Your else if clause looks very strange. Revisit and rethink that code lines.
 
Share this answer
 
Comments
Member 13818240 24-Oct-18 4:57am    
what you said was correct ,but i don't think so this is the answer for my question
Richard MacCutchan 24-Oct-18 6:30am    
See my updated solution above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900