Javascript string comparison not detected - javascript

Sure this is a simple problem but I am new to this. Am trying to develop a simple website for a user to work out a password and then if they get it right, take them to a congrats screen where they win a prize. However my if statement is not working in the code below
<script>
function CheckPassword() {
var pword = document.getElementById("inpPassword").value;
if (pword == ""|| pword == NULL) {
alert("Please enter a password");
} else if (pword == 'newman') {
window.open('congratulations.html', '_self');
} else {
alert("Wrong password");
}
}
</script>
If I add an alert with the pword value then it prints out correctly what was entered. However the code picks up when the password input is blank, however if the input field is not blank then it does not go to the other screen or show the wrong password alert box. I have tried it with an alert box saying correct instead of the link to the new page but still did not work. Also tried using String(pword) in case that was the problem. Am sure this is a simple solution but just can't see it.
Thanks

function CheckPassword() {
var pword = document.getElementById("inpPassword").value;
if (pword == ""|| pword == null){
alert("Please enter a password");
}
else if (pword == 'newman') {
window.open('congratulations.html', '_self');
}
else
{
alert("Wrong password");
}
}
<input type='password' id='inpPassword'>
<input type='button' onclick='CheckPassword()'>
if (pword == "" || pword == null) , its null not NULL . keywords are case sensitive.

You should use null instead of NULL. Seems like it works besides of opening new window, because there is no congratulations.html in my snippet:
function CheckPassword() {
var pword = document.getElementById("inpPassword").value;
if (pword == ""|| pword == null){
alert("Please enter a password");
}
else if (pword == 'newman') {
window.open('congratulations.html', '_self');
}
else
{
alert("Wrong password");
}
}
<input id="inpPassword">
<button onclick="CheckPassword()">Click</button>

You may try here:
The mistake was here, you wrote NULL instead of null, beacuse of javascript understand null as a reserve keyword.
And use === for hard checking(data as well type checking of variaable)
function CheckPassword() {
var pword = document.getElementById("inpPassword").value;
if (pword == ""|| pword == null){
alert("Please enter a password");
}
else if (pword === 'newman') {
alert('congratulations.html', '_self');
}
else
{
alert("Wrong password");
}
}
<input type="" name="a" id="inpPassword">
<input type="button" onClick="CheckPassword()" value="check">

It seems the problem is when you compare to NULL. If you check your browser console you will likely get the following error:
Uncaught ReferenceError: NULL is not defined
That is not the correct syntax for null checks, it will look for a variable called "NULL" which you don't have. You should use null (lowercase) instead:
if (pword == ""|| pword == null){
The full adjusted code is as follows:
function CheckPassword() {
var pword = document.getElementById("inpPassword").value;
if (pword == ""|| pword == null) {
alert("Please enter a password");
} else if (pword == 'newman') {
window.open('congratulations.html', '_self');
} else {
alert("Wrong password");
}
}
Here is a working example.

Null, "" are equivalent to false in Javascript. You just have to:
if (!pword) {
alert("Please enter a password");
} else if (pword === 'newman'){
window.open('congratulations.html', '_self');
} else {
alert("Wrong password");
}

you can write null in small case so its work
function CheckPassword() {
var pword = document.getElementById("inpPassword").value;
if (pword == "" || pword == null ) {
alert("Please enter a password");
} else if (pword == 'newman') {
window.open('congratulations.html', '_self');
} else {
alert("Wrong password");
}
}
<input id="inpPassword" onblur="CheckPassword()">

Related

How to stop loader when alert box opens

I have added a loader on my phonegap app on login button it works. but when Some alert occurs like password mismatch or invalid user, The loader doesn't stops. I have added my loader on html page, and want to stop at my Authentication js page in which alerts box are there in my html page to hide on next page I am using
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('#loading').hide();
});
</script>
how to hide my loader on js page alerts
if(Server == "" || UserName == "" || UserPass == "")
{
if(Server == "")
{
alert("Please enter server address");
}
if(UserName == "")
{
alert("Please enter User Name");
}
if(UserPass == "")
{
alert("Please enter Password");
}
}
else
{
strUrl="https://" + Server + ":1000";
//alert("Test"+strUrl);
sessionStorage.setItem("Server",Server);
window.localStorage.setItem("serUrl", strUrl);
window.localStorage.setItem("serUrl", strUrl);
window.localStorage.setItem("Server", Server);
strSerUrl="http://" + Server + ":8888/";
//alert(strSerUrl);
window.localStorage.setItem("ServerUrl", strSerUrl);
var DivId=sessionStorage.getItem('DivId');
DivId=window.localStorage.getItem("DivId")
// alert(DivId);
Identifier=Identifier+"_"+DivId;
//alert("Create Session1"+Identifier);
window.localStorage.setItem("Identifier", Identifier);
sessionStorage.setItem("Identifier",Identifier);
//alert("above create session")
services.CreateSession(Identifier, SystemID, UserName, UserPass, ConnectionEstablished, AuthenticationFailed);
}
}
function ConnectionEstablished(ResponseData) {
if (ResponseData != "")
{
//alert("resp"+ResponseData)
AuthenticationSuccess(ResponseData);
}
else
alert("Username or Password is incorrect!");
}
you can make the following change to your function to make it work:
function ConnectionEstablished(ResponseData) {
if (ResponseData != "")
{
//alert("resp"+ResponseData)
AuthenticationSuccess(ResponseData);
}
else
{
$('#loading').hide(); //hide the loader!
alert("Username or Password is incorrect!");
}
}
Try if it works!

