Scripting example
Validating a number between 1 and 10
The example below shows scriptingProgramming language with a series of commands that are followed when the program runs used to implement sequencing, selectionUsed in a programme or algorithm to decide which command or set of commands is to be executed, repetitionAlso known as iteration. Defined as a smaller program to be executed several times in a main program and eventsAn event or action to be recognised and handled by the software programming in multimedia authoringThe creation of programmes and databases for applications or multimedia products software:
1 <script>
2 function myFunction() {
3 var x, text;
4
5 // Get the value of the input field with id="numb"
6 x = document.getElementById("numb").value;
7
8 // If x is Not a Number or less than one or greater than 10
9 if (isNaN(x) || x < 1 || x > 10) {
10 text = "Input not valid";
11 } else {
12 text = "Input OK";
13 }
14 document.getElementById("demo").innerHTML = text;
15 }
16 </script>
Line 3 is a statement declaring two variables x and text.
Line 6 is a statement that assigns the value from a HTML called numb to the variable x
Line 9 uses selectionUsed in a programme or algorithm to decide which command or set of commands is to be executed and boolean operators to determine if x is a number and if the value of x is less than 1 and greater than 10.
Line 10 is a statement that sets the value of the text variable if Line 9 evaluates to True.
Line 11 is the Else clause from the selection statement.
Line 12 is a statement that sets the value of the text variable if Line 9 evaluates to False.
Line 14 is a statement that sets a HTML element on the web page to the value of the text variable.
The image below demonstrates how this script is incorporated into a HTML web page. When the submit button is pressed (an eventsAn event or action to be recognised and handled by the software), the script is called, validates the number and returns the text to the page. If the value lies outside the range permitted, an error will be returned – in this case 'Input not valid'.