android - How to validate edit Text so it is only possible to enter valid email? -
i making android application using eclipse. have edittext box validate email. edittext in xml , id of "edittext1". found answers how validate email problem is: don't know how apply "edittext1" in java code.
below nowadays code email validation found: public final 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}" + ")+" ); private boolean checkemail(string email) { homecoming email_address_pattern.matcher(email).matches(); }
when paste code java file message:
"the method checkemail(string) type mainactivity never used locally"
you have reference edittext
something should work:
edittext et = (edittext) findviewbyid(r.id.edittext1);
then can text edittext this:
string str = et.gettext().tostring();
then can phone call validate method
if(checkemail(str)){ //email valid }
however based on question asking , manner in you've asked can tell benefit going , studying plain java before dive android. , 1 time you've got java syntax down, should start examples , tutorials on official android developer website, covers lot of basics seem having problem understanding.
and 1 lastly aside, "edittext1" not name edittext, doesn't explain object. code easier work if strive utilize more descriptive names objects, perhaps "emailtext" might improve choice?
android
No comments:
Post a Comment