Regex validation for removing invalid characters in Android.

By | July 14, 2011

This is a simple example in Android to allow only valid characters to enter in a textBox or EditText.

I am using this regex pattern to allow only valid characters.

“^[a-z_A-Z0-9 ]*$”;

This expression allows only characters from a-z, A-Z and numbers from 0-9 and a ‘ ‘(space) character.
Here is the java code

package pack.coderzheaven;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class RegexDemo extends Activity {
    EditText tbox;
    Button but;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tbox = (EditText)findViewById(R.id.EditText01);
        but  = (Button)findViewById(R.id.Button01);
        but.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isValid(tbox.getText().toString().trim())){
                    Toast.makeText(getApplicationContext(), "Text entered is OK.", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getApplicationContext(), "Oops!! Text entered is Invalid.", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    public static boolean isValid(String str)
    {
            boolean isValid = false;
            String expression = "^[a-z_A-Z0-9 ]*$";
            CharSequence inputStr = str;
            Pattern pattern = Pattern.compile(expression);
            Matcher matcher = pattern.matcher(inputStr);
            if(matcher.matches())
            {
                isValid = true;
            }
            return isValid;
     }
}

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<EditText
    android:text=""
    android:hint="enter some text"
    android:id="@+id/EditText01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
</EditText>
<Button
    android:text="Test"
    android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>
</LinearLayout>

Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Regex invalid character validation from <b>CoderzHeaven.</b></string>
    <string name="app_name">Regex Validation Demo From CoderzHeaven</string>
</resources>
Regex Validation

Regex Validation

Regex Validation

Regex Validation

Leave a Reply

Your email address will not be published. Required fields are marked *