Cocoa Dev
Cocoa Dev

Reputation: 9541

JavaScript text from textfield not getting value

<input id="checkOldPassword" type="button" title="Check New Password" value="Check New Password" onclick="checkPassword()" />
<input id="newPassword" type="text" maxlength="8" min="8" />

<script language="javascript">
function checkPassword()
{
  var validString = /^[a-z](?=[a-z]*[0-9])[a-z0-9]{0,6}[a-z]$/;
  alert("this worked");
  var password = document.getElementById(newPassword).value;
  alert(password);
  var test = re.test(password);
}
</script>

The popup window saying "this worked" appears correctly so I know the code is executing but alert(password) doesn't pop up the typed password. What am i doing wrong?

Upvotes: 0

Views: 158

Answers (2)

Engineer
Engineer

Reputation: 48793

Use quotes:

document.getElementById('newPassword').value

Without quotes you have:

document.getElementById(undefined)

as you don't have any newPassword variable defined.

Upvotes: 1

freefaller
freefaller

Reputation: 19953

Change...

var password = document.getElementById(newPassword).value;

To (note the quotes on the element id)...

var password = document.getElementById("newPassword").value;

Upvotes: 2

Related Questions