Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing software version numbers

Status
Not open for further replies.

dirtyholmes

Programmer
Mar 17, 2004
43
AU
I am new to writing javascript code and i am trying to find a way of comparing software version numbers.

I have some code that needs to check a version of software on the users PC against the recomended version, and find out if the version on the PC is more recent.

eg

var minimumReqVers = 6.00.00.3

( the result of my check on the PC software version could be 6.0.3213 )

so effectively i have

var PCVersion = 6.0.3213

How therefore can i compare these two values to find which is the most recent, which in this example is the PC version.

 
Assuming you want to check from left to right, the best bet would to be:

- Convert the whole lot to a string
- Split on the '.' characters
- From left to right, convert each array position to a number, comparing it against the same position of the minimum requirements array. If any number is less, you know you've not met the requirements.

Something like this:

Code:
<html>
<script type="text/javascript">
	var minimumReqVers = '6.00.00.3';
	var PCVersion = '6.0.32.13';

	function hasMetMinimum(minVersion, installedVersion) {
		var minArray = minVersion.split('.');
		var installedArray = installedVersion.split('.');
		for (var loop=0; loop<minArray.length; loop++) {
			if (parseInt(installedArray[loop], 10) < parseInt(minArray[loop], 10)) return(false);
		}
		return(true);
	}

	onload = function() {
		alert('Does ' + PCVersion + ' meet the minimum version number requirements of ' + minimumReqVers + ' ?\n\n' + hasMetMinimum(minimumReqVers, PCVersion));
	}

</script>
</html>

Of course, it's not perfect. At the moment, there is no error checking to make sure both version strings have 4 sets of digits. If, for example, one had 4, and one had 3, unexpected results might occur.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top