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

How to custom validation? #7

Open
cedarice opened this issue Oct 18, 2019 · 4 comments
Open

How to custom validation? #7

cedarice opened this issue Oct 18, 2019 · 4 comments

Comments

@cedarice
Copy link

I clone a date type, I want to validate the date user picked is two days after today or not, but i could not write the proper function to validate it.
please help me, thanks.

@herongyang
Copy link

var cf_my_date = {};
cf_my_date.two_day_after = function() {
var errors = [];
for (var i=0; i<rsv_custom_func_errors.length; i++) {
if (rsv_custom_func_errors[i].func != "cf_my_date.two_day_after") {
continue;
}
var field_name = rsv_custom_func_errors[i].field;
var fields = $("input[name^="" + field_name + ""]");
var field_value = fields.val();
var date_input = new Date(field_value);
var today = new Date();
var difference = (date_input - today)/(2436001000);
if (difference<2) {
var el = document.edit_submission_form[field_name];
errors.push([el, rsv_custom_func_errors[i].err]);
}
}
if (errors.length) {
return errors;
}

return true;
}

Hope the above JS function works for you.

Herong

@cedarice
Copy link
Author

var cf_my_date = {};
cf_my_date.two_day_after = function() {
var errors = [];
for (var i=0; i<rsv_custom_func_errors.length; i++) {
if (rsv_custom_func_errors[i].func != "cf_my_date.two_day_after") {
continue;
}
var field_name = rsv_custom_func_errors[i].field;
var fields = $("input[name^="" + field_name + ""]");
var field_value = fields.val();
var date_input = new Date(field_value);
var today = new Date();
var difference = (date_input - today)/(24_3600_1000);
if (difference<2) {
var el = document.edit_submission_form[field_name];
errors.push([el, rsv_custom_func_errors[i].err]);
}
}
if (errors.length) {
return errors;
}

return true;
}

Hope the above JS function works for you.

Herong

Thanks for you kindly reply.
I need a custom function in validation tab, but I cound not found it.

@herongyang
Copy link

"cf_my_date.two_day_after" is function to be used on the validation tab. But you also need enter the above code in the Displaying > JS tab.

@geprieto
Copy link

Almost perfect. But the code had some errors. This is what finally worked for me:

var cf_my_date = {};
cf_my_date.two_day_after = function() {
var errors = [];
for (var i=0; i<rsv_custom_func_errors.length; i++) {
if (rsv_custom_func_errors[i].func != "cf_my_date.two_day_after") {
continue;
}

var field_name = rsv_custom_func_errors[i].field;
var fields = $('input[name="'+ field_name + '"]');
var field_value = fields.val();
var date_input = new Date(field_value);
var today = new Date();
var difference = (date_input - today)/(2436001000);
if (difference<2) {
var el = document.edit_submission_form[field_name];
errors.push([el, rsv_custom_func_errors[i].err]);
}
}
if (errors.length) {
return errors;
}

return true;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants