The only thing left to do is to return to the validate function. After all the other four functions have been executed, the four variables will either be true or false. We only want to send the form if all four variables are true. If they are not then the user has made an error and we need to keep them on the same page. Here's the rest of the code for the validate function:

Showing posts with label Java Script. Show all posts
Showing posts with label Java Script. Show all posts
Thursday, 4 June 2015
Checkboxes
The final function we need is for the checkbox. You can have 1 or more checkboxes on a form. We only have 1, so checking it is fairly straightforward. If you have more than checkbox, though, you do the testing in exactly the same way as for radio buttons.
But here's the code for the checkCheckbox function:
Dropdown Lists
The code to check the dropdown list is more or less the same as for radio buttons. Here it is:

Radio Buttons
Checking radio buttons on a form is a little trickier. You have to loop through each button, testing for a value of checked, which means it was selected. Only one radio button in a group can be selected, so if you find a value of checked you can break out of the loop.
If no radio button was checked then you can return a value of false. If any one of them is checked then you can return a value of true. Here's the code for your radio button function:
Javascript Form Validation - the email function
We'll start with the function that checks the email address text box. We'll only check if the text box has had anything typed into it. Checking for a correct email address is notoriously difficult, so we're just trying to keep things simple here.
Add the following function to your code (you won't be able to copy and paste because ours is an image - you'll learn more if you type it out for yourself):
Wednesday, 3 June 2015
Javascript and HTML Forms
You can use Javascript to check that form elements have been filled in correctly. If not, you can keep your users on the same page and display an error message. If everything is OK then you can submit the form. You'll see how that works now.
SEE CODE BELLOW...

SEE CODE BELLOW...
Javascript Dates and Time
The first thing you need to do is to create a new Date object:
var the_date = new Date();
Note the use of the keyword new. After a space you type the word Date (capital "D"). This is followed by a pair of round brackets. This is enough to create a new date object, which we've assign to a variable called the_date. To see what this prints out, add the following
Function Arguments in Javascript
You can pass values over to your functions. These values are called arguments, and they go between the round brackets of the function. If you have more than one value to pass over, you separated them with commas. Let's look at an example.

Javascript Functions
The scripts we have at the moment are set to run as soon as the page loads in the browser. But they don't have to. A better way is to put the code inside something called a function. A function is just a separate code segment that you call into action. In fact, they don't do anything unless you do call them into action. Let's see how they work.
A function without any code looks like this:
A function without any code looks like this:
function myFunction() {
Javascript Arrays
A normalJavascript variable like var age = 18 holds a single piece of data. An array is a way to hold more than one piece of data under the same name. There's a few ways to set up an array but the simplest looks like this:
var my_array = [10, "ten", true];
Javascript while loops
While loops (lowercase "w") are another programming tool you can add to your armoury. Here's the structure of a while loop:
while ( condition_to_test ) {
}
Instead of the word "for" this time type while.
Javascript Programming Loops
One programming tool common to all languages is the loop. Normally, a programme is executed from top to bottom, with each line of code getting processed in turn. If you want to go back up and not down, you can use a loop. Loops are a great way to execute lines of code again and again. There are many reasons why you want to do this. But take the following sum as an example:
var total;
total = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10;
total = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10;
Javascript Switch Statements
Sometimes, you'll have more than one value to check for your variables. You can do the checking with a lot of if ... else statements. Or you can use something called a switch statement. Here's an example:
var age = 17;
switch (age) {
case 17:
document.write("under 18");
break;
case 24:
Javascript Logical Operators
Operators you will want to use with your IF Statements are the logical ones. The logical operators give you more options for your IF statements. There are only three to get the hang of:
&& Two ampersands mean AND
|| Two pipe characters mean OR
! One exclamation mark/point means NOT
As an example, create the following code for a web page
Javascript Comparison Operators
They have used the double equal sign (== ). But there are other operators you can use besides the double equal sign. These are known as comparison operators. Here are some more of them. Remember, all these evaluate to either true or false.
!= Is not equal to
> Greater Than
< Less Than
>= Greater Than or equal to
<= Less Than or equal to
Javascript IF ... ELSE
This lesson continue from the previous one
You can also add an else part to your IF statements. This is for when you want to say what should happen if your IF Statement evaluates to false. As an example, change your code to this:
var first_name = "Benny";
if ( first_name == "Kenny" ) {
document.write("First Name is " + first_name);
}
else {
Javascript and IF Statements
Javascript is what's known as a sequential programming language. This means that each and every line of code is executed from top to bottom. Quite often, however, you don't want every line to execute - you'll want more control over the way your programmes work.
One way to control the flow of your code is with conditional logic. Conditional logic is all about what happens IF a condition is met or not met. For example, you may have an email text box on a form that you want your users to fill in. You don't want this text box to be empty when the user click the SUBMIT button. So you need a way to say "IF the email address is missing THEN don't send the form". You do this with IF Statement.
Mathematical Operators
You can use the mathematical operators with variables. This allows you to add up, multiply, subtract, and divide. The mathematical operators are these:
- The plus symbol ( + ) is used for addition
- The minus symbol ( - ) is used for subtraction
- The asterisk symbol ( * ) is used for multiplication
- The forward slash symbol ( / ) is used for division
- The percentage symbol ( % ) is used for modulus calculations
Javascript Variables
In programming terminology, a variable is a storage area. You store things in variables so that you can retrieve them later. Think of variables as small boxes. You can write a number on a piece of paper then place that piece of paper in a box. Write a label on the box, such as "Mom's phone number", and you'll have a quick way to identify what kind of information you have stored in the box. One day, you might even decide to phone Mom. If so, you can





