简体   繁体   中英

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.

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");
}

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"
    />

Try this pattern ^([0-9]{1,4})(.[0-9]{1,2})?$

it accepts up-to 4 digits before . and only up-to 2 after

You can check your period position. For example in your case

char value = 'anychar' ; if (edittext.gettext.toString().indexOf(value) == 4 ){ // your logic goes here }

    <EditText
    android:id="@+id/edit1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:maxLength="7" />

4 - digits for Number , 1 - digits for .(Dot) and 2- Digits for fraction Number. Its also Number Decimal SoftKeyboard when you made focus on Edittext.

if you want to focus on Edittext auto use this:

          android:focusable="true"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM