|
•
•
•
•
| ||||||||||||||||||||
JavaScriptJavaScript is one of the tools we use to develop complex interactive survey applications.JavaScript is a programming language that is embedded into HTML documents. It controls the rendering of the HTML document and can be used to enhance the functionality of the questionnaire elements. JavaScript can be used for:
Some of the examples below show how JavaScript can be used in questionnaires.
Data Validation: Range CheckingIf you need values to be entered into a text box and want to restrict the range of acceptable values, the JavaScript code below will restrict the value entered and will only accept numbers from 0 to 5.
<Form>
<input type=text size=2
onBlur="this.value=(isNaN(this.value))?0:(this.value<0)?0:(this.value>5)?5:this.value;">
</Form>
Data Validation: Value DistributionIf you use an Ipsative Scale (one where the respondent allocates a fixed number of points to several boxes), the JavaScript code below will restrict the total number of points entered to 100.
function ComputeTotal(i) {
var GTotal;
eval('document.dataform.CS'+i+'Total.value=\'\'')
eval('GTotal=1*(document.dataform.CS'+i+'A.value)')
eval('GTotal+=(1*document.dataform.CS'+i+'B.value)')
eval('GTotal+=(1*document.dataform.CS'+i+'C.value)')
eval('GTotal+=(1*document.dataform.CS'+i+'D.value)')
eval('document.dataform.CS'+i+'GTotal.value=GTotal')
if(GTotal==100) {
eval('document.dataform.CS'+i+'Total.value=100')
}
}
Data Validation: Email Address SyntaxIf you need accurate values to be entered into email address fields, the JavaScript code below will check the data entered.
<Form>
<input type=text size=40
onBlur="filter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
if(!filter.test(this.value)) {alert('Invalid email syntax.');Form.EmailAddress.focus();}">
</Form>
Dynamic Generation of Questionnaire ElementsWhat if you had several locations and each location had a variety of departments. And you want to restrict the employees to selecting only the departments at their location. How could you do that in HTML? You could use conditional branching (offered by some of our competitors). But that would require more than one page for your questionnaire. What if you wanted the questionnaire to be on one page and still maintain this functionality. How could you do it in just one HTML page? Answer: JavaScript.
JavaScript can be used to dynamically generate questionnaire elements in real-time directly on the questionnaire as it is
Add commas to a numberIf your questionnaire requires a dollar amount (such as base salary or operating budget), you may want to have the number formatted with commas to enhance the accuracy of the data collected. This can easily be performed using a function similar to the one shown below:
function insert(MyValue) {
var re = /(-?\d+)(\d{3})/
var num = MyValue
while (re.test(num)) {
num = num.replace(re,"$1,$2")
}
return MyValue
}
Remove commas from a numberFor Example:
function remove(form) {
var re = /,/g
form.plainOutput.value = form.commaInput.value.replace(re,"")
}Interactive ShadingInternet Explorer allows for interactive shading of form elements. Just move the mouse over the elements below for an example.
clr=new Array('yellow','white','silver');
function highlight(state) {
element=event.srcElement;
if(element.tagName=='INPUT') {
etype=element.type;
if((etype=='submit' || etype=='reset') && state==1) state=2;
element.style.backgroundColor=clr[state];
element.focus();
}
}Conditional Enabling of ItemsInternet Explorer allows you to select certain items to be enabled or disabled. Just answer the items below for an example.
Example 1:Example 2:
|