
Write a java swing program to get name and email id. Display the error message if email id is not in xxxxx@xxxx.xxx format.

import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.regex.*;
class FormData{
public static boolean isEmailValid(String email) {
boolean isValid = false;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
public static void main(String[] args){
Frame f=new Frame();
Label label1=new Label("Name: ");
Label label2=new Label("Email: ");
final TextField text1=new TextField(20);
final TextField text2=new TextField(20);
Button b=new Button("Check");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String v1=text1.getText();
String v2=text2.getText();
if(isEmailValid(v2) != true) {
JOptionPane.showMessageDialog(null,"Invalid Email Id!");
text2.setText("");
}
}
});
Panel p=new Panel(new GridLayout(3,2));
p.add(label1);
p.add(text1);
p.add(label2);
p.add(text2);
p.add(b);
f.add(p);
f.setVisible(true);
f.pack();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.