/**
* Form Validation Utility - v1.0.0
* Developped By Digital Scorpions
* URL: http://www.digitalscorpions.in
*/
var dsFormData = new FormData();
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
!(function($) {
"use strict";
$(document).on('submit', 'form.ds-controled', function(e) {
e.preventDefault();
var f = $(this).find('.form-group'),
ferror = false,
mobilExp = /^[^\s0]\d{9}$/,
emailExp = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i;
//--- Validation :: Start
//--- Check all input fields
f.find(':input').each(function() {
var i = $(this);
//i.val($.trim(i.val));
var rule = i.attr('data-rule');
if (rule !== undefined) {
var ierror = false; // error flag for current input
var pos = rule.indexOf(':', 0);
if (pos >= 0) {
var exp = rule.substr(pos + 1, rule.length);
rule = rule.substr(0, pos);
} else {
rule = rule.substr(pos + 1, rule.length);
}
switch (rule) {
case 'required':
if (i.val() === '') {
ferror = ierror = true;
}
break;
case 'minlen':
if (i.val().length < parseInt(exp)) {
ferror = ierror = true;
}
break;
case 'mobile':
if (!mobilExp.test(i.val())) {
ferror = ierror = true;
}
break;
case 'email':
if (!emailExp.test(i.val())) {
ferror = ierror = true;
}
break;
case 'checked':
if (! i.is(':checked')) {
ferror = ierror = true;
}
break;
case 'selected':
if (! i.find(":selected").val()) {
ferror = ierror = true;
}
break;
case 'regexp':
exp = new RegExp(exp);
if (!exp.test(i.val())) {
ferror = ierror = true;
}
break;
}
if(i.hasClass('selectpicker'))
i.parent().next('.validate').html((ierror ? (i.attr('data-msg') !== undefined ? i.attr('data-msg') : 'wrong Input') : '')).show('blind');
else
i.next('.validate').html((ierror ? (i.attr('data-msg') !== undefined ? i.attr('data-msg') : 'wrong Input') : '')).show('blind');
}
});
if (ferror) return false;
//--- Validation :: End
//--- Submit :: Start
var this_form = $(this);
var action = $(this).attr('action');
if( ! action ) {
this_form.find('.loading').slideUp();
showMessage("error", 'The form action property is not set!');
return false;
}
this_form.find('.loading').slideDown();
ds_form_submit(this_form, action);
return true;
});
function ds_form_submit(this_form, action) {
var MainFArr = this_form.serializeArray();
var TempData = new FormData();
for (var i=0; i < MainFArr.length; i++){
//alert(MainFArr[i].name + ' = ' + MainFArr[i].value);
TempData.append(MainFArr[i].name, MainFArr[i].value);
}
for(var pair of dsFormData.entries()){
//alert(pair[0] + ', ' + pair[1]);
TempData.append(pair[0], pair[1]);
}
$.ajax({
type: "POST",
url: action,
processData: false,
contentType: false,
data: TempData,
timeout: 40000
}).done( function(ReturnValue){
this_form.find('.loading').slideUp();
var msgType = 'error';
if(isJson(ReturnValue)){
res = $.parseJSON(ReturnValue);
if(res.cd){
this_form.find('.ds-keyfield').val(res.cd);
msgType = 'success';
if(!this_form.hasClass('ds-hold-value')){
this_form.find(":input:not([submit], #submit-mode, )").val('');
this_form.find('.ds-keyfield').val('-1');
}
if(typeof loadList == 'function')
loadList();
}
showMessage(msgType, res.msg);
} else {
if(ReturnValue.indexOf('||') >=0)
var res = ReturnValue.split("||");
else
var res = ['info', ReturnValue];
if(!this_form.hasClass('ds-hold-value'))
this_form.find(":input:not([submit], #submit-mode)").val('');
showMessage(res[0], res[1]);
//-- Check if Followup Function Exists in App
if(res[0] == 'success'){
if(typeof afterSave == 'function')
afterSave(this_form);
}
}
}).fail( function(data){
var error_msg = "Form submission failed!
";
if(data.statusText || data.status) {
error_msg += 'Status:';
if(data.statusText) {
error_msg += ' ' + data.statusText;
}
if(data.status) {
error_msg += ' ' + data.status;
}
error_msg += '
';
}
if(data.responseText) {
error_msg += data.responseText;
}
this_form.find('.loading').slideUp();
showMessage("error", error_msg);
});
}
$(document).on('change', 'input:file', function(){
const fi = $(this);
const mDt = new Date();
const mMaxSize = fi.attr('data-maxsize')?fi.attr('data-maxsize'):0;
const mMaxNo = fi.attr('data-maxno')?fi.attr('data-maxno'):0;
const mType = fi.attr('accept')?fi.attr('accept'):'';
const mPreview = fi.attr('data-preview')?fi.attr('data-preview'):'';
const mId = fi.attr('PreCd') + '-' + mDt.getTime();
const mTotFiles= this.files.length;
var mAddedNo = 0;
if(mPreview != ''){ mAddedNo = $('#' + mPreview).children().length;}
var i = 0;
var mRowId = '';
//-- If No File Selected
if (mTotFiles <= 0) {return false;}
var BigFiles = '';
var Existing = '';
var NoFormat = '';
var mCount = 0;
//-- Check validity
for(i=0; i < mTotFiles; i++) {
var mOk = true;
//-- If File is bigger than allowed size
if (Math.round(this.files.item(i).size/1024/1024) > mMaxSize && mMaxSize != 0){
if(BigFiles != ''){BigFiles += ', ';}
BigFiles += this.files.item(i).name;
mOk = false;
}
//-- If already selected
if($('#' + mPreview).find('span:contains(' + this.files.item(i).name + ')').length > 0){
if(Existing != ''){Existing += ', ';}
Existing += this.files.item(i).name;
mOk = false;
}
//-- If not of allowed file format
if(mType.indexOf(this.files.item(i).type) < 0 && mType != ''){
if(NoFormat != ''){NoFormat += ', ';}
NoFormat += this.files.item(i).name;
mOk = false;
}
if(!mOk){mCount++;}
}
//-- Show Messages
if(BigFiles != ''){
var posB = BigFiles.lastIndexOf(',');
BigFiles = BigFiles.substring(0, posB) + ' and ' + BigFiles.substring(posB+1);
showMessage("error", 'File size of ' + BigFiles + ' is more than ' + mMaxSize + ' Mb.');
}
if(Existing != ''){
var posE = Existing.lastIndexOf(',');
Existing = Existing.substring(0, posE) + ' and ' + Existing.substring(posE+1);
showMessage("warning", 'File ' + Existing + ' already selected.');
}
if(NoFormat != ''){
var posF = NoFormat.lastIndexOf(',');
NoFormat = NoFormat.substring(0, posF) + ' and ' + NoFormat.substring(posF+1);
showMessage("warning", 'File ' + NoFormat + ' is of unsupported format.');
}
//-- If Total no. of files is more than allowed no.
if(mTotFiles + mAddedNo - mCount > mMaxNo && mMaxNo > 0){
showMessage("error", "You are trying to select " + (mTotFiles + mAddedNo - mCount) + " file(s). But maximum " + mMaxNo + " file(s) can be selected.");
return false;
} else if(mPreview != '') {
for(i=0; i < mTotFiles; i++) {
//-- If within size limit
if (Math.round(this.files.item(i).size/1024/1024) <= mMaxSize || mMaxSize == 0){
if($('#' + mPreview).find('span:contains(' + this.files.item(i).name + ')').length <= 0){
if(mType.indexOf(this.files.item(i).type) >= 0 || mType == ''){
mRowId = 'F' + mId + '[' + i + ']';
dsFormData.append(mRowId, this.files.item(i));
$('#' + mPreview).prepend($('