Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
492
JavaScript Form
Introduction
Forms are basically used to allow the user to enter some
information, which will then used for further processing on the server. However
before the processing takes places, many developers use JavaScript to validate
the form – i.e. does the user entered all required information? JavaScript
provides many functions, which makes it easy to work with forms. The following
example shows this.
Example
In the following example I have created a form and a
function “GetFormValues()”. This function retrieves all values from the form
and validates them. Only the name fields are validated, the radio buttons can
be selected or not. If the user doesn’t enter the first name or the last name
in the fields, then it will show an error message next to the textbox. Once the
user has entered the information the form is validated and the user information
is shown in the browser.
<html>
<head>
<script type="text/javascript">
function
GetFormValues()
{
//
Get the form
var
myForm = document.myform
//
Get the value from the element firstName
var
firstName = myForm.firstName.value
var
lastName = myForm.lastName.value
var
bMale = myForm.gender[0].value
var
bFemale = myForm.gender[1].value
firstName
= firstName
if(firstName
== "")
spfirstName.innerHTML
= "Please enter the first name"
else
spfirstName.innerHTML
= ""
if(lastName
== "")
splastName.innerHTML
= "Please enter the last name"
else
splastName.innerHTML
= ""
if(firstName
!= "" && lastName != ""){
document.write("Thanks
<b>" + firstName + " " + lastName + "</b> for
entering the information.<br/>")
if(!bMale
|| !bFemale)
document.write("However
you havent selected your gender!")
else{
var
gender = (bMale == true) ? "Male" : "Female"
document.write("Your
gender is: " + gender)
}
}
}
</script>
</head>
<body>
<form name="myform">
* First
Name: <input type="text" name="firstName"/>