Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master 468 date validator #166

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions Source/Forms/Form.Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,21 +422,19 @@ Form.Validator.addAllThese([
},
test: function(element, props){
if (Form.Validator.getValidator('IsEmpty').test(element)) return true;
var date;
if (Date.parse){
var format = props.dateFormat || '%x';
date = Date.parse(element.get('value'));
var formatted = date.format(format);
var dateLocale = Locale.getCurrent().sets.Date,
dateNouns = new RegExp([dateLocale.days, dateLocale.days_abbr, dateLocale.months, dateLocale.months_abbr].flatten().join('|'), 'i'),
value = element.get('value'),
wordsInValue = value.match(/[a-z]+/gi);

if (wordsInValue && !wordsInValue.every(dateNouns.exec, dateNouns)) return false;

var date = Date.parse(value),
format = props.dateFormat || '%x',
formatted = date.format(format);

if (formatted != 'invalid date') element.set('value', formatted);
return date.isValid();
} else {
var regex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
if (!regex.test(element.get('value'))) return false;
date = new Date(element.get('value').replace(regex, '$1/$2/$3'));
return (parseInt(RegExp.$1, 10) == (1 + date.getMonth())) &&
(parseInt(RegExp.$2, 10) == date.getDate()) &&
(parseInt(RegExp.$3, 10) == date.getFullYear());
}
}
}],

Expand Down
5 changes: 5 additions & 0 deletions Specs/1.3/Forms/Form.Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,14 @@ describe('Form.Validator', function(){

var validator = getValidator('validate-date');

beforeEach(function(){
Locale.use('en-US');
});

it('should return false for fields whose value is not a date', function(){
expect(validator.test(createInput('Mr. Foo'))).toEqual(false);
expect(validator.test(createInput('blah 12, 1000'))).toEqual(false);
expect(validator.test(createInput('Boo 12'))).toEqual(false);
});

it('should return true for fields whose value parses to a date', function(){
Expand Down