12 Replies Latest reply: Dec 24, 2007 1:17 AM by 807603 RSS

    Validate input if Alpha characters

    807603
      Hi Guys,

      Good day!

      I need your help on validating field if it contains only alpha characters (A - Z) only. I have a field that accepts user input...

      This is sample of the code:
      String str = fld.getText().trim();
      
      if(isAlpha(str)){
      System.out.println("Alpha characters");
      }
      I try to use
      isAlpha(str) 
      to check the input but it doesn't compile...

      Would anybody knows how to solve it. Please help...

      I am also validating that it shouldn't have special characters....

      Thanks in advance.

      Regards,
      Psyve
        • 1. Re: Validate input if Alpha characters
          791266
          Have you written a method that is named isAlpha?

          Kaj
          • 2. Re: Validate input if Alpha characters
            807603
            No I haven't created a method... But would it be possible to use isLetter from Character class...I'm just a beginner in java....If you have any suggestions or a simple code please let me know...

            Thanks Kaj
            • 3. Re: Validate input if Alpha characters
              807603
              hi,
              post your error message..,
              or
              definition of isAlpha() method...
              • 4. Re: Validate input if Alpha characters
                807603
                psyeu wrote:
                No I haven't created a method... But would it be possible to use isLetter from Character class...I'm just a beginner in java....If you have any suggestions or a simple code please let me know...

                Thanks Kaj
                Some methods created for alphanumeric, now you to avoid numberic part validation and use this any of the method which is suitable to you...

                try this link

                http://forum.java.sun.com/thread.jspa?threadID=626568&messageID=3585242
                • 5. Re: Validate input if Alpha characters
                  807603
                  psyeu wrote:
                  But would it be possible to use isLetter from Character class
                  Yes it would be if you use it correctly. That method takes a char as a parameter not an entire String. So what do you think you need to do?
                  • 6. Re: Validate input if Alpha characters
                    791266
                    psyeu wrote:
                    No I haven't created a method... But would it be possible to use isLetter from Character class...I'm just a beginner in java....If you have any suggestions or a simple code please let me know...

                    Thanks Kaj
                    Yes, but you need to create a loop in that case since that method takes a character as argument and not a whole string. You must also call that method on the Character class.

                    Kaj
                    • 7. Re: Validate input if Alpha characters
                      807603
                      Hi,

                      This is the error code:
                      I already imported java.lang.String;
                      cannot find symbol
                      symbol  : method isAlpha(java.lang.String)
                      • 8. Re: Validate input if Alpha characters
                        807603
                        Because there is no magical isAlpha method. You need to write it.
                        • 9. Re: Validate input if Alpha characters
                          807603
                          psyeu wrote:
                          Hi,

                          This is the error code:
                          I already imported java.lang.String;
                          cannot find symbol
                          symbol  : method isAlpha(java.lang.String)
                          isAlpha () is not defined in your class,

                          so you have to create a method signature called isAlpha(String)


                          better you to get help regarding this in my previous reply
                          • 10. Re: Validate input if Alpha characters
                            807603
                            Hi drvijayy2k2 ,

                            I found your link helpful but when I modify the code :
                            I get a result true for a string that contains both number and letter...

                            I only need to return true if the string is letters (A to Z) only...
                            Do you have suggestions to solve this.... Thanks
                            public class SampIsAlpha {
                            
                                   /**
                                  * @param args
                                  */
                                 public static void main(String[] args) {
                                      // TODO Auto-generated method stub
                                      
                                   
                                   System.out.println(isAlpha("123a"));//returns true even if it contains number
                                   System.out.println(isAlpha("123"));//false -correct
                                   System.out.println(isAlpha("*-/"));//false - correct
                                   System.out.println(isAlpha("abc"));//true - correct
                                 
                                 }
                                 private static boolean isAlpha(String str)
                                 {
                                      //boolean blnNumeric = false;
                                      boolean blnAlpha = false;
                                      
                                      char chr[] = null;
                                      if(str != null)
                                      chr = str.toCharArray();
                                  
                                      //for(int i=0; i<chr.length; i++)
                                      //{
                                      //     if(chr[i] >= '0' && chr[i] <= '9')
                                      //     {
                                      //          blnNumeric = true;
                                      //          break;
                                      //     }
                                      //}
                                  
                                      for(int i=0; i<chr.length; i++)
                                      {
                                           if((chr[i] >= 'A' && chr[i] <= 'Z') || (chr[i] >= 'a' && chr[i] <= 'z'))
                                           {
                                                blnAlpha = true;
                                                break;
                                           }
                                      }
                                      //return (blnNumeric && blnAlpha);
                                           return (blnAlpha);
                                 }
                            
                            }
                            • 11. Re: Validate input if Alpha characters
                              807603
                              Thanks guys for the effort on answering my questions...
                              I figure it out, by using the original code, and replacing
                              return (blnNumeric && blnAlpha); 
                               
                              with 
                              
                              return (!blnNumeric && blnAlpha);
                              • 12. Re: Validate input if Alpha characters
                                807603
                                Hi psyeu ,


                                Replace ur if statement with following code

                                if(!((firstFive[i] >= 'A' && firstFive[i] <= 'Z') || (firstFive[i] >= 'a' && firstFive[i] <= 'z')))
                                               {
                                                    System.out.println(" Hi "+firstFive);
                                                    f = false;
                                                    break;
                                               }

                                Edited by: AbhishekForums on Dec 24, 2007 1:17 AM