Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#2908 closed defect (fixed)

[dojox.validate] isInRange does not handle numbers that start with a decimal

Reported by: guest Owned by: tk
Priority: high Milestone:
Component: Dojox Version: 0.4.2
Keywords: Cc:
Blocked By: Blocking:

Description

I attempted to validate a number with min and max of 0.3 and 1.3 respectively. I used ".5" and the method isInRange returned false. I think the code might assume that the number will have characters before the decimal point.

Change History (11)

comment:1 Changed 12 years ago by Adam Peller

Milestone: 0.9M2

comment:2 Changed 12 years ago by Adam Peller

Owner: changed from dylan to dante

This can be implemented now with two calls to dojo.Number and the necessary boolean logic.

comment:3 Changed 12 years ago by Adam Peller

Component: ValidateDojox

comment:4 Changed 12 years ago by dante

Summary: isInRange does not handle numbers that start with a decimal[dojox.validate] isInRange does not handle numbers that start with a decimal

comment:5 Changed 12 years ago by tk

Fix was submitted to Dante... (I cant do it from here) But here is the patched function:

dojox.validate.isInRange = function(/*String*/value, /*Object?*/flags){
	// summary:
	//	Validates whether a string denoting an integer, 
	//	real number, or monetary value is between a max and min. 
	//
	// value: A string
	// flags: {max:Number, min:Number, decimal:String}
	//    flags.max  A number, which the value must be less than or equal to for the validation to be true.
	//    flags.min  A number, which the value must be greater than or equal to for the validation to be true.
	//    flags.decimal  The character used for the decimal point.  Default is ".".
	
	if(isNaN(value)){
		return false; // Boolean
	}
	
	// assign default values to missing paramters
	flags = (typeof flags == "object") ? flags : {};
	var max = (typeof flags.max == "number") ? flags.max : Infinity;
	var min = (typeof flags.min == "number") ? flags.min : -Infinity;
	var dec = (typeof flags.decimal == "string") ? flags.decimal : ".";
	
	var cache = dojox.validate._isInRangeCache;
	var cacheIdx = value+"max"+max+"min"+min+"dec"+dec;
	if(typeof cache[cacheIdx] != "undefined"){
		return cache[cacheIdx];
	}
	
	// splice out anything not part of a number
	var pattern = "[^" + dec + "\deE+-]";
	value = value.replace(RegExp(pattern, "g"), "");
	// pad initial value with one 0 if it starts with a decimal character (fixes ticket #2908)
	value = (value.indexOf(dec)==0) ? '0'+value : value;
	// trim ends of things like e, E, or the decimal character
	value = value.replace(/^([+-]?)(D*)/, "$1");
	value = value.replace(/(D*)$/, "");
	// replace decimal with ".". The minus sign '-' could be the decimal!
	pattern = "(\d)[" + dec + "](\d)";
	value = value.replace(RegExp(pattern, "g"), "$1.$2");
	value = Number(value);
	if ( value < min || value > max ) { cache[cacheIdx] = false; return false; } // Boolean

	cache[cacheIdx] = true; return true; // Boolean
}

If Dante doesnt commit it before I get home, I will ;)

-Karl

comment:6 Changed 12 years ago by Adam Peller

Karl, this code needs to die. Can you use dojo.number for comparisons instead?

comment:7 Changed 12 years ago by tk

Owner: changed from dante to tk
Status: newassigned

comment:8 in reply to:  6 Changed 12 years ago by tk

Replying to peller:

Karl, this code needs to die. Can you use dojo.number for comparisons instead?

dojo.number does format matching... are you ok with adding isInRange to dojo.number? I see no functionality it its current functions to provide this...

I'm just fixing stuff that exists and is broken for Dante and something I may need very soon... let me know how/where you want it and I can certainly do it.

-Karl

comment:9 Changed 12 years ago by Adam Peller

dojo.number.parse

comment:10 Changed 12 years ago by Karl Tiedt

Resolution: fixed
Status: assignedclosed

(In [10805]) fixes #2908 Will be doing this via dojo.number in the near future, but closing out this ticket for now.

comment:11 Changed 12 years ago by Karl Tiedt

(In [10806]) refs #2908 updated dojox.validate.isInRange() to use dojo.number.parse

Note: See TracTickets for help on using tickets.