Other else if statement not executed

Problem: The first conditional statement else if was performed, But the next else if statement was not performed. It should performed if both username and password are empty. What did I missed?
$(document).ready(function(){
$("#submit").click(function(e){
var username = $("#username").val();
var password = $("#password").val();
if(username == ""){
alert("account id required");
}else if(password == ""){
alert("password required!");
}else if((username == "" || username == null) && (password == "" || password == null)){
alert("All fields of information is required!");
}else{
$.ajax({
type: "POST",
url: "checklogin.php",
data: "username="+username+"&password="+password,
success:function(data){
if(data == "success"){
window.location.href="main.php";
}else if(data == "unsuccess"){
alert("Invalid account id/password");
$("#username").val('');
$("#password").val('');
}
}
});
}
return false;
});
});
If, else if, else if, else.. In this hierarchy. Any one and only one block will execute.. If your intention is to execute the second else if .. Then make it if...
So your code should be like below.
if((username == "" || username == null) && (password == "" || password == null)){
alert("All fields of information is required!");
}
else{
if(username == ""){
alert("account id required");
}else if(password == ""){
alert("password required!");
}
}
You have to put the condition containing both username and password before the other ones.
if it gets inside an if statement it wont try with the next else, so if it performs the first else if, it won't check the next else if.
you need to do something like this
if(!username && !password){
alert("All fields of information is required!");
}else if(!username){
alert("account id required");
}else if(!password){
alert("password required!");
}else{
here the rest of your code
}

username and password validation using javascript

I have this simple problem that I don't know what did I do wrong.
so I have this code:
function validateForm()
{
var validation = true;
validation &= validateUsername();
validation &= validatePassword();
return validation? true:false;
}
function validateUsername()
{
var username = $('#username').val();
if( username == "" )
{
alert("Login failed, Please enter your username");
return false;
}
else if( username != "username" )
{
alert("Login failed, Username Incorrect");
return false;
}
else
{
return true;
}
}
function validatePassword()
{
var password = $('#pass').val();
if(password != "password")
{
alert("Login failed, Password is incorrect");
return false;
}
else if(password == "")
{
alert("Login failed, Please enter your password");
return false;
}
else
{
return true;
}
}
If I enter no password it should alert that you should enter your password but instead that it is alerting password is incorrect. Why is it not going through all the if's I created?
You swap the conditions, and check for an empty string before you check for the correct password
function validatePassword() {
var password = $('#pass').val();
if(password == "") {
alert("Login failed, Please enter your password");
return false;
} else if(password != "password") {
alert("Login failed, Password is incorrect");
return false;
} else {
return true;
}
}
right now you're checking if it's not the correct password first, and as an empty string probably isn't the correct password, that matches before the check for an empty string.

How to validate CAPTCHA correctly in JavaScript?

I'm in the middle of coding CAPTCHA in JavaScript, and I'm trying to get the validation for a contact form to work properly. I'm almost there, the form won't be submitted until the CAPTCHA text-field is entered, but the problem is I'm still getting an error message when I entered the CAPTCHA code correctly.
<script>
function ValidateContactForm()
{
var name = document.ContactForm.name;
var phone = document.ContactForm.phone;
var code = document.ContactForm.code;
if (name.value == "")
{
window.alert("Please enter your name.");
name.focus();
return false;
}
if (phone.value == "")
{
window.alert("Please enter a valid phone number..");
phone.focus();
return false;
}
if (code.value == "")
{
window.alert("Please enter the code as displayed on screen.");
code.focus();
return false;
}
else if (code.value != "")
{
window.alert("Your code does not match. Please try again.");
code.focus();
return false;
}
else {
return true;
}
return true;
}
</script>
Any help would be greatly appreciated. Thanks.
Check this lines, the problem is here:
if (code.value == "")
{
window.alert("Please enter the code as displayed on screen.");
code.focus();
return false;
}
else if (code.value != "")
{
window.alert("Your code does not match. Please try again.");
^^^^^^^^^^^^^
code.focus();
^^^^^^^^^^^^^
return false;
^^^^^^^^^^^^^
}
else {
return true;
}
This code will return false every time.

JavaScript Load New Page Question

What I am looking to do is if a user complete a form it will provide access to a new location.
<script language="JavaScript" type="text/javascript">
<!--
function validateForm(theForm) {
var firstname = theForm.firstname.value;
var lastname = theForm.lastname.value;
var email = theForm.email.value;
if (firstname == "") {
alert("Please fill in your First Name.");
theForm.firstname.focus();
return false;
}
if (lastname == "") {
alert("Please fill in your Last Name.");
theForm.lastname.focus();
return false;
}
if (email == "") {
alert("Please fill in your email address.");
theForm.email.focus();
return false;
}
return true;
}
I know this part is wrong but I have no idea how to go about doing it. any help would be nice..
if lastname=""
if firstname=""
if email=""
load('www.google.com');
if (validateForm(theForm)) window.location = 'http://www.google.com';
Is equivalent to using
if (validateForm(theForm)) window.location.href = 'http://www.google.com';
Both will work, so choose which one you prefer.
if lastname=""
if firstname=""
if email=""
load('www.google.com');
becomes
if ((lastname == "") && (firstname == "") && (email == "")) {
window.location = "http://www.google.com";
}

Categories

Resources