Validate Mobile number with the country code

前端 未结 2 1624
攒了一身酷
攒了一身酷 2020-12-22 00:49

I want to validate the mobile number user enters. I have two edit texts one for the code i.e. +91,0 etc and another for the phone number.

I have a question that how

2条回答
  •  醉话见心
    2020-12-22 01:15

    Try this Example...

    activity_main.xml

    
    
    
    
    
    
    
    
    
    

    MainActivity.java

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.text.TextUtils;
    import android.util.Patterns;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    import com.google.i18n.phonenumbers.NumberParseException;
    import com.google.i18n.phonenumbers.PhoneNumberUtil;
    import com.google.i18n.phonenumbers.Phonenumber;
    
    public class MainActivity extends AppCompatActivity {
    
    TextView tvIsValidPhone;
    EditText edtPhone;
    EditText edtCountryCode;
    Button btnValidate;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        tvIsValidPhone = (TextView) findViewById(R.id.tvIsValidPhone);
        edtCountryCode = (EditText) findViewById(R.id.edtCountryCode);
        edtPhone = (EditText) findViewById(R.id.edtPhoneNumber);
        btnValidate = (Button) findViewById(R.id.btnValidate);
        btnValidate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String countryCode = edtCountryCode.getText().toString().trim();
                String phoneNumber = edtPhone.getText().toString().trim();
                if(countryCode.length() > 0 && phoneNumber.length() > 0){
                    if(isValidPhoneNumber(phoneNumber)){
                        boolean status = validateUsing_libphonenumber(countryCode, phoneNumber);
                        if(status){
                            tvIsValidPhone.setText("Valid Phone Number (libphonenumber)");
                        } else {
                            tvIsValidPhone.setText("Invalid Phone Number (libphonenumber)");
                        }
                    }
                    else {
                        tvIsValidPhone.setText("Invalid Phone Number (isValidPhoneNumber)");
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "Country Code and Phone Number is required", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    
    private boolean isValidPhoneNumber(CharSequence phoneNumber) {
        if (!TextUtils.isEmpty(phoneNumber)) {
            return Patterns.PHONE.matcher(phoneNumber).matches();
        }
        return false;
    }
    
    private boolean validateUsing_libphonenumber(String countryCode, String phNumber) {
        PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
        String isoCode = phoneNumberUtil.getRegionCodeForCountryCode(Integer.parseInt(countryCode));
        Phonenumber.PhoneNumber phoneNumber = null;
        try {
            //phoneNumber = phoneNumberUtil.parse(phNumber, "IN");  //if you want to pass region code
            phoneNumber = phoneNumberUtil.parse(phNumber, isoCode);
        } catch (NumberParseException e) {
            System.err.println(e);
        }
    
        boolean isValid = phoneNumberUtil.isValidNumber(phoneNumber);
        if (isValid) {
            String internationalFormat = phoneNumberUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
            Toast.makeText(this, "Phone Number is Valid " + internationalFormat, Toast.LENGTH_LONG).show();
            return true;
        } else {
            Toast.makeText(this, "Phone Number is Invalid " + phoneNumber, Toast.LENGTH_LONG).show();
            return false;
        }
    }}
    

    Download jar file and add in libs folder from below link.. http://www.java2s.com/Code/Jar/l/Downloadlibphonenumber41jar.htm

提交回复
热议问题