Passwords in javascript are trivial to circumvent, and in this case, they could find the password by looking at the source.
Having said that, you could trigger your code by using onclick - <div class="mymenu" onclick="password();">
EDIT:
Also, (!pass1) is invalid syntax, did you mean (pass==null)?
php - Password in confirm box Javascript - Stack Overflow
How can I hide the password entered via a JavaScript dialog prompt? - Stack Overflow
HTML/Javascript - username and password - Stack Overflow
Creating a basic JavaScript password prompt for webpage closure
Videos
I have some code that will run when a button is clicked on another page. The code looks for the student email and password, what I am trying to do is if email is right and the password is wrong then alert that the password is wrong. If the password is right, and email is wrong then alert the user about the email being wrong. If both password and email are wrong then alert that both are wrong. My problem right now is even if the password and email are right, I am still getting the alert that the email is wrong, and the alert that both are wrong
onLogin(): any {
var emailInput = (<HTMLInputElement>document.getElementById('emailInput')).value.trim();
var passInput = (<HTMLInputElement>document.getElementById('passInput')).value.trim();
var emailFormat = "^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$";
if(!emailInput.match(emailFormat)){
alert("Please enter a valid email!");
}
if(passInput ==''){
alert("Password field can not be empty!");
}
for (let i = 0; i < this.Student.length; i++) {
if (emailInput == this.Student[i].email) {
console.log('email success');
if (passInput == this.Student[i].password) {
console.log('pasword success');
//get the id
console.log(this.Student[i]._id);
this.studentId = this.Student[i]._id;
localStorage.setItem("studentId", this.Student[i]._id);
this.ngZone.run(() => this.router.navigateByUrl('/course-list'))
} else if (emailInput == this.Student[i].email && passInput !== this.Student[i].password){
alert('Password is incorrect for this email!');
console.log('password fail');
}
} else if (emailInput !== this.Student[i].email && passInput == this.Student[i].password) {
alert("Email is incorrect for this password!");
console.log('email fail');
}
else if (emailInput !== this.Student[i].email && passInput !== this.Student[i].password) {
alert("Both password and email are incorrect!");
}
else{
console.log("successful login");
}
}
}Hi You Need some thing like this, Modify according to your requirements
<script>
function do_check()
{
var return_value=prompt("Password:");
if(return_value==="your_password")
return true;
else
return false;
}
</script>
<form action="log.php" method="post" onsubmit="return do_check();">
Try using input type="password" , required attribute` ; disable submit until password set
document.forms["form"].querySelector("input[type=button]").onclick = function(e) {
e.preventDefault();
this.nextElementSibling.disabled = false;
this.nextElementSibling.nextElementSibling.disabled = false;
}
<form name="form">
<input type="button" value="Are you sure?" />
<input type="password" minlength="3" maxlength="7" required disabled />
<input type="submit" disabled />
</form>
Are you looking for the prompt function?
var response = prompt("What is your name?");
alert("Hello, " + response);
The dialog will look something like this:

This this probably isn't the best way to get password input, because it does not mask the input. Instead, consider using an HTML form with a password input field.
Maybe you are looking for basic HTTP authentication instead?
You can set this by getting your web server to send a few headers; for example with PHP:
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
} else {
echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}
?>
This will cause the client to show a dialog like this:
You cannot mask the input with a JavaScript window.prompt()
Consider using a jQuery UI Modal Form Dialog.
http://jqueryui.com/dialog/#modal-form
You are missing some braces. Corrected code (fiddle: http://jsfiddle.net/RyjhP/1/):
var user1="grant";
var username=prompt('Please Log in. Username:',' ');
if (username==user1){
var pass1="password";
password=prompt('If you are suppose to be here you have a password. Please type it now:',' ');
if (password==pass1){
alert("correct!")
}
else {
window.location="wrongpassword.html";
}
}
else {
window.location="wrongpassword.html";
}
short way to do that.
js
window.onload=function(){
window.location=prompt('Enter Pass')!='password'?'wrong.html':'ok.html'
}
example
http://jsfiddle.net/6KzDJ/
but this is a very bad thing.
you should check the password from a secure location with ajax.
Just add onsubmit event handler for your form:
<form action="insert.php" onsubmit="return myFunction()" method="post">
Remove onclick from button and make it input with type submit
<input type="submit" value="Submit">
And add boolean return statements to your function:
function myFunction() {
var pass1 = document.getElementById("pass1").value;
var pass2 = document.getElementById("pass2").value;
var ok = true;
if (pass1 != pass2) {
//alert("Passwords Do not match");
document.getElementById("pass1").style.borderColor = "#E34234";
document.getElementById("pass2").style.borderColor = "#E34234";
return false;
}
else {
alert("Passwords Match!!!");
}
return ok;
}
if ($("#Password").val() != $("#ConfirmPassword").val()) {
alert("Passwords do not match.");
}
A JQuery approach that will eliminate needless code.
If I were you I'd add an HTML element to stuff an error into. Then you can style it with CSS however you'd like.
<div id="error"></div>
Then your function would look something like this:
function check(){
if(document.getElementById("victory").value == "victory")
return true;
else
document.getElementById("error").innerHTML = "Wrong keyword entry."
return false;
}
You can simply add the alert in the else condition…
function check(){
if(document.getElementById("victory").value == "victory") {
return true;
}
else {
alert("wrong keyword entry");
return false;
}
}