Javaで正当なメールアドレスかどうかを判断する
Javaコード:
public class Mailcheck_Startnews24 {
public static void main(String[] args) {
//メールアドレス
String check = “^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(“mail@arkgame.com");
boolean isMatched = matcher.matches();
System.out.println(isMatched);
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String mail=null;
System.out.println(“E-Mailを入力してください:");
mail=scanner.next();
Pattern pattern=Pattern.compile(“\\w+@(\\w+.)+[a-z]{2,3}");//\wa-z,A-Z,0-9(\\エスケープ)
Matcher matcher=pattern.matcher(mail);
boolean b=matcher.matches();
if (b) {
System.out.println(mail+"有効なメールアドレス!");
}else {
System.out.println(mail+"のフォーマットが間違えました!!");
}
}
/**
*javascriptでメールアドレスの正確を判断
*/
function isEmail(email)
{
var srt=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
if(srt.test(email))
{
//無効
return false;
}
else
{
//有効
return true;
}
}
}
public static boolean validateEmail(String email) {
boolean flag = false;
int pos = email.indexOf(“@");
if (pos == -1 || pos == 0 || pos == email.length() – 1) {
return false;
}
String[] strings = email.split(“@");//@がない場合
if (strings.length != 2) {
return false;
}
CharSequence cs = strings[0];
for (int i = 0; i < cs.length(); i++) {
char c = cs.charAt(i);
if (!Character.isLetter(c) && !Character.isDigit(c)) {
return false;
}
}
pos = strings[1].indexOf(“.");// @の後ろ.がない場合。
if (pos == -1 || pos == 0 || pos == email.length() – 1) {
return false;
}
strings = strings[1].split(“.");
for (int j = 0; j < strings.length; j++) {
cs = strings[j];
if (cs.length() == 0) {
return false;
}
for (int i = 0; i < cs.length(); i++) {//不規則な文字を含む場合
char c = cs.charAt(i);
if (!Character.isLetter(c) && !Character.isDigit(c)) {
return false;
}
}
}
return true;
}