Number Format Validation

Number Validation using regular expression in java

Page 1 of 1

10 Replies - 33685 Views - Last Post: 27 November 2009 - 02:39 AM Rate Topic: -----

#1 q8mikan17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 23-January 09

Number Format Validation

Post icon  Posted 04 February 2009 - 09:59 AM

import java.io.*;
	public class Number{
		public static void main(String args[]){
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String num="";

		try{
			System.out.println("Please enter a number:");
			num=br.readLine();
			String numToken="[\\p{Digit}&&[123456789]]+";
			if(num.matches(numToken))
			System.out.println("you enter a VALID number format");
			else
			System.out.println("you enter a INVALID number format");
			}
			catch(Exception e){}
}
}


sample output:
Please enter a number:
1234
you enter a VALID number format
------
Please enter a number:
01234
you enter a INVALID number format
------

that code accept 1-9 no.'s only
but i need to include special character -,. with a conditon that . would only appear once but everytime time i use [-,.] the program error.

ex. i will input -123,111.00
this should be a VALID input

another ex.
i will input -123,111,000.00
still this is a VALID input

another ex.
i will input -123-111,000.00
it is INVALID input

but i don't know what to do for the program to accept those special character and for the - as well as . to appear once but , can appear more than once.

anyone knows what should i do?

*Edited: after more than 40 posts you should know the rules :code:

This post has been edited by pbl: 26 November 2009 - 10:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Number Format Validation

#2 Thura  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 25
  • Joined: 27-February 08

Re: Number Format Validation

Posted 04 February 2009 - 12:20 PM

How about this ?

public class Test {

	public static void main(String[] args) {
		String pattern = "[+-]?[\\d,]+\\.?\\d+";
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String num = "";
		while (true) {
			try {
				System.out.println("Please enter a number:");
				num = br.readLine();
				if (Pattern.matches(pattern, num)) {
					System.out.println("you enter a VALID number format");
				} else {
					System.out.println("you enter a INVALID number format");
				}
			} catch (Exception e) {}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 koki  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 02-January 09

Re: Number Format Validation

Posted 04 February 2009 - 12:31 PM

please use the
...
next time to make the code easy to read.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Number Format Validation

Posted 04 February 2009 - 08:23 PM

Please post your code :code:
This is to better help you
Was This Post Helpful? 0
  • +
  • -

#5 q8mikan17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 23-January 09

Re: Number Format Validation

Posted 05 February 2009 - 08:23 AM

thanks your code does help though it has one logical error.
when i input 23,44.00 it is valid we all know that there is no such 23,44.00 number format therefore it should be invalid.

23,440.00 --> valid
2,344.00 --> valid
23,44.00 --> invalid
Was This Post Helpful? 0
  • +
  • -

#6 Thura  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 25
  • Joined: 27-February 08

Re: Number Format Validation

Posted 05 February 2009 - 12:36 PM

This is a better pattern
String pattern = "[+-]?\\d{1,3}(,\\d{3})*\\.?\\d+";


Was This Post Helpful? 0
  • +
  • -

#7 rendra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-November 09

Re: Number Format Validation

Posted 25 November 2009 - 07:25 PM

How if the format must be 05/25/2009

that format mm/dd/YYYY

how it can be ???

thanks,

This post has been edited by rendra: 25 November 2009 - 07:27 PM

Was This Post Helpful? 0
  • +
  • -

#8 Thura  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 25
  • Joined: 27-February 08

Re: Number Format Validation

Posted 26 November 2009 - 08:47 PM

View Postrendra, on 25 Nov, 2009 - 06:25 PM, said:

How if the format must be 05/25/2009

that format mm/dd/YYYY

how it can be ???

thanks,


You have two options.
1. Use google.
2. Learn Regular Expressions.

I would recommend the latter. These are some examples of date validation in java.

http://www.devdaily....a-1-4-example-1
http://en.allexperts...-expression.htm
http://szc001.wordpr...proved-formula/
Was This Post Helpful? 1
  • +
  • -

#9 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Number Format Validation

Posted 26 November 2009 - 10:19 PM

Might I add the basic tutorial link
http://java.sun.com/...books/tutorial/

And the Java API link
http://java.sun.com/...1.4.2/docs/api/
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Number Format Validation

Posted 26 November 2009 - 10:25 PM

View Posttheautokustomizer, on 26 Nov, 2009 - 09:19 PM, said:

Might I add the basic tutorial link
http://java.sun.com/...books/tutorial/

And the Java API link
http://java.sun.com/...1.4.2/docs/api/

A link to API 1.4.2 ?
Even 1.5 is not supported by Sun since a few weeks....
http://java.sun.com/javase/6/docs/api/
this should do it until 1.7 that should come soon
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Number Format Validation

Posted 27 November 2009 - 02:39 AM

Not a particular good context for using regex. NumberFormat is designed for this

	NumberFormat nf = NumberFormat.getInstance();
	double d = nf.parse(someString).doubleValue();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1