BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
JavaScript
JavaScript Error (try/catch/finally)
JavaScript Date Object Functions
JavaScript Date Object
JavaScript Loops
JavaScript Operators
JavaScript Conditional Statements
JavaScript Boolean Object
JavaScript Assertiveness
JavaScript Array Object Functions
JavaScript Arrays
JavaScript Variables
How to use JavaScript?
JavaScript Guidelines
Stock sorter example for IE5
Confused about how to use a data island in IE5??
Software Release tracking application
Employee Phonebook dataisland
Multitab DHTML browser
Trim function in Javascript
Multiple Grid
 
LAMP >>

By :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"/>

            <span ID="spfirstName" style="color:red"></span><br/>

            * Last Name: <input type="text" name="lastName"/>

            <span ID="splastName" style="color:red"></span><br/><br/>

            Male: <input type="radio" name="gender" value="male">

            Female: <input type="radio" name="gender" value="female">

            <input type="button" value="Click Me!" onclick="GetFormValues()">

</form>

</body>

</html>

Output


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

An immediate job opportunity as a B
Software Developers Needed in Charl
Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C

View all Jobs (Add yours)
View all CV (Add yours)



answering service
conference calling
swimming pool builder
spfxmasks
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP