Android checks whether the entered email/email address format is valid when creating/editing a contact.

Source: Internet
Author: User

Android checks whether the entered email/email address format is valid when creating/editing a contact.
JB version:
1. SIM/USIM card contact: You can directly use USIM_EMAIL_PATTERN in EditSimContactActivity to search for "USIM_EMAIL_PATTERN" and remove the comments added to the relevant code.


2. Mobile phone local contact: Modify the Code as follows:
ContactEditorFragment. java (packages \ apps \ contacts \ src \ com \ android \ contacts \ editor)
A import package
Import java. util. regex. Pattern;
B. Add variable
Private static final String USIM_EMAIL_PATTERN = "0-9] [a-z] [A-Z] [_ 0-9] [a-z] [A-Z] [-_. * @ 0-9] [a-z] [A-Z] [-_. + ";
In the C save function,
In the following statement:
If (! HasValidState () | mStatus! = Status. EDITING ){
Log. I (TAG, "[save]! HasValidState (): "+ (! HasValidState ())
+ "| MStatus! = Status. EDITING: "+ (mStatus! = Status. EDITING)
+ ", MStatus:" + mStatus );
Return false;
}
Then add:
// Email
For (int n = 0; n <mState. size (); n ++ ){
RawContactDelta field = mState. get (n );
ArrayList Data = field. getMimeEntries ("vnd. android. cursor. item/email_v2 ");
If (data! = Null)
For (int m = 0; m <data. size (); m ++ ){
String email = data. get (m). getAsString ("data1 ");
If (! TextUtils. isEmpty (email )){
If (! Pattern. matches (USIM_EMAIL_PATTERN, email )){

Toast. makeText (mContext, R. string. email_invalid, Toast. LENGTH_SHORT). show ();
GetActivity (). finish ();
Return false;
}
}
}
}
KK version
1. SIM/USIM card contact:
SIMEditProcessor. java (alps \ packages \ apps \ contacts \ src \ com \ mediatek \ contacts \ simservice)
First add private boolean mEmailInvalid = false;
Then modify
Private boolean isUSIMUpdateValuesInvalid ()
// Mtk add
If (! TextUtils. isEmpty (mUpdatemail )){
If (! Pattern. matches (USIM_EMAIL_PATTERN, mUpdatemail )){
MEmailInvalid = true;
}
}
// Mtk add end
LogUtils. I (TAG, "mNumberIsNull:" + mNumberIsNull + ", mNumberInvalid:" +
MNumberInvalid + ", mFixNumberInvalid:" + mFixNumberInvalid); // reference location

Modify again
Private boolean isInsertValuesInvalid ()
If (! TextUtils. isEmpty (mUpdateAdditionalNumber )){
If (! Pattern. matches (SIM_NUM_PATTERN, PhoneNumberUtilsEx
. ExtractCLIRPortion (mUpdateAdditionalNumber ))){
MFixNumberInvalid = true;
}
} // Reference location
// Mtk add
If (! TextUtils. isEmpty (mUpdatemail )){
If (! Pattern. matches (USIM_EMAIL_PATTERN, mUpdatemail )){
MEmailInvalid = true;
}
}
// Mtk add end

Last Modified
Private boolean setSaveFailToastText ()
} */Else if (mEmailInvalid ){
MSaveFailToastStrId = R. string. cannot_insert_error_format_email; // Add resources by yourself
MEmailInvalid = false;
2. Mobile phone local contact: Modify the Code as follows:
ContactEditorFragment. java (packages \ apps \ contacts \ src \ com \ android \ contacts \ editor)
A import package
Import java. util. regex. Pattern;
B. Add variable
Private static final String USIM_EMAIL_PATTERN = "0-9] [a-z] [A-Z] [_ 0-9] [a-z] [A-Z] [-_. * @ 0-9] [a-z] [A-Z] [-_. + ";
In the C save function,
In the following statement:
If (! HasValidState () | mStatus! = Status. EDITING ){
Log. I (TAG, "[save]! HasValidState (): "+ (! HasValidState ())
+ "| MStatus! = Status. EDITING: "+ (mStatus! = Status. EDITING)
+ ", MStatus:" + mStatus );
Return false;
}
Then add:
// Email
For (int n = 0; n <mState. size (); n ++ ){
RawContactDelta field = mState. get (n );
ArrayList Data = field. getMimeEntries ("vnd. android. cursor. item/email_v2 ");
If (data! = Null)
For (int m = 0; m <data. size (); m ++ ){
String email = data. get (m). getAsString ("data1 ");
If (! TextUtils. isEmpty (email )){
If (! Pattern. matches (USIM_EMAIL_PATTERN, email )){

Toast. makeText (mContext, R. string. email_invalid, Toast. LENGTH_SHORT). show ();
GetActivity (). finish ();
Return false;
}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.