bagaimana cara memeriksa teks edittext
apakah alamat email atau tidak tanpa menggunakan javascript
dan ekspresi reguler? Di sini saya menggunakan inputtype="textEmailAddress"
ini berfungsi tetapi tidak ada pesan kesalahan yang ditampilkan.
bagaimana cara memeriksa teks edittext
apakah alamat email atau tidak tanpa menggunakan javascript
dan ekspresi reguler? Di sini saya menggunakan inputtype="textEmailAddress"
ini berfungsi tetapi tidak ada pesan kesalahan yang ditampilkan.
Jawaban:
/**
* method is used for checking valid email id format.
*
* @param email
* @return boolean true for valid false for invalid
*/
public static boolean isEmailValid(String email) {
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
Berikan string teks edit Anda dalam fungsi ini.
untuk verifikasi email yang benar, Anda memerlukan otentikasi sisi server
Perhatikan sekarang ada metode bawaan di Android, lihat jawaban di bawah.
String
mengimplementasikan CharSequence
jadi saya pikir pemeran dari email
hingga inputStr
berlebihan.
Di Android 2.2+ gunakan ini:
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
sebagai contoh:
EditText emailid = (EditText) loginView.findViewById(R.id.login_email);
String getEmailId = emailid.getText().toString();
// Check if email id is valid or not
if (!isEmailValid(getEmailId)){
new CustomToast().Show_Toast(getActivity(), loginView,
"Your Email Id is Invalid.");
}
Silakan ikuti langkah-langkah berikut
Langkah 1 :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_below="@+id/textView_email"
android:layout_marginTop="40dp"
android:hint="Email Adderess"
android:inputType="textEmailAddress" />
<TextView
android:id="@+id/textView_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Email Validation Example" />
</RelativeLayout>
Langkah 2:
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
Langkah 3:
public class MainActivity extends Activity {
private EditText email;
private String valid_email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initilizeUI();
}
/**
* This method is used to initialize UI Components
*/
private void initilizeUI() {
// TODO Auto-generated method stub
email = (EditText) findViewById(R.id.editText_email);
email.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Is_Valid_Email(email); // pass your EditText Obj here.
}
public void Is_Valid_Email(EditText edt) {
if (edt.getText().toString() == null) {
edt.setError("Invalid Email Address");
valid_email = null;
} else if (isEmailValid(edt.getText().toString()) == false) {
edt.setError("Invalid Email Address");
valid_email = null;
} else {
valid_email = edt.getText().toString();
}
}
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email)
.matches();
} // end of TextWatcher (email)
});
}
}
Saya menulis perpustakaan yang memperluas EditText yang mendukung beberapa metode validasi asli dan sebenarnya sangat fleksibel.
Saat ini, saat saya menulis, metode validasi yang didukung secara asli (melalui atribut xml ) adalah:
Anda dapat memeriksanya di sini: https://github.com/vekexasia/android-form-edittext
Berharap kamu menikmatinya :)
Di halaman yang saya tautkan, Anda juga dapat menemukan contoh untuk validasi email. Saya akan menyalin cuplikan relatif di sini:
<com.andreabaccega.widget.FormEditText
style="@android:style/Widget.EditText"
whatever:test="email"
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
/>
Ada juga aplikasi uji yang menampilkan kemungkinan perpustakaan.
Ini adalah tangkapan layar dari aplikasi yang memvalidasi bidang email.
Seperti yang disebutkan dalam salah satu jawaban, Anda dapat menggunakan Patterns
kelas seperti di bawah ini:
public final static boolean isValidEmail(CharSequence target) {
if (target == null)
return false;
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
Secara kebetulan jika Anda bahkan mendukung API level kurang dari 8, maka Anda cukup menyalin Patterns.java
file ke dalam proyek Anda dan mereferensikannya. Anda bisa mendapatkan kode sumber Patterns.java
dari tautan ini
Dalam kasus Anda, Anda dapat menggunakan android.util.Patterns package
.
EditText email = (EditText)findViewById(R.id.user_email);
if(Patterns.EMAIL_ADDRESS.matcher(email.getText().toString()).matches())
Toast.makeText(this, "Email is VALID.", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Email is INVALID.", Toast.LENGTH_SHORT).show();
Kode berikut akan berguna bagi Anda.
String email;
check.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
checkEmail(email);
if (checkMail) {
System.out.println("Valid mail Id");
}
}
});
}
}
public static boolean checkEmail(String email) {
Pattern EMAIL_ADDRESS_PATTERN = Pattern
.compile("[a-zA-Z0-9+._%-+]{1,256}" + "@"
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" + "(" + "."
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" + ")+");
return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}
di sini email adalah email-id Anda.
public boolean validateEmail(String email) {
Pattern pattern;
Matcher matcher;
String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();
}
Apache Commons Validator dapat digunakan seperti yang disebutkan di jawaban lain.
Langkah: 1) Unduh file jar dari sini
Langkah: 2) Tambahkan ke libs proyek Anda
Impor:
import org.apache.commons.validator.routines.EmailValidator;
Kode:
String email = "myName@example.com";
boolean valid = EmailValidator.getInstance().isValid(email);
dan untuk mengizinkan alamat lokal ::
boolean allowLocal = true;
boolean valid = EmailValidator.getInstance(allowLocal).isValid(email);
Metode sederhana
private boolean isValidEmail(String email)
{
String emailRegex ="^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
if(email.matches(emailRegex))
{
return true;
}
return false;
}
I Hope this code is beneficial for you
public class Register extends Activity
{
EditText FirstName, PhoneNo, EmailId,weight;
Button Register;
private static final Pattern EMAIL_PATTERN = Pattern
.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
private static final Pattern USERFIRSTNAME_PATTERN = Pattern
.compile("[a-zA-Z0-9]{1,250}");
private static final Pattern PHONE_PATTERN = Pattern
.compile("[a-zA-Z0-9]{1,250}");
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
Register=(Button) findViewById(R.id.register);
FirstName=(EditText)findViewById(R.id.person_firstname);
PhoneNo =(EditText)findViewById(R.id.phone_no);
EmailId=(EditText)findViewById(R.id.email_id);
weight=(EditText) findViewById(R.id.weight);
Register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sFirstName= FirstName.getText().toString();
sPhoneNo= PhoneNo.getText().toString();
sEmailId= EmailId.getText().toString();
sweight= weight.getText().toString();
if(sFirstName.equals("")||sPhoneNo.equals("")||sEmailId.equals("")||sweight.equals(""))
{
if ((!CheckUsername(sFirstName)))
{
Toast.makeText(Register.this, "FirstName can not be null",Toast.LENGTH_LONG).show();
}
else if ((!Checkphoneno(sPhoneNo)))
{
Toast.makeText(Register.this, "ENTER VALID mobile no ",Toast.LENGTH_LONG).show();
}
else if ((!CheckEmail(sEmailId)))
{
Toast.makeText(Register.this, "ENTER VALID EMAIL ID",Toast.LENGTH_LONG).show();
}
else if ((!Checkweight(sweight)))
{
Toast.makeText(Register.this, "ENTER Weight in kg",Toast.LENGTH_LONG).show();
}
}
}
private boolean CheckEmail(String sEmailId) {
return EMAIL_PATTERN.matcher(sEmailId).matches();
}
private boolean CheckUsername(String sFirstName) {
return USERFIRSTNAME_PATTERN.matcher(sFirstName).matches();
}
private boolean Checkphoneno(String sPhoneNo) {
return PHONE_PATTERN.matcher(sPhoneNo).matches();
}
private boolean Checkweight(String sweight) {
return Weight_PATTERN.matcher(sweight).matches();
}
});
public static boolean isEmailValid(String email) {
boolean isValid = false;
String expression = "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
+ "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
+ "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";
// "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (!matcher.matches()) {
isValid = true;
}
return isValid;
}
untuk validasi email coba ini.
public boolean checkemail(String email)
{
Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
Anda dapat memeriksanya dengan ekspresi reguler
public boolean isValid(String strEmail)
{
pattern = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
matcher = pattern.matcher(strEmail);
if (strEmail.isEmpty()) {
return false;
} else if (!matcher.matches()) {
return false;
}
else
{
return true;
}
}
private boolean isValidEmailID(String email) {
String PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
Dengan android.util.Patterns dan Kotlin, itu sangat sederhana. Fungsi satu baris yang mengembalikan nilai Boolean.
fun validateEmail(email: String) = Patterns.EMAIL_ADDRESS.matcher(email)