简体   繁体   中英

JAVA JTextField verification user input email

I need to verify that the user input has both the characters '@' and '.' in their email address.

So far I've figured out how to verify one character(the '@' char). But how do I make it so that the program checks for two characters in the same textfield (both '@' and '.') ?

if (evt.getSource() == submitButton) {

            String get = emailText.getText().toString();
            char a = '@';
            char dot = '.';
            String message = "";
            int count = 0;

            for(int i=0; i<get.length(); i++) {

                if(get.charAt(i)== a){
                    count++;
                    message = "This is valid email";

                    if(count>=2){
                        message = "this isn't valid email";
                    }
                }else{
                    message = "This isn't valid";
                }
            }

this is a very basic check for verification of email

int atposition = 0, dotposition = 0, flag = 0;

for(int i = 0; i < get.length(); i++) {

    if(get.charAt(i) == a) {
                count++;
                atposition = i;

                if(count >= 2) {
                    flag = 1;
                    break;
                }
    }

   if (get.charAt(i) == '.') {
                dotposition = i;
    }
 }

 if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= get.length() || flag == 1) {
    message = "Not a valid e-mail address";
 } else {
    message = "valid email-id";
 }
 return message;

there can be more than one dot in an email..see if it works for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM