Difference between revisions of "Check whether values of open parts of a close question are positive numbers or not"

From Catglobe Wiki
Jump to: navigation, search
Line 5: Line 5:
 
This solution will check whether values of open parts of a close question (single question type or multi question type) are positive numbers or not.
 
This solution will check whether values of open parts of a close question (single question type or multi question type) are positive numbers or not.
  
[[File:OpenPart_closeQuestion.jpg]]
+
[[File:OpenPart closeQuestion.jpg]]
  
 
=== How To Use ===
 
=== How To Use ===
<br/>
+
 
*'''Edit the qName and openPartIndexes values in the below script.'''  
+
 
 +
 
 +
*'''Edit the qName and openPartIndexes values in the below script.'''
  
 
<source lang="javascript">
 
<source lang="javascript">
/********************************************************************************************
 
* CHECK WHETHER VALUES OF OPEN PARTS OF A CLOSE QUESTION ARE POSITIVE NUMBERS OR NOT
 
********************************************************************************************/
 
 
/**
 
/**
  * Check whether values of open parts of a close question are positive numbers or not
+
  * Check whether values of open parts of a close question are positive numbers or not
  * @param {String} qName The name of the close question (case sensitive)
+
  * @param {String} qName The name of the close question (case sensitive)
  * @param {Array} openPartIndexes The indexes of the open parts
+
  * @param {Array} openPartIndexes The indexes of the open parts
  * @return {Boolean} return true if valid, false if invalid
+
  * @return {Boolean} return true if valid, false if invalid  
  *     Ex: validate_Open_Parts_Of_Close_Question('Sp7', [2,3]); // Means check whether values of open parts of answer options 2,3 of the close  
+
  * Ex: validate_Open_Parts_Of_Close_Question('Sp7', [2,3]); // Means check whether values of open parts of answer options 2,3 of the close  
  *                                                               // question Sp7 are positive numbers or not
+
  * // question Sp7 are positive numbers or not
  */
+
  */  
 
function validate_Open_Parts_Of_Close_Question(qName, openPartIndexes) {
 
function validate_Open_Parts_Of_Close_Question(qName, openPartIndexes) {
function isPositiveNumber(n) {
+
function isPositiveNumber(n) {
        var isNo = !isNaN(parseFloat(n)) && isFinite(n);
+
var isNo = !isNaN(parseFloat(n)) && isFinite(n);
return isNo && (Number(n) >= 0) ? true : false;
+
return isNo && (Number(n) >= 0) ? true : false;  
}
+
}
+
if(!questioncheck())
+
if(!questioncheck())  
return false;
+
return false;
+
var isValid = true;
+
var isValid = true;
$.each(openPartIndexes, function(i, opIndex) {
+
$.each(openPartIndexes, function(i, opIndex) {  
var opTxtBoxName = 'QUESTION.' + qName + '.Open.' + opIndex;
+
var opTxtBoxName = 'QUESTION.' + qName + '.Open.' + opIndex;  
var opValue = $('input[type=text][name=' + opTxtBoxName + ']').val();
+
var opValue = $('input[type=text][name=' + opTxtBoxName + ']').val();
if(opValue !== '' && !isPositiveNumber(opValue)) {
+
if(opValue !== '' && !isPositiveNumber(opValue)) {
isValid = false;
+
isValid = false;
return false; // break this each loop
+
return false; // break this each loop
}
+
}  
});
+
});
+
if(!isValid) {
+
if(!isValid) {
alert('Værdien skal være et tal.');
+
alert('Værdien skal være et tal.');
return false;
+
return false;
}
+
}
+
return true;
+
return true;
 
}
 
}
  
 
quest.next = function() {
 
quest.next = function() {
// Valiate the open parts of close question to make sure their values are positive numbers
+
// Valiate the open parts of close question to make sure their values are positive numbers
var qName = 'Sp7';         // Change your close question name here
+
var qName = 'Sp7'; // Change your close question name here
var openPartIndexes = [2]; // Set the open part indexes which want to validate values here
+
var openPartIndexes = [2]; // Set the open part indexes which want to validate values here
var isValid = validate_Open_Parts_Of_Close_Question(qName, openPartIndexes);
+
var isValid = validate_Open_Parts_Of_Close_Question(qName, openPartIndexes);
if(!isValid)
+
if(!isValid)
return false;
+
return false;
+
document["query"]["dir"].value = "next";
+
document["query"]["dir"].value = "next";  
return true;
+
return true;  
 
};
 
};
 
</source>
 
</source>
<br/>
+
 
 
*'''Put the script to below location of your close question in the questionnaire editor'''
 
*'''Put the script to below location of your close question in the questionnaire editor'''
  
[[File:Question_Javascript_Property.jpg]]
+
[[File:Question Javascript Property.jpg]]

Revision as of 06:33, 11 November 2013

Check whether values of open parts of a close question are positive numbers or not

Description

This solution will check whether values of open parts of a close question (single question type or multi question type) are positive numbers or not.

OpenPart closeQuestion.jpg

How To Use

  • Edit the qName and openPartIndexes values in the below script.
/**
 * Check whether values of open parts of a close question are positive numbers or not
 * @param {String} qName The name of the close question (case sensitive)
 * @param {Array} openPartIndexes The indexes of the open parts
 * @return {Boolean} return true if valid, false if invalid 
 * Ex: validate_Open_Parts_Of_Close_Question('Sp7', [2,3]); // Means check whether values of open parts of answer options 2,3 of the close 
 * // question Sp7 are positive numbers or not
 */ 
function validate_Open_Parts_Of_Close_Question(qName, openPartIndexes) {
 function isPositiveNumber(n) {
 var isNo = !isNaN(parseFloat(n)) && isFinite(n);
 return isNo && (Number(n) >= 0) ? true : false; 
 }
 
 if(!questioncheck()) 
 return false;
 
 var isValid = true;
 $.each(openPartIndexes, function(i, opIndex) { 
 var opTxtBoxName = 'QUESTION.' + qName + '.Open.' + opIndex; 
 var opValue = $('input[type=text][name=' + opTxtBoxName + ']').val();
 if(opValue !== '' && !isPositiveNumber(opValue)) {
 isValid = false;
 return false; // break this each loop
 } 
 });
 
 if(!isValid) {
 alert('Værdien skal være et tal.');
 return false;
 }
 
 return true;
}

quest.next = function() {
 // Valiate the open parts of close question to make sure their values are positive numbers
 var qName = 'Sp7'; // Change your close question name here
 var openPartIndexes = [2]; // Set the open part indexes which want to validate values here
 var isValid = validate_Open_Parts_Of_Close_Question(qName, openPartIndexes);
 if(!isValid)
 return false;
 
 document["query"]["dir"].value = "next"; 
 return true; 
};
  • Put the script to below location of your close question in the questionnaire editor

Question Javascript Property.jpg