Results 1 to 4 of 4
  1. #1
    lockmac is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

    Default java validation?

    Hi.. i need to validate two different Strings.

    The first String has to only be characters and no numbers
    The second String has to be only numbers and no characters. How would I go about doing this?

    Many thanks

  2. #2
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    I would recommend you use a couple of regex functions to test this. That would probably be the easiest way of doing it:
    Java Code:
    String numbers = "0123456789";
    		String letters = "abcde";
    		String rubbish="$%^&£\"";
    		Pattern numberPattern = Pattern.compile("^[0-9]+$");
    		Pattern letterPattern = Pattern.compile("^[a-zA-Z]+$");
    		
    		System.out.print("Numbers should match number pattern: ");
    		System.out.println(numberPattern.matcher(numbers).matches());
    		
    		System.out.print("Numbers should not match letter pattern: ");
    		System.out.println(letterPattern.matcher(numbers).matches());
    		
    		System.out.print("Letters should match letter pattern: ");
    		System.out.println(letterPattern.matcher(letters).matches());
    		
    		System.out.print("Letters should not match number pattern: ");
    		System.out.println(numberPattern.matcher(letters).matches());
    		
    		System.out.print("Rubbish should not match number pattern: ");
    		System.out.println(numberPattern.matcher(rubbish).matches());
    		
    		System.out.print("Rubbish should not match letter pattern: ");
    		System.out.println(letterPattern.matcher(rubbish).matches());
    I would recommend making the Patterns private static final in your class so they are only compiled once this will make it pretty efficient.

    Hope that helps,
    Shane.

  3. #3
    lockmac is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

    Default

    Thanks for the reply... im not too sure though how to use your code..

    im learning java at the moment and not too advanced.. learning interface class at the moment.

    thanks

  4. #4
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    OK I'll put it into context a bit more then:
    Java Code:
    import java.util.regex.Pattern;
    
    /**
     * Validates String objects.
     * 
     * @author Shane Preater
     */
    public class StringValidator {
    	private static final Pattern numberPattern = Pattern.compile("^[0-9]+$");
    
    	private static final Pattern letterPattern = Pattern.compile("^[a-zA-Z]+$");
    
    	/**
    	 * Tests the given string contains only numbers.
    	 * 
    	 * @param string
    	 *            the string to test
    	 * @return <code>true</code> is it only contains numbers.
    	 */
    	public static boolean isAllNumbers(String string) {
    		return numberPattern.matcher(string).matches();
    	}
    
    	/**
    	 * Tests the given string contains only letters.
    	 * 
    	 * @param string
    	 *            the string to test
    	 * @return <code>true</code> is it only contains letters.
    	 */
    	public static boolean isAllLetters(String string) {
    		return letterPattern.matcher(string).matches();
    	}
    }
    This will allow you to simply test your strings against the methods then in your main method you can do:
    Java Code:
    public class Main {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		String numberString = "123";
    		StringValidator.isAllNumbers(numberString);
    
    		String letterString = "abc";
    		StringValidator.isAllLetters(letterString);
    	}
    
    }
    Obviously you will need to edit the code in the Main class to get the strings from where ever you are expecting them and then call the methods as per the example.

    If you need more help give me a shout.
    Shane.

Similar Threads

  1. Live Email Validation In Java
    By shameel in forum Web Frameworks
    Replies: 9
    Last Post: 01-05-2010, 06:42 AM
  2. Struts validation
    By Jack in forum Web Frameworks
    Replies: 3
    Last Post: 10-06-2008, 01:54 AM
  3. Swing Validation
    By ppayal in forum AWT / Swing
    Replies: 0
    Last Post: 02-09-2008, 10:00 AM
  4. Swing validation
    By ppayal in forum New To Java
    Replies: 0
    Last Post: 02-09-2008, 09:59 AM
  5. javascript validation
    By yuchuang in forum New To Java
    Replies: 5
    Last Post: 05-14-2007, 04:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •