Skip to content
This repository

Master 468 date validator #166

Closed
wants to merge 1 commit into from

1 participant

Jacob
Jacob

lighthouse 468, Form.Validator should not use Date.parse for validating
https://mootools.lighthouseapp.com/projects/24057/tickets/468-formvalidator-should-not-use-dateparse-for-validating

add check for correct date nouns in date validator for failing spec "boo
12" in chrome

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 23, 2010
Jacob lighthouse 468, Form.Validator should not use Date.parse for validating
https://mootools.lighthouseapp.com/projects/24057/tickets/468-formvalidator-should-not-use-dateparse-for-validating

add check for correct date nouns in date validator for failing spec "boo
12" in chrome
0d2dd23
This page is out of date. Refresh to see the latest.
24  Source/Forms/Form.Validator.js
@@ -422,21 +422,19 @@ Form.Validator.addAllThese([
422 422
 		},
423 423
 		test: function(element, props){
424 424
 			if (Form.Validator.getValidator('IsEmpty').test(element)) return true;
425  
-			var date;
426  
-			if (Date.parse){
427  
-				var format = props.dateFormat || '%x';
428  
-				date = Date.parse(element.get('value'));
429  
-				var formatted = date.format(format);
  425
+			var dateLocale = Locale.getCurrent().sets.Date,
  426
+				dateNouns = new RegExp([dateLocale.days, dateLocale.days_abbr, dateLocale.months, dateLocale.months_abbr].flatten().join('|'), 'i'),
  427
+				value = element.get('value'),
  428
+				wordsInValue = value.match(/[a-z]+/gi);
  429
+
  430
+				if (wordsInValue && !wordsInValue.every(dateNouns.exec, dateNouns)) return false;
  431
+
  432
+				var date = Date.parse(value),
  433
+					format = props.dateFormat || '%x',
  434
+					formatted = date.format(format);
  435
+
430 436
 				if (formatted != 'invalid date') element.set('value', formatted);
431 437
 				return date.isValid();
432  
-			} else {
433  
-				var regex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
434  
-				if (!regex.test(element.get('value'))) return false;
435  
-				date = new Date(element.get('value').replace(regex, '$1/$2/$3'));
436  
-				return (parseInt(RegExp.$1, 10) == (1 + date.getMonth())) &&
437  
-					(parseInt(RegExp.$2, 10) == date.getDate()) &&
438  
-					(parseInt(RegExp.$3, 10) == date.getFullYear());
439  
-			}
440 438
 		}
441 439
 	}],
442 440
 
5  Specs/1.3/Forms/Form.Validator.js
@@ -162,9 +162,14 @@ describe('Form.Validator', function(){
162 162
 
163 163
 			var validator = getValidator('validate-date');
164 164
 
  165
+			beforeEach(function(){
  166
+				Locale.use('en-US');
  167
+			});
  168
+
165 169
 			it('should return false for fields whose value is not a date', function(){
166 170
 				expect(validator.test(createInput('Mr. Foo'))).toEqual(false);
167 171
 				expect(validator.test(createInput('blah 12, 1000'))).toEqual(false);
  172
+				expect(validator.test(createInput('Boo 12'))).toEqual(false);
168 173
 			});
169 174
 
170 175
 			it('should return true for fields whose value parses to a date', function(){
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.