Validating IP in android

HI all, I am using simple socket communication between android(as client) and PC(as server). I am using EditText for Ip address. I want to Validate the IP address that is being entered in EditText field. Can please some one help me.. I am need of it. Thanks in advance.


If you are targeting API Level 8, you can use the Patterns.IP_ADDRESS global regex. You may directly include this regex in your project if you target device with android < 2.2:

private static final Pattern IP_ADDRESS
    = Pattern.compile(
        + "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]"
        + "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
        + "|[1-9][0-9]|[0-9]))");
Matcher matcher = IP_ADDRESS.matcher("");
if (matcher.matches()) {
    // ip is correct


To check the IP as it's being entered you might want to use this instead:

private static final Pattern PARTIAl_IP_ADDRESS =

ipEditText.addTextChangedListener(new TextWatcher() {                       
    @Override public void onTextChanged(CharSequence s, int start, int before, int count) {}            
    @Override public void beforeTextChanged(CharSequence s,int start,int count,int after) {}            

    private String mPreviousText = "";          
    public void afterTextChanged(Editable s) {          
        if(PARTIAl_IP_ADDRESS.matcher(s).matches()) {
            mPreviousText = s.toString();
        } else {
            s.replace(0, s.length(), mPreviousText);

Need Your Help

How to calculate REM for type?

css css3

How do I convert PX into REM for my type? I've read Jonathan Snook's article about using REM, but he was using font-size:62.5%, which defaults to your font size to 10px (for easier calculating). How

Maximum length of variable name in JavaScript

javascript name-length

What is the maximum length of a variable name in JavaScript?