



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Examples of using the date and string objects in javascript to get and display the current date and time in various formats, as well as performing checks on user input in a registration form.
Typology: Exercises
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Lesson 15 JAVA SCRIPTING (CONTINUED….)
Example Date object Writing the Current Date and Time
Result is shown in Fig. 1 below. Here, ‘Date()’ is the constructor of the date object whichprovides current date of the system.
Fig. 1
Getting date and time in a user friendly format
To get the date/time in a different format, an instance of the date object can be created. In the following example‘d’ is such an instance. To define the instance d of the date object we have to use a constructor of the date object, preceded by the word ‘new’. Constructor is defined as the initializing function used to create instance/copy of an object. It is after the name of the object whose constructor it is. Note that we can invoke or apply different methods/functions of the date object using this instance ‘d’, e.g, d.getDay(), d.getYear() etc. Example - Current Date and Time
else if (hourValue == 12) { greeting = "Good afternoon!" timeText = " at " + hourValue + ":" + minuteValue + " PM" } else if (hourValue < 17) { greeting = "Good afternoon!" timeText = " at " + (hourValue-12) + ":" + minuteValue + " PM" } else { greeting = "Good evening!" timeText = " at " + (hourValue-12) + ":" + minuteValue + " PM" } // Write the greeting, the date, and the time to the page document.write(greeting + " It's " + dateText + timeText) //--> Result is shown in Fig. 2 below. Note that mainly three variables, greeting, dateText and timeText have been used. Also, a number of if statements have been used in order to get customized values.
Fig. 2
Example - String Object
In the following example, ‘str’ and ‘myArray’ are the instances of string and array objects, respectively. The size of the array is 10. Here, charAt() is the function/method of string object. So, charAt(3) would provide the value of the element at the index three. Different other functions of string object have also been used. In the example, str.Split(' ') splits the string on the basis of blank space. After splitting, we assign parts of the string as values for the array. Using the String object Using the String object
Result is shown in Fig. 3 below.
Fig. 3
See Fig. 4 below: Fig. 4
Fig. 4 Secondly, if the user violates the permissible limit of 3-15 characters in the text box for user login, again a pop-up box can confront him with a message as shown in Fig. 5 below.
Fig. 5 Similarly, if the user violates the permissible limit of 3-15 characters in respect of Password, an alert box can inform him about it as shown in Fig. 6 below.