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.


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

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

if(document.myForm.pass.value == myPassWord)

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.



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.


Syntax: object.form

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



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.
If more than one object on any form share the same NAME attribute, an array of those objects is automatically created.


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".


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.



Syntax: object.blur()

This method is used to remove focus from the object.


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.


Syntax: object.handleEvent(event)

This method calls the handler for a specified event.


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: