Handling HTML Forms - Part II


© Muhammad Ali Shah
Articles in this Topic    Discussions in this Topic



Abstract

This is the second part of a series of articles on HTML forms processing in JavaScript. We have already reviewed how to create HTML forms and how to pass a form to a JavaScript function for validation. See my last article if you are new to this topic. Today we will see two HTML form objects: text boxes and passwords. So, lets start the work!


Text Boxes

Text Boxes are used to get input in text form, like userid and addressing information, etc. In JavaScript a text box object supports several properties; the most important of which is "value". Suppose you have a text box named email which contains the text ali_bhai@yahoo.com. You can access this text (or value) by using email.value in a JavaScript function. This value property is a string in itself and therefore supports all string properties like length and methods like toUpperCase(). Suppose you want to determine the length of the email address entered by the user; you can do so by writing email.value.length. Following example will clarify the things.

<html>
<head>
<title>HTML Form Processing</title>
<script languague="JavaScript">
<!--hiding from old broswers
  function check_form (myForm)
    {
    len = myForm.email.value.length;
    alert ("Your email address is " + len + " characters long");
    }
 // -->
</script>
</head>

<body>
<form name="survey">
Enter you email <input type="text" name="email">
<input type="button" value="Check Size" onClick="check_form(survey)">
</form>

</body>
</html>


Code Explanation

I will stop here for a moment to take a look on the above JavaScript code. We are gradually coming to complex scripts, right? Here, in this example, we have created an HTML form as apparent from the body section of the HTML page. This form has been given the name of survey. You may choose a name of your liking. Anyhow, the form contains two elements: a text box to accept user's email address and a button to check the address' size. When the button is clicked it simply calls the check_form() function defined in the head part of the page. Note that we have passed the current form named survey to this function.

The JavaScript function in turn receives the form. Inside this function, it will be referred to as myForm. So, we assign len the length of the email address by specifying myForm.email.value.length, i.e. read from right to left to get: the length of the value of the email object in myForm.


Passwords? In HTML?

Yes, HTML allows you to accept passwords. You may have entered your password on certain web sites (like Hotmail, Yahoo! Mail, etc.). The passwords appear as asterisks on these pages. You can create the same effect by using input type="password" in your form. The password type is similar to text type in all other respects, except that it is hidden under asterisks. Following is a code that is similar to the above one. It tells the length of your password and instructs the user to enter a password if the length is zero (Remember the if statements?).

Go To Page: 1 2


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo