Using a funcCall that retrieves multiple fields - javascript

Ello, I'm using jQuery-Validation-Engine and i don't know if this is currently possible but, Is there a way for you to possibly use the funcCall in a senario such as this.
I have to check a date range using 2 textboxes. I want to bind funcCall[dateRangeCheck] in such a way that I can get access
to 2 fields as opposed to just the one. The end result would would something like this.
dateRangeCheck = function (fields, rules, i, options) {
if (isDate(fields[0]) && isDate(fields[1])) {
if (!dateCompare(fields[0], fields[1])) {
return "* Invalid Date Range";
}
}
};
Or possibly all the fields that a utilizing this particular funcCall like this
dateRangeCheck = function (fields, rules, i, options) {
for (var i=0;i<fields.length;i++)
if(!isDate(fields[i]){
return "* Invalid Date Range";
}
}
};
Is there some way to accomplish this?

Well now jQuery-validation-engine does support date range checking. Just made the commit myself waiting for it to be reviewed. lol, But no to that second example as of now.
Here is the source. Have a nice day.
Update
The latest version does support group validation.

Related

Mongoose, Function Cannot Be Performed on Undefined, From Query Function

I am trying to use today's date as the field on which to
search - at this juncture, without having to worry about integrating the moment.js library. There will be only one record with today's date in the "table".
app.dashboardreport
.findOne()
.$where(function() {
return this.createdOn.toJSON().slice(0, 10) == Date.now().toJOSN().slice(0, 10);
})
.exec(function(err, foundDashboardReport) {
The $where function is throwing an error.
MongoError: TypeError: Cannot call method 'toJSON' of undefined
at _funcs1 (_funcs1:2:26) near 'createdOn.toJSON().slice(0, 10) == Date.n' (line 2)
It appears that "this" may not be defined; however, I know there is a record which meets those criteria.
Am I structuring the query incorrectly? If so, what needs to change in the function so that I can filter on that condition?
The problem, here, is a lack of understanding of the way in which data is structured in document-based storage. Each document must contain the createdOn field - if it is absent, then you will receive this error. this is defined; createdOn is not.
If you have data that is not uniform, you need to evaluate the presence of the field. So, it might look like this:
app.dashboardreport
.findOne()
.$where(function() {
/* The check for the field createdOn ensures that the compared document
does indeed contain the field */
return this.createdOn && this.createdOn.toDateString() == new Date().toDateString();
})
Note the addition of the this.createdOn evaluation in the return statement.
You get this error a CreatedOn is not present in every document. You could so something like this to get around this:
(this.createdOn|| new Date('31 Dec 9999')).toJSON()

Date range picker - invalid date

I am using this date range picke (http://www.daterangepicker.com/#options)
Here are some of options, I understand the most but I need help about "isInvalidDate"
I use this code and it works perfectly. Date 11/12/2015 is disabled and users can't select it.
isInvalidDate: function(date) {
if (date.format('YYYY-MM-DD') == '2015-11-12') {
return true;
} else {
return false;
}
},
But I need to add few dates to invalid, so user can't use them.
I don't know how to do some array and loop through to return true or false days, could anyone help me with this?
I hope it will help someone
var some_date_range = [
'02-04-2016',
'03-04-2016',
'04-04-2016',
'05-04-2016'
];
"isInvalidDate" : function(date){
for(var ii = 0; ii < some_date_range.length; ii++){
if (date.format('DD-MM-YYYY') == some_date_range[ii]){
return true;
}
}
}
You'll need a way to feed the blocked date from your backend to the client.
But let's suppose you solved that and have the dates in an array.
All you need to do then is to check if the date in in the array.
See e.g. here:
Checking if date belongs to array of dates
To get the invalid dates from the backend you could either put them in the script itself, of have the script fetch the to be blocked dates from the server using e.g. ajax.
Don't forget to revalidate on the server, never trust filtering in the clients to actually happen.

Detect SQL min date string in javascript

In an angularJS application I'm working on, we are using a SQL server to provide data. Nothing odd there. I am tasked with working on the date sanitation, so objects that are passed into our application that have the SQL min date value are undefined, and ones that do have a date are javascript Date objects.
I know that during upgrades, sometimes the string that is defined for min date can change. Right now, it is 0001-01-01T00:00:00Z. I cannot just do a string comparison because it could change.
My problem is, how do i determine this min date in javascript? new Date(varThatIsMinDate) is a valid date. MomentJS thinks its valid too, and it is technically a valid date. I'm less worried about validation and more worried about "is it min date"
How do you determine min date === true from a zulu pattern 0001-01-01T00:00:00Z or similar in javascript?
My code so far, cause I know you are gonna ask for it:
if (response.config.responseType === 'json') {
for (var property in response.data) {
if (response.data.hasOwnProperty(property)) {
if (property.toUpperCase().indexOf('DATE') > -1 || property.toUpperCase().indexOf('TIME') > -1) {
console.log(property.toUpperCase());
// Attempt to use JS built in date and validate
var tmpDate = new Date(response.data[property]);
if ($moment(tmpDate).isValid()) {
// Make it a valid date object if it has a valid date
response.data[property] = tmpDate;
} else {
// Make it undefined
response.data[property] = undefined;
}
}
}
}
}
Thing is, its always valid, so this code doesn't work.
EDIT: I could cheat and use tmpDate.getUTCFullYear() === 1 but i'd like to know how to do this the right way.

Backbone ModelBinder and jQuery Plugin

In an application written with Backbone & Marionette, I want some of the form inputs as numeric only and with thousands separator. Backbone ModelBinder automatically detects changes in the form. I implemented jQuery number plugin which works fine. But the problem is that when there is a thousands separator in a numeric input, ModelBinder doesn't work. When there is less than 4 digits (without a separator everything is ok.
The problem occurs on Chrome. There isn't any problem on Firefox.
I don't have any clue how to solve or debug the problem.
You're asking for trouble by combining the two: model binder triggers change events when inputs change and forwards the fields data to the model. Except it has been tampered with by the number plugin, so issues arise.
Instead, try using ModelBinder's converter binding settings (https://github.com/theironcook/Backbone.ModelBinder#formatting-and-converting-values), it will allow you to defined how data should be formatted/parsed when going from the model to the form and back.
Use ModelBinder's converters for this instead of the jQuery plug-in. Here's an example that cleans up a time (e.g. 3p, 3:00, 3PM, 15:00, 1500, etc.), stores the data in the model in a canonical form (time portion of ISO8601) if the input can be parsed, and if not then it is stored as is so that validation can check it separately and provide an error.
ModelBinder's converters get called twice when a user changes an input. First when the input data is copied from the view to the model, direction === 'ViewToModel'. This allows for cleanup to occur and to transform the input value into a canonical form suited for storage, e.g. in 24-hour time with seconds (15:30:00) in this example. And secondly, from the model back to the view, direction === 'ModelToView', which allows you to present the data to the user in a friendly manner, in 12-hour time (3:30 PM) in this example.
This example uses a time library to manipulate the time input, parse it, and format it.
Binding
In this case onRender is called right after the view is rendered using Backbone.Marionette
onRender: function() {
var bindings = ModelBinder.createDefaultBindings(this.$el, 'name');
bindings.value.converter = ModelSaver.timeStringConverter;
this.modelbinder.bind(this.model, this.$el, bindings);
}
Converter
ModelSaver.timeStringConverter = function(direction, value, attributeName, model, els) {
var result;
if (direction === 'ViewToModel') {
if (!value)
// if the input is empty, just pass it along so it can be deleted, and/or validation can indicate it was a required field
result = value;
else {
// parse the input value and store it with the second component only if it's valid, if not, store the invalid value so that model validation can catch it
result = new Time(value);
result = result.isValid() ? result.format('HH:mm')+':00' : value;
}
}
if (direction === 'ModelToView') {
// chop off the seconds, parse, check for validity, and format
result = value && new Time(value.substr(0,5));
result = (value && result.isValid()) ? result.format('h:mm AM') : value;
}
return result;
};

Quotes Required in Google Script Function Parameters?

Running into a bit of a weird issue here. I'm still learning the ins and outs of writing functions for Google Apps, and my first real project is a "shipping dashboard" - basically, a spreadsheet where I can take a tracking number from UPS, FedEx, etc., and from it parse the carrier, generate a tracking link, that type of thing. I'm trying to set it up as a function where I can set the type of data being requested (say, carrier), a tracking number, and have it return said information. Here's where I'm at right now:
function trackingData(infoType,trackingNumber) {
//Regex for various carrier's tracking numbers.
var upsValue = /\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|[\dT]\d\d\d ?\d\d\d\d ?\d\d\d)\b/i;
var fedexValue = /(\b96\d{20}\b)|(\b\d{15}\b)|(\b\d{12}\b)/;
var uspsValue = /\b(91\d\d ?\d\d\d\d ?\d\d\d\d ?\d\d\d\d ?\d\d\d\d ?\d\d|91\d\d ?\d\d\d\d ?\d\d\d\d ?\d\d\d\d ?\d\d\d\d)\b/i;
//First option: we want to know the carrier we're shipping with.
if (infoType == 'carrier') {
if (upsValue.test(trackingNumber)) {
return 'UPS';
}
else if (fedexValue.test(trackingNumber)) {
return 'FedEx';
}
else if (uspsValue.test(trackingNumber)) {
return 'USPS';
}
else return null;
}
The issue comes when passing a value for infoType - if I reference a cell, or set each infoType as a variable and put in a value directly when calling the formula it works just fine. However, if I call it by putting in a cell:
=infoType(carrier,trackingNumber)
I get:
error: Unknown range name carrier
The weird thing is it DOES work if I call it with:
=infoType("carrier",trackingNumber)
(note the quotes around "carrier").
I've looked all over the place to find a solution to keep from having to put the quotes around the formula when it is called but so far haven't had any luck. Anyone have ideas?
error: Unknown range name carrier
That error message is due to the way that the Spreadsheet is interpreting the function arguments.
A function parameter is either a reference or a data element (number, date, string). References can be to a single cell (A1) or range (A1..A23), to a range on a different sheet (Sheet2!A1), or a named range. When you entered carrier without quotes, it was interpreted as a named range. Read Named and protected ranges if you're interested.
Here's a little hint from the preamble text with the Google spreadsheets function list:
... don't forget to add quotation marks around all function components made of alphabetic characters that aren't referring to cells or columns.
Summary - if you want an argument to be passed as a string, put quotes on it.

Categories

Resources