How to validate the string object for alphabet input
Hi,
I want to check for alphabet (a-z,A-Z), in String object. I need to check the object, whether its contain numerals or special character, in that case, I want to throw an error stating that "value is not valid". It should accept only the a-z or A-Z.
how to do this.
Thanks in advance
Karthi
[324 byte] By [
Codecafea] at [2007-10-3 2:59:42]

Look at the Pattern class and the matches() method.
> I want to check for alphabet (a-z,A-Z), in String
> object. I need to check the object, whether its
> contain numerals or special character, in that case,
> I want to throw an error stating that "value is not
> valid". It should accept only the a-z or A-Z.
> how to do this.
As Rene suggested, you can do this using the Pattern class:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
You can do it also by looping through your String and check with String's charAt(index) method (which returns a char) to see if every char from the String is >= A AND <= z.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
Good luck.
> You can do it also by looping through your String and
> check with String's charAt(index) method
> (which returns a char) to see if every char from the
> String is >= A AND <= z.
There's a gap between the upper-case and the lower-case letters. So a few special chars like underscore will sneak in. You'd need to check for <= Z and >= a too...
> There's a gap between the upper-case and the
> lower-case letters. So a few special chars like
> underscore will sneak in. You'd need to check for <=
> Z and >= a too...
Cripes, I was under the impression there wasn't a gap! Thank for pointing out the error.
@OP: every char should then satisfy the following condition:
char ch = ...
((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
But the Pattern solution would be far shorter.
> Cripes, I was under the impression there wasn't a> gap! Thank for pointing out the error.And if you recovered from that shock, go and look up an EBCDIC table. (Not that it's relevant to Java.)
> And if you recovered from that shock, go and look up> an EBCDIC table. (Not that it's relevant to Java.)Jeepers... I'm not touching those IBM mainframes, that's for sure!; )
> Jeepers... I'm not touching those IBM mainframes,> that's for sure!> ; )Well, I've only seen a handful of non-IBM mainframes so far, Amdahl mostly, so you might not have much of a choice. ;)
hi, Could u pls, give me some info abt the pattern function with some eg.Thanks for ur response.
> Could u pls, give me some info abt the pattern> function with some eg.Oh read the darn API, it even comes with an example.