is Number Or Alpha by regular expression - Android java.util.regex

Android examples for java.util.regex:Number Pattern

Description

is Number Or Alpha by regular expression

Demo Code

import android.util.Log;
import android.widget.TextView;
import java.io.File;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static boolean isNumberOrAlpha(String inputed) {
        if (inputed == null) {
            Log.e(TAG, "isNumberOrAlpha  inputed == null >> return false;");
            return false;
        }/*from   w  ww  .j  av  a2 s .c o  m*/
        Pattern pNumber = Pattern.compile("[0-9]*");
        Matcher mNumber;
        Pattern pAlpha = Pattern.compile("[a-zA-Z]");
        Matcher mAlpha;
        for (int i = 0; i < inputed.length(); i++) {
            mNumber = pNumber.matcher(inputed.substring(i, i + 1));
            mAlpha = pAlpha.matcher(inputed.substring(i, i + 1));
            if (!mNumber.matches() && !mAlpha.matches()) {
                return false;
            }
        }

        currentString = inputed;
        return true;
    }

}

Related Tutorials