• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

How to only accept characters and integers

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to only accept Characters for name and numbers??
 
Sheriff
Posts: 6456
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want just letters or just numbers or both?  A character can be a letter, a number, punctuation, and other things.

You probably will need a TextFormatter but I can't show you how to write one until the above is settled.
 
Cruz Loyde
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Do you want just letters or just numbers or both?  A character can be a letter, a number, punctuation, and other things.

You probably will need a TextFormatter but I can't show you how to write one until the above is settled.



I want my name variable to only accept characters while the year only accept numbers.
 
Sheriff
Posts: 24761
59
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cruz Loyde wrote:I want my name variable to only accept characters.



That field can only accept characters anyway. But like Knute said, maybe you only want a restricted set of characters? Maybe you don't want, say, the = character or the ™ character? If so then you're going to need to specify what characters can be in the name.

(Warning: in real life you'd be surprised what kind of characters people have in their names, so you'd have to be careful if real employees were going to use this code.)
 
Cruz Loyde
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Cruz Loyde wrote:I want my name variable to only accept characters.



That field can only accept characters anyway. But like Knute said, maybe you only want a restricted set of characters? Maybe you don't want, say, the = character or the ™ character? If so then you're going to need to specify what characters can be in the name.

(Warning: in real life you'd be surprised what kind of characters people have in their names, so you'd have to be careful if real employees were going to use this code.)



Whenever i try to add a name like "John1" it adds it, i want to prevent it from being added.
 
Knute Snortum
Sheriff
Posts: 6456
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cruz Loyde wrote:I want my name variable to only accept characters while the year only accept numbers.


I'm assuming you mean, I want my text field for name to only accept letters and my year text field the only accept numbers.  Here's the code for yearOfHire:
 
Knute Snortum
Sheriff
Posts: 6456
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So for employee name, you'd change line 5 to something like this:
But as Paul pointed out, this won't allow you to enter a lot of legitimate names, like hyphenated names or names with non-Latin alphabet characters, etc.  If you just want numbers excluded, you could try:
but why do that?
 
Cruz Loyde
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:

Cruz Loyde wrote:I want my name variable to only accept characters while the year only accept numbers.


I'm assuming you mean, I want my text field for name to only accept letters and my year text field the only accept numbers.  Here's the code for yearOfHire:



i figured it out for name
 
Ranch Hand
Posts: 199
4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if the name is Thurston Howell the 3rd Jr. ? it requires numbers and a period.
you'll get sued for name discrimination
 
Paul Clapham
Sheriff
Posts: 24761
59
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it has spaces too. But at least the OP is on the right track, and now it's just a matter of fixing up the regex to handle that sort of thing.
 
Cruz Loyde
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

S Fox wrote:what if the name is Thurston Howell the 3rd Jr. ? it requires numbers and a period.
you'll get sued for name discrimination



Calm down its just a project for my class, im just following instructions on what to prevent.
 
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

S Fox wrote:. . . Thurston Howell the 3rd Jr. ? . . .

Surely you can't be both the 3rd and Junior? I thought you would use Roman numerals III instead of the 3rd.
 
Knute Snortum
Sheriff
Posts: 6456
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cruz Loyde wrote:i figured it out for name


Just so you know, the anchors (^, $) aren't needed, as the matches() method matches the entire String.
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!