Reputation: 11
User will be entering the email ids in the text box with semicolon as separator. Before storing into db, i want to validate whether entered email id are valid. So i started using split. But the problem is, when user enter the value like '[email protected];;;',
in this case i want to validate to remove the unwanted characters.
I thought split will return the length will be 4 and then i can check for empty or null. But split returning length as only one. So along with extra characters it stores in the DB. Any help to resolve this ?
Upvotes: 0
Views: 462
Reputation: 11937
Firstly, using a JTextField
for what is essentially a list seems to be impractical. What you should do instead is have a JList<String>
with each item representing an email and then add/remove buttons that will modify the list through your DefaultListModel<String>
. After that, to retrieve all of the elements (email addresses), you may then call the .elements()
method and traverse through them.
So, it would look something like this:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class EmailList extends JPanel implements ActionListener{
private final JList<String> list;
private final DefaultListModel<String> model;
private final JScrollPane scroll;
private final JButton addButton;
private final JButton removeButton;
private final JPanel buttonPanel;
public EmailList(){
super(new BorderLayout());
addButton = new JButton("Add");
addButton.addActionListener(this);
removeButton = new JButton("Remove");
removeButton.addActionListener(this);
buttonPanel = new JPanel(new GridLayout(1, 2, 5, 0));
buttonPanel.add(addButton);
buttonPanel.add(removeButton);
model = new DefaultListModel<>();
list = new JList<>();
list.setModel(model);
scroll = new JScrollPane(list);
add(buttonPanel, BorderLayout.NORTH);
add(scroll, BorderLayout.CENTER);
}
private boolean valid(final String email){
//see if email is valid in here
return true;
}
public List<String> emails(){
final List<String> emails = new LinkedList<>();
final Enumeration<String> elements = model.elements();
while(elements.hasMoreElements())
emails.add(elements.nextElement());
return emails;
}
public void actionPerformed(final ActionEvent e){
final Object source = e.getSource();
if(source.equals(addButton)){
final String input = JOptionPane.showInputDialog(null, "Enter an email address");
if(input == null || input.trim().isEmpty())
return;
final String email = input.trim();
if(!valid(email)){
JOptionPane.showMessageDialog(null, "That email is not valid");
return;
}
model.addElement(email);
list.repaint();
}else if(source.equals(removeButton)){
final int i = list.getSelectedIndex();
if(i < 0)
return;
model.removeElementAt(i);
list.repaint();
}
}
}
Just a minor note, I wrote this in Java 8 so you may need to make some minor changes (only change seems to be putting String
in the empty <>
Upvotes: 0
Reputation: 3652
Would something like this help:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test{
public static void main(String args[]){
String line = "'[email protected];;;',";
if(line.contains("@")){
int x=0;
for (String valueBetweenCommas: line.split("'", 2)){
x++;
if(x==2){
Pattern p = Pattern.compile("[\\w]*([.+]??[\\w]+?)+?[\\w]@[\\w]+[.][a-zA-Z]{2,5}+$");
Matcher m = p.matcher(valueBetweenCommas);
boolean result = m.matches();
if(result){
//add to database
}else{
break;
}
}
}
}
}
Upvotes: 0
Reputation: 225
Do it this way to validate and get a length
String values = textBox1.getText();
String[] split = values.split(";");
System.out.println(split.length);
for(String s : split) {
if(null != s)
//do junk
}
Upvotes: 0
Reputation: 13123
After you use split(), you will have all the supposed email ids in an array and can validate them. After validation, instead of using the original string, rebuild the string using the array. That eliminates any duplicate semicolons you had. Use StringBuffer for the building, it is slightly more efficient for concatenating a number of strings.
Upvotes: 0
Reputation: 178263
The split
method by default discards trailing empty strings when splitting the text. If you really want the trailing empty strings, then use the overloaded version of split
that takes two arguments -- the second argument limit
can be negative, and it tells split
not to discard trailing empty strings.
But I don't see why you would want the trailing empty strings. The split
method normally discards them for you.
Upvotes: 2