Setting the value of the hidden form field using Javascript

Login to your account

[Hint: The correct login and password is: admin and demo_password]

Try entering incorrect login ID and password a few times ]

The code

function trim(str) {
  return str.replace(/^\s+|\s+$/g, '');
}


function isEmpty(str) {
  str = trim(str);
  return ((str == null) || (str.length == 0))
}

function checkLogin(loginElem, passwordElem, loginAttempts) {
  if (isEmpty(loginAttempts.value)) {
  loginAttempts.value = 1;
  }

  else {
  loginAttempts.value++;
  }  

  var login = loginElem.value;
  var password = passwordElem.value;

  var error_flag = false;
  var msg = 'Login Attempt No: ' + loginAttempts.value + '\n';
  if(isEmpty(login)) {
    msg += 'Please enter your Login ID\n';
    error_flag = true;
  }
  if(isEmpty(password)) {
    msg += 'Please enter your password\n';
    error_flag = true;
  }

  if(!error_flag) {
  if(login == 'admin' && password == 'demo_password') {
    msg += 'Congratulations: You have entered the correct login details';
    loginAttempts.value = 0;
  } else {
    msg += 'Incorrect Login / Password combination';
    error_flag = true;
  }
  }

  alert(msg);

}

Download the code here: javascript-form-field-example-1.zip

Back to article: set a form field using Javascript

  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • StumbleUpon
  • Technorati
  • YahooMyWeb

How to make a form without coding? Click here for a demo.

Follow me on twitter

HTML Forms
HTML Form Tutorial
The Form Tag
The Form Submit Button
Form design software
How to make a web form
Make web forms quickly, without coding

Javascript Form Handling
  Basics
Using getElementById to get the elements in a form
Set value of form element using JavaScript
Get value of form element using JavaScript
Handling multiple forms using JavaScript
JavaScript reset/clear a form
  Advanced
JavaScript Form Validation
Submitting a form using JavaScript
Can JavaScript Email a Form?
Switching the form action field dynamically
JavaScript Button

JavaScript Popup Windows
The window.open method
The window.close method
More ...

 
.   Copyright © 2003-2009 JavaScript-coder.com. All rights reserved.