Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#2264 closed defect (fixed)

dojo.regexp.realNumber needs option for no leading zero

Reported by: Michael Schall Owned by: Adam Peller
Priority: high Milestone:
Component: Validate Version: 0.4.1
Keywords: Cc:
Blocked By: Blocking:

Description

Real numbers should not require a leading zero. Either by default or a flag should allow

    alert(dojo.validate.isRealNumber(".25", {integer: [true, false]}))

to alert true. Since isRealNumber utilizes dojo.regexp.realNumber which in turn utilizes dojo.regexp.integer to build the regular expression, dojo.regexp.integer needs the ability to return "" as a valid integer.

The following code should take care of issue. I will include a diff as well...

dojo.regexp.integer = function(flags) {
	...
	if (typeof flags.integer == "undefined") { flags.integer = true; }	
	...
	// number RE
	var numberRE = dojo.regexp.buildGroupRE(flags.separator,
		function(sep) { 
			var initialDigit = dojo.regexp.buildGroupRE(flags.integer, 
				function (integer) {
					return integer ? "\d" : ""
				}, true);			
			if ( sep == "" ) { 
				return "(" + initialDigit + "\d*)";
			}
			var grp = flags.groupSize, grp2 = flags.groupSize2;
			if ( typeof grp2 != "undefined" ) {
				var grp2RE = "(" + initialDigit + "\d{0," + (grp2-1) + "}([" + sep + "]\d{" + grp2 + "})*[" + sep + "]\d{" + grp + "})";
				return ((grp-grp2) > 0) ? "(" + grp2RE + "|(0|[1-9]\d{0," + (grp-1) + "}))" : grp2RE;
			}
			return  "(" + initialDigit + "\d{0," + (grp-1) + "}([" + sep + "]\d{" + grp + "})*)";
		}
	);

	// integer RE
	return (signRE + numberRE);
}

/**
  Builds a regular expression to match a real number in exponential notation.
    ...
    flags.integer  Whether integer portion is allowed.
      Can be true, false, or [true, false].  Default is [true, false]
    ...
*/
dojo.regexp.realNumber = function(flags) {
	...
	if (typeof flags.integer == "undefined") { flags.integer = [true, false]; }
	...
}

Attachments (1)

regexp.patch (2.7 KB) - added by Michael Schall 13 years ago.

Download all attachments as: .zip

Change History (6)

Changed 13 years ago by Michael Schall

Attachment: regexp.patch added

comment:1 Changed 13 years ago by Adam Peller

Owner: changed from dylan to Adam Peller

comment:2 Changed 13 years ago by Michael Schall

Unfortunately the patch I gave is flawed. It will allow empty string as a valid real number.

The reason for this is the current implementation uses the integer portion as the required portion of the string. So a possible way of fixing my patch would be to have the regular expression that has an or where either the integer portion is required or the decimal portion is required...

Messy!

Any takers? :)

comment:3 Changed 12 years ago by Adam Peller

(In [7364]) References #2264

comment:4 Changed 12 years ago by Adam Peller

Resolution: fixed
Status: newclosed

(In [7365]) Fixes #2264. Thanks, schallm.

comment:5 Changed 12 years ago by Adam Peller

Not that messy at all, really, but please check what I've done and let me know if it messes anything else up. In particular, I suggest trying dojo.number as your interface to parsing and validating numbers. The parse() method should return the value, or if you prefer a regexp match, try regexp() instead. This should eventually give you a localized way of checking input. I didn't think the flag was necessary, but we may revisit that.

Note: See TracTickets for help on using tickets.