(function($){
var allData = {};
var inst_value = '';
var field_name = '';
var error = false;
var $form = $('#submitForm');
var $submitButton = $('#submitButton');
var $topicField = $('#topicField');
var $descriptionField = $('#descriptionField');
var $nameField = $('#nameField');
var $emailField = $('#emailField');
var $allFields = $('.selectionField');
function bindEvents(){
$submitButton.click(function() {
getInputFields();
validateFields();
if (!error) {
submitData(allData);
}
});
//clear out error message on focus
$allFields.focus(function() {
var string = '
';
var fieldName = $(this).attr('id');
switch(fieldName) {
case 'StudentPhoneNumber':
string = '
Example: xxx-xxx-xxxx';
break;
}
$(this).parent().children('.errorField').empty();
$(this).parent().children('.errorField').append(string);
});
}
function getInputFields(){
var field_length = Number($('.selectionField').length) - 1;
$allFields.each(function(e, p) {
field_name = $(this).attr('id').trim();
inst_value = $(this).val().trim();
allData[field_name] = inst_value;
});
}
function submitData(allValues){
var requestDigest = $("#__REQUESTDIGEST").val();
var url = "/_api/web/lists/GetByTitle('ViewerQuestions')/items";
var item = {
"__metadata":{
"type":"SP.Data.ViewerQuestionsListItem"
},
"Title" : allValues.nameField,
"Description": allValues.descriptionField,
"Email": allValues.emailField
};
//clear all fields
$allFields.each(function(g, k) {
var k_type = ($(this).attr('type') === 'text');
if (k_type) {
$(this).val('');
} else {
$(this).prop('selectedIndex', 0);
}
});
$.ajax({
url: url,
method: "POST",
data: JSON.stringify(item),
contentType: "application/json;odata=verbose",
headers: { "Accept": "application/json;odata=verbose",
"X-RequestDigest" : requestDigest
},
success: function (data, request) {
var shareNewsFormHTML = $form.html(),
string_To_Append = '
Thank you for your submission!
' +
//'Reload Form. ' +
'
';
appendData = function() {
$form.empty();
$form.append(string_To_Append);
};
appendData();
$('#reloadForm').click(function() {
string_To_Append = shareNewsFormHTML;
appendData();
initEvents();
});
},
error: function (xhr, ajaxOptions, thrownError) {
console.ReportAjaxError('Error creating item', xhr, ajaxOptions, thrownError);
}
});
}
function validateFields(){
error = false;
$.each(allData, function(e, h) {
var errorString = 'Please enter a valid value.';
var errorInFields = false;
switch(true) {
case (e == 'nameField' || e == 'emailField'):
//ingore validation on these fields
break;
case (h === ''):
case (h === 'undefined'):
case (typeof h === 'undefined'):
if (e !== 'StudentPhoneNumber') {
errorInFields = true;
}
break;
case (e === 'StudentPhoneNumber'):
if (h.match(/^\d{3}-\d{3}-\d{4}$/) >= 0) {
errorString = 'Please enter a valid phone number. Example xxx-xxx-xxxx';
errorInFields = true
}
break;
case (e === 'emailField'):
if (h.indexOf('@') === -1 || (h.indexOf('.edu') === -1 && h.indexOf('.net') === -1 && h.indexOf('.com') === -1)) {
errorString = 'Please enter a valid email address. Example
[email protected]';
errorInFields = true
} else {
var domain = h.substr(h.indexOf('@') + 1, h.lastIndexOf('.'));
domain = domain.substr(0, domain.lastIndexOf('.'));
if (domain === '') {
errorString = 'Please enter a valid email address. Example
[email protected]';
errorInFields = true
}
}
break;
}
if (errorInFields) {
$('[id="' + e + '"]').parent().children('.errorField').text(errorString);
error = true;
}
});
}
function init(){
bindEvents();
}
init();
})(jQuery);