import jregex.*; import java.awt.*; import java.applet.*; import java.awt.event.*; /* * Simple email validator */ public class Email extends Applet{ private static final String userNameChars="[\\w.\\-]+"; //letters, dots, hyphens private static final String alphaNums="\\w+"; private static final String dot="\\."; private static final Pattern email= new Pattern(userNameChars + "@" + "(?:"+ alphaNums + dot + ")*" + alphaNums); Label lAddr=new Label("Type an address here:"); TextField addrInput=new TextField(35); Label lResult=new Label("Result:"); TextField display=new TextField(10); Button bCheck=new Button("Check"); public void init(){ GridBagLayout gb=new GridBagLayout(); setLayout(gb); GridBagConstraints gbc=new GridBagConstraints(); gbc.fill=gbc.BOTH; gbc.insets=new Insets(2,4,2,4); gbc.weightx=1; gbc.weighty=1; //gbc.gridwidth=gbc.RELATIVE; gbc.gridwidth=gbc.REMAINDER; gb.setConstraints(lAddr,gbc); add(lAddr); gb.setConstraints(addrInput,gbc); add(addrInput); gbc.gridwidth=1; gb.setConstraints(lResult,gbc); add(lResult); gb.setConstraints(display,gbc); add(display); gbc.gridwidth=gbc.REMAINDER; gb.setConstraints(bCheck,gbc); add(bCheck); display.setEditable(false); addrInput.addTextListener(new TextListener(){ public void textValueChanged(TextEvent e){ display.setText(null); } }); bCheck.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ display.setText(check(addrInput.getText())? "Valid": "Invalid"); } }); } private static boolean check(String s){ return email.matcher(s).matches(); } }