JavaScript » Objects » Password

A Password object is created by using an HTML <INPUT> tag and assigning "password" to the TYPE attribute. When a user then enters a password, an asterisk (*) is displayed for every character entered, thus hiding the value of the password from the view of others. A Password object must be defined within an HTML <FORM> tag and the JavaScript runtime engine will then create an entry for it in the elements property of the appropriate Form object. The Password object can then be accessed through this elements array by referencing either its element number or name, if a NAME attribute was used in its creation.

Examples

Code:
<input type = "password" name = "pass" value = "" size = 20>
Explanation:

For example, this HTML code creates a password field with the name "Pass" and no initial value

Code:
if(document.myForm.pass.value == myPassWord)
   allowEntry()
Explanation:

Using javaScript you could then, say, test the value of a user's entry in the password field as in the following example which, if the user's entry matches the value previously stored in the MyPassWord variable, executes a function called AllowEntry.

Properties

defaultValue

Syntax: object.defaultValue

This property, tainted by default, is a string reflecting the VALUE attribute of a Password object. Initially this is null (for security reasons) regardless of any value assigned to it. You can override the initial defaultValue property at any time by setting it programmatically, although this won't be reflected in the display of the Password object.

form

Syntax: object.form

This property is a reference to the parent form to which a particular Password object belongs.

name

Syntax: object.name

This property, which is tainted by default, is a string reflecting the NAME attribute of a Password object, and can be set at any time, overriding the previous value.
 
NOTE:
 
If more than one object on any form share the same NAME attribute, an array of those objects is automatically created.

type

Syntax: object.type

This property reflects the type of any particular object on a form, and in the case of the Password object is always "password".

value

Syntax: object.value

This property, tainted by default, reflects the value entered into a password field by the user. It can be set programatically at any time, but if a user tries to alter it interactively, it won't be evaluated properly unless data-tainting is enabled. Whether altered or not, the value is at all times displayed as a string of asterisks.

Methods

blur

Syntax: object.blur()

This method is used to remove focus from the object.

focus

Syntax: object.focus()

This method is used to give focus to an object. It can be used to focus on a Password object prior to a value being entered, either by the user in the password field, or by JavaScript code programatically.

handleEvent

Syntax: object.handleEvent(event)

This method calls the handler for a specified event.

select

Syntax: object.select()
 
NOTE:
 
The Select object also inherits the watch and unwatch methods from the Object object.

This method causes the input area of a Password object to be selected and the cursor to be positioned ready for user input.

See Also: