Validating a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shoby
    New Member
    • Jul 2008
    • 9

    Validating a string

    Hi,

    I am writing code to validate a String.The validation should be such that the String can contain only alphabets.It should display an error message if the string contains any numbers or special characters.Can someone help me on this???

    Regards,
    Shoby
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Use regular expressions for that; the following code snippet solves it all; read
    the API documentation for the Pattern class and String class for
    an explanation:

    [code=java]
    public boolean isAllLetters(St ring s) {
    return s.replaceAll("[a-zA-Z]+", "").length( ) == 0;
    }
    [/code]

    kind regards,

    Jos

    Comment

    Working...