Checking Usernames IS there an apI?

What's the best way to do this?

Just seen Pattern class, is this a good way to do it?

Just want to check that a username doesn't contain any characters apart from letters, numbers and underscores basically.

[227 byte] By [DragonSlayrea] at [2007-11-27 10:19:25]
# 1

Use String.matches and a regular expression.

floundera at 2007-7-28 16:57:33 > top of Java-index,Java Essentials,Java Programming...
# 2

> What's the best way to do this?

>

> Just seen Pattern class, is this a good way to do

> it?

>

> Just want to check that a username doesn't contain

> any characters apart from letters, numbers and

> underscores basically.

You can try like this :

public void keyTyped(KeyEvent e)

{

try

{

char c = e.getKeyChar();

if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) ||(c == KeyEvent.VK_DELETE))) || cmbDocPerPage.getEditor().getItem().toString().length()>=3)

{

getToolkit().beep();

e.consume();

}

}catch(Exception ex)

{}

}

Hope this will help you.

LaKshya_Nickya at 2007-7-28 16:57:33 > top of Java-index,Java Essentials,Java Programming...
# 3

if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) || cmbDocPerPage.getEditor().getItem().toString().length()>=3 )

KeyEvent.VK_[whatever] is meant to be compared with a key code, not a char. Also, getKeyCode() will always return KeyEvent.VK_UNDEFINED for key typed events.

CaptainMorgan08a at 2007-7-28 16:57:33 > top of Java-index,Java Essentials,Java Programming...