我有一个包含手机号码,通行证和电子邮件的应用程序,如果用户输入的电子邮件ID类似于" lll"然后按提交按钮,然后应用程序将发送类似" lll"到服务器,但我想要的条件只会检查用户在电子邮件编辑文本中输入一些东西,如果用户没有输入任何东西,那么应该只向移动和密码发送请求到服务器,因为电子邮件是optionel.How我可以做什么该
代码: -
@Override
public void onClick(View v) {
m_Email = m_EmailEditText.getText().toString().trim();
if (!isValidEmail(m_Email)) {
CSnackBar.getInstance().showSnackBarError(findViewById(R.id.mainLayout), "Please enter valid Email Id", getApplicationContext());
} else {
confirmationDialog();//open confirmation dialog.....
}
}
});
/*validation for email*/
private boolean isValidEmail(String email) {// validation for email Id
String emailPattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(emailPattern);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
答案 0 :(得分:2)
我遇到了类似的问题,我准备了一个模块来检查它。 我准备了一个代码,用于所有类型的检查,例如姓名,电话号码,电子邮件,注册号等。
这是Validation.java类,我们在其中定义了所有类型的检查: -
package com.Validation;
import android.util.Log;
import android.widget.EditText;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Validation {
// Regular Expression
// you can change the expression based on your need
private static final String EMAIL_REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
private static final String PHONE_REGEX = "\\d{3}-\\d{7}";
public static final String USERNAME_PATTERN = "^[a-z0-9_-]{3,15}$";
private static Matcher matcher;
private static Pattern pattern;
/// private static final String USERNAME_PATTERN =;
// Error Messages
private static final String REQUIRED_MSG = "required";
private static final String EMAIL_MSG = "invalid email";
private static final String PHONE_MSG = "###-#######";
////for user name validation
public Validation(){
pattern = Pattern.compile(Validation.USERNAME_PATTERN);
}
public static boolean validateusername(String username){
matcher = pattern.matcher(username);
return matcher.matches();
}
// call this method when you need to check email validation
public static boolean isEmailAddress(EditText editText, boolean required) {
return isValid(editText, EMAIL_REGEX, EMAIL_MSG, required);
}
// call this method when you need to check phone number validation
public static boolean isPhoneNumber(EditText editText, boolean required) {
return isValid(editText, PHONE_REGEX, PHONE_MSG, required);
}
// return true if the input field is valid, based on the parameter passed
public static boolean isValid(EditText editText, String regex, String errMsg, boolean required) {
String text = editText.getText().toString().trim();
editText.setError(null);
// text required and editText is blank, so return false
if ( required && !hasText(editText) ) return false;
// pattern doesn't match so returning false
if (required && !Pattern.matches(regex, text)) {
editText.setError(errMsg);
return false;
};
return true;
}
// check the input field has any text or not
// return true if it contains text otherwise false
public static boolean hasText(EditText editText) {
String text = editText.getText().toString().trim();
editText.setError(null);
// length 0 means there is no text
if (text.length() == 0) {
editText.setError(REQUIRED_MSG);
return false;
}
return true;
}
}
现在,在您要进行验证的活动中,请输入以下代码: -
public class MyActivity extends Activity {
private EditText etNormalText;
private EditText etEmailAddrss;
private EditText etPhoneNumber;
private Button btnSubmit;
public static final String USERNAME_PATTERN = "^[a-z_-]{1,15}$";
public static final String ENUM_PATTERN = "^[a-z0-9_-]{3,12}$";
private static Matcher matcher;
private static Pattern pattern,patforeno;
public MyActivity(){
pattern = Pattern.compile(USERNAME_PATTERN);
patforeno = Pattern.compile(ENUM_PATTERN);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
registerViews();
}
public static boolean validateusername(String username){
matcher = pattern.matcher(username);
return matcher.matches();
}
private void registerViews() {
etNormalText = (EditText) findViewById(R.id.et_normal_text);
// TextWatcher would let us check validation error on the fly
etNormalText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
//Validation.hasText(etNormalText);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
etEmailAddrss = (EditText) findViewById(R.id.et_email_address);
etEmailAddrss.addTextChangedListener(new TextWatcher() {
// after every change has been made to this editText, we would like to check validity
public void afterTextChanged(Editable s) {
Validation.isEmailAddress(etEmailAddrss, true);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
etPhoneNumber = (EditText) findViewById(R.id.et_phone_number);
etPhoneNumber.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Validation.isPhoneNumber(etPhoneNumber, false);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
btnSubmit = (Button) findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*
Validation class will check the error and display the error on respective fields
but it won't resist the form submission, so we need to check again before submit
*/
if (checkValidation())
submitForm();
else
Toast.makeText(MyActivity.this, "Form contains error", Toast.LENGTH_LONG).show();
}
});
}
private void submitForm() {
// Submit your form here. your form is valid
etNormalText = (EditText) findViewById(R.id.et_normal_text);
boolean isvalidu = false;
etPhoneNumber = (EditText) findViewById(R.id.et_phone_number);
etEmailAddrss = (EditText) findViewById(R.id.et_email_address);
////email validation
if (Validation.isEmailAddress(etEmailAddrss, true)) {
Log.d("mail", "validmail");
} else {
Log.d("mail", "invalidvalidmail");
}
///phone number validation
int length = etPhoneNumber.getText().toString().length();
Log.d("length", String.valueOf(length));
int ten = 10;
if (length != ten) {
Toast.makeText(getApplicationContext(),"Please enter your ten digit mobile number",Toast.LENGTH_LONG).show();
} else {
Validation.isPhoneNumber(etPhoneNumber, false);
Log.d("numberpho","valid");
}
///username validation
try {
isvalidu=validateusername(etNormalText.getText().toString());
Log.d("hello valid username", "error message"+isvalidu);
Toast.makeText(this, "Submitting form..."+isvalidu, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.d("hello valid username", "error message"+isvalidu);
Toast.makeText(this, "Submitting form...", Toast.LENGTH_LONG).show();
}
}
private boolean checkValidation() {
boolean ret = true;
if (!Validation.hasText(etNormalText)) ret = false;
if (!Validation.isEmailAddress(etEmailAddrss, true)) ret = false;
if (!Validation.isPhoneNumber(etPhoneNumber, false)) ret = false;
return ret;
}
}
所以希望这些对你的代码有所帮助,实际上你必须根据你的需求优化我的代码.registerView()和checkValidation()以及submitforms()方法对于验证来说是相当可观的。
答案 1 :(得分:1)
您可以查看电子邮件是否为空。
如果电子邮件不为空 - >验证电子邮件
如果电子邮件为空 - >什么都不做
:not(:first-child)
答案 2 :(得分:1)
请检查此链接并使用Saripar https://github.com/ragunathjawahar/android-saripaar
答案 3 :(得分:0)
这是我做的事情
if (email.contains("@") ){
if ( email.charAt(email.length-3)=='.' || email.charAt(email.length-4) == '.'){
//do stuff
}}
else Toast.makeText(this , "Email is not valid" , Toast.LENGTH_SHORT).show;
这可以确保电子邮件像something@something.xxx或.xx 由于域名太多,我认为其中有2或3个字符 您可以相应地进行编辑,但它可以正常工作
答案 4 :(得分:0)
使用此功能检查邮件是否格式化:
public final static boolean isValidEmail(CharSequence target) {
if (target == null) {
return false;
} else {
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
}
然后使用你的方法。
P.S。在这里,我使用默认匹配器来处理电子邮件地址。所以没有 需要创建我们自己的匹配器。