Validation for Version number

JAVAians

i have a small requirement which i am doin in java swing. i need to validate a field which will accept only numbers and dots.

for example:

i have a version number field which has to accept only in format like ***.***.***.***

but the numbers between the dots can vary accordingly. and the length can also vary

note:

[1.0.0.1] or [111.0.0.0] or [111.11.0.0] or [111.111.12.1] and so on...

or

[1.0] or [12.0] or [1.2.0]

PraDz

[492 byte] By [VickyPrada] at [2007-10-3 4:21:54]
# 1
Look at Pattern and regex and think about "\\d+(\\.\\d+)*" (might need some fixing, but you should get the idea)
CeciNEstPasUnProgrammeura at 2007-7-14 22:24:09 > top of Java-index,Java Essentials,Java Programming...
# 2
A custom [url= http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/DefaultFormatter.html]DefaultFormatter[/url][url= http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html]How to Use Formatted Text Fields[/url]
mlka at 2007-7-14 22:24:09 > top of Java-index,Java Essentials,Java Programming...
# 3

Simple IP-address matching

\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

but this will allow i.e. 666.666.666.666 (and we all know who has that IP...)

Complex:

\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

(all in one line)

Lajma at 2007-7-14 22:24:09 > top of Java-index,Java Essentials,Java Programming...
# 4
thank lajmbut this will not solve my problem,.this will allow numbers and dots and we cant make it optinal like 1.0.0.0 or 1.0.0 or 1.0.0.0
VickyPrada at 2007-7-14 22:24:09 > top of Java-index,Java Essentials,Java Programming...
# 5

> thank lajm

>

> but this will not solve my problem,.

>

> this will allow numbers and dots and we cant make it

> optinal like 1.0.0.0 or 1.0.0 or 1.0.0.0

Ah, you mean like this:

String numbers = "(?:[0-9]|[1-9][0-9]|[1-9][0-9][0-9])";

Pattern versionPattern = Pattern.compile("^(?:"+numbers+"\\.){0,3}"+numbers+"$");

This will allow any version number that match a number from 0 to 999 and it will require at least one but at most 4 levels.

A singel "1" would match, 1.1, 1.4.2 and 1.5.0.2 but not 8.1.9.1.4.

If you want to restrict it so that you only alow "major.minor" (ie. 1.0), then change the {0,3} to {1,3}.

Lajma at 2007-7-14 22:24:09 > top of Java-index,Java Essentials,Java Programming...