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
|