Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText (Currency) validation in android

I want to set EditText validation,

Only contains number, size 4 digit for decimal, and 2 digit for fraction value..total 6 digit,

I also want just enter only number form number pad. if i am select any non numeric number then EditText will not accept it.

if any suggestion, it would be great. Thanks.

like image 644
Vrajesh Avatar asked Mar 18 '23 20:03

Vrajesh


2 Answers

Have

android:inputType="numberDecimal"

attribute for EditText

You can use a regex for the validation part. The below is only a sample

You can check with a online regex tester @ http://regex101.com/r/uG9aF6/1

String input = "1024.22";
String pattern = "([0-9]{4})(\.)([0-2]{2})"; // 4 digits followe by . followed by 2 digits
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
if(m.matches())
{
   System.out.println("Validated");
}
else
{
   System.out.println("Not Validated");
}
like image 53
Raghunandan Avatar answered Mar 23 '23 22:03

Raghunandan


android:inputType ="number" and android:maxLength="6" in xml layout of editText will popup the keypad of digits and only six digits can be entered in the editText

like this

<EditText
        android:id="@+id/myinput"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:maxLength="6"
        android:hint="@string/somestring"
        android:layout_weight="1"
    />
like image 40
nobalG Avatar answered Mar 23 '23 20:03

nobalG