±«Óătv

Scripting example

Validating a number between 1 and 10

The example below shows used to implement sequencing, , and programming in multimedia 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 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 ), 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'.

An example of a script designed to validate data