• Post Reply Bookmark Topic Watch Topic
  • New Topic

Entry limit to Textfields  RSS feed

 
linda lichelli
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all, i wrote some code to check the length of textfields. as i have to save all into a DB, where i've got some varchar(10). the code i have allows you to type only 10 chars into the respective textfield, but my problem is, that if i paste a longer string into it (from clipboard), the textfields accepts it and while i try to save, i get a DB exception that a submitted value is too big. am thankfull for any help.
aa.
 
ali rafiq
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it's happened when u paste at textfeild .......
if u dont need the word longer then 10 chars
my logic is
u have to put this code at the event of button

// tf is the veriable of textfeild
String str = tf.getText();
int i = str.lenght();
if (i<10)
{
System.out.println("ok");
}
esle{
System.out.println("again");
// you have to write under 10
//
}
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Linda,
There is an excellent (in my opinion) example of how to achieve this in "The Java Tutorial" (assuming you haven't already looked at it):
http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html#validation
Hope this helps you.
Good Luck,
Avi.
 
linda lichelli
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much. although the java tutorial implementation would be much better as i did now, i chose the way of getting the stringbuffer length and check it before writing to the DB. it's not too proper, but it works for the moment! thx again.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!