I was writing a very simple function but can't get the if statement to work correctly.
$("#supplyRequestTextbox-clients").live("blur", function() {
var textboxValue = $(this).val().replace(/\d+/g, '').replace(" ", "");
if (textboxValue == "NO ACCOUNT") {
$(".controlBarTextboxNoAccount").fadeIn("fast");
}
});
The value in the input #supplyRequestTextbox-clients represents a client and is organized like this:
id# FirstName LastName email phone
An example: 000001 John Doe johndoe#johndoe.com 123-456-7890
When no account is found, the string appears exactly like this:
000906 NO ACCOUNT
In my function I strip the digits and first space, then check to see if it works in my if statement. I have alerted textboxValue and it is passing correctly, but no matter that textboxValue is, the if statement never fires.
Change the if condition to:
if(textboxValue.indexOf("NO ACCOUNT") !== -1)
indexOf("NO ACCOUNT") finds "NO ACCOUNT" within textboxValue, if it can't find it -1 is returned. So this will be true if "NO ACCOUNT" is anywhere in your string.
Use == for comparisons. You are assigning a truthy value, so the statement always fires.
Change your if condition to
if (textboxValue == "NO ACCOUNT")
If you do
if (textboxValue = "NO ACCOUNT")
You're actually assigning "NO ACCOUNT" to textboxValue and evaluating the result as bool.
Try doing this:
$("#supplyRequestTextbox-clients").live("blur", function() {
var textboxValue = $(this).val().replace(/\d+/g, '').replace(" ", "");
// check what textboxValue evaluates to:
alert(textboxValue);
if (textboxValue == "NO ACCOUNT") {
$(".controlBarTextboxNoAccount").fadeIn("fast");
}
});
Use == for comparisons as Kolink suggest.
You are replacing " " with "", IE. removing all spaces so your if statement will never return true.
Related
I'm attempting to create a javascript variable str. The ID idofnet may not exist, if it doesn't I want to ask for a value for str. If it does exist I want to pick it up from the ID. Here is my code...
function ics214button() {
if (typeof $("idofnet").html() === "undefined") {
var str = prompt("Enter a Log number.");
} else {
var str = $("#idofnet").html().trim();
}
if (str =="") {alert("Sorry no net was selected");}
else {alert ("It worked");}
}
When I test this knowing there is no ID = idofnet, I get the prompt to enter a log number. And the rest of the code executes properly.
But when the idofnet does exist and it contains a value, I still get the prompt asking enter a log number. The value is never set in the else condition testing for undefined.
If idofnet contains a value, why is it still asking me as if it were undefined? The var str will always be a number.
Changing the if condition test to this:
if ( $('#idofnet').length ) {
var str = $("#idofnet").html().trim();
} else {
var str = prompt("Enter a Log number.");
}
Solved the problem.
A noob with a problem here.
Scenario 1:
do { var yourName = prompt("Who are you?"); } while (!yourName); console.log(yourName);
Returns a string if something was typed into the prompt box.
If you didn't type anything and click "OK", it continues to ask for name. And if you click "Cancel", it still continues to ask.
Scenario 2:
do { var yourName = prompt("Who are you?"); } while (yourName != true); console.log(yourName);
Keeps asking for a name even if you type something or click "OK" or "Cancel". It just gets stuck. Infinite loop?
In scenario 1 the program works like this:
Do this (ask for name) while yourName is false (false meaning undefined). If yourName gets a defined value console.log the name.
I know that an empty string ("") is converted in boolean value as false. And i also know that it is converted from false to true because of ! before being negated by ! from true back to false. So if the program is getting the empty string by clicking "OK" or "Cancel", it is getting the original false, converted to true and then the converted true is negated by ! back to false, and that's why it is keeping asking for a name for as long as you don't type it. And when you type in something, the typed value is true, it gets checked by !, we get back true and it is being console.logged.
In scenario 2 the program works almost like in 1st case but it doesn't accept any typed values because when you type in something, it is true in boolean, true != true will give us false so it will ask for name again. But here is what i don't understand about 2nd case: when you click "OK" or "Cancel", they are translated as false, and false != true is true, which should pass and console.log the empty string.
I think i understood something wrong or...i don't even know. Please explain me this guys.
Also this is quiet strange too:
do { var yourName = prompt("Who are you?"); } while (typeof(yourName) != true); console.log(yourName);
And
do { var yourName = prompt("Who are you?"); } while (typeof(yourName) != false); console.log(yourName);
Scenario 1:
Cancel returns null and OK returns empty "", and both satisfies !yourName.
Instead just check if user has clicked cancel or not by adding a condition yourName != null
Demo
do {
var yourName = prompt("Who are you?");
} while (!yourName && yourName != null);
console.log(yourName);
Scenario 2:
OK doesn't return boolean so yourName != true will never fail - hence the infinite loop in this scenario as well.
When you use !yourName, the empty string '' is falsy and a non-empty string is truthy, so the boolean condition is true.
When you use yourName != true, both operands are converted to a number and then compared: loose comparison with ==. This means that comparing the string '1' to true evaluates to true.
console.log("'' and true:", +'', +true);
console.log("'your name' and true:", +'your name', +true);
console.log("'true' and true:", +'true', +true);
console.log("'1' and true:", +'1', +true);
This is one of those places that JavaScript is just out to get you. It is quite commonly accepted to use !yourName, but being explicit with (yourName != null && yourName != '') will not hurt.
I am trying to make a simple if-else statement, but when I run the code, it always returns true, even if I enter something in the prompt that I know should be false. I have ran it through JsFiddle, and it seems that the code snippet is perfectly valid.
var captchaTest = 5;
var captchaInput = prompt('What is five plus five?');
if ('captchaTest + captchaInput = 10') {
alert('You passed, you may continue'); window.location.href = 'pagepass.html';
}
else {
alert('Failed, try again.'); window.location.href = 'main.html';
}
Can someone tell me what I did wrong?
Non-empty strings in JavaScript are truthy. 'captchaTest + captchaInput = 10', when evaluated as Boolean, is true.
You need to remove the quotation marks and change = to ==:
if (captchaTest + captchaInput == 10)
Apart from the answer that other provided I would also make a point that as per your captcha question, your condition should be like this
if (captchaInput == 10){
alert('You passed, you may continue'); window.location.href = 'pagepass.html';
}
else {
alert('Failed, try again.'); window.location.href = 'main.html';
}
I don't see any use of the variable captchaTest
You shouldn't be using a 'captchaTest + captchaInput = 10' as it is a String and always evaluates to true unless it is an empty one.
Also you should use comparison operator == instead of assignment operator =
So remove the quotes
if ((captchaTest + captchaInput) == 10)
I have started javascript today. Trying with the very basic and got stuck with If Else loop.
var input = prompt("type your name"); //variable stores the value user inputs
var outout = tostring(input); // the input value is changed to string datatype and stored in var output
alert(output);//supposed to display the value which it doesn't
if(output == "Tiger")
{alert("It is dangerous");
}
Else
{alert("all is well");
}//I only get a blank page
If I omit the line var output = tostring(input) and try to display the alert box with input value I get the alert box. But after that I only get a blank page. The If Else loop doesn't work at all. I am using notepad++. Also checked in Dreamweaver. There is no compile error. What am I doing wrong?
Sorry for such a basic question and thanks for replying.
Regards,
TD
Your line
tostring(input);
Should be
toString(input);
The toString() method has a capital S
Also, your output variable is called "outout". Don't know if that's a typo...
Not only that, your Else should also have a small e. All JavaScript keywords are case sensitive.
You do not have to convert the result of a prompt to a string, it is a string already. And it actually would be
input.toString()
And Else is lowercase, the correct would be else.
So you can use like this
var input = prompt("Type your name");
if (input == "Tiger")
{
alert("Wow, you are a Tiger!");
}
else
{
alert("Hi " + input);
}
Notice that if you type tiger (lowercase) you will end up on the else. If you want to compare a string case insensitive you can do this:
if (input.toLowerCase() == "tiger")
Then even tIgEr will work.
Your code has the following problems:
var input = prompt("type your name");
var outout = tostring(input);
// Typo: outout should be output
// tostring() is not a function as JavaScript is case-sensitive
// I think you want toString(), however in this context
// it is the same as calling window.toString() which is going to
// return an object of some sort. I think you mean to call
// input.toString() which, if input were not already a string
// (and it is) would return a string representation of input.
alert(output);
// displays window.toString() as expected.
if(output == "Tiger")
{alert("It is dangerous");
}
Else // JavaScript is case-sensitive: you need to use "else" not "Else"
{alert("all is well");
}//I only get a blank page
I suspect what you want is this:
var input = prompt("type your name");
alert(input);
if (input === "Tiger") {
alert("It is dangerous");
} else {
alert("all is well");
}
I've got a form where the user inputs 3 values, which are then calculated. The outputs are displayed again within the form in some "readonly" output boxes. For each input, I want to validate if they are a number, if not, instead of the form showing "NaN" I want to display an error saying, "Please enter a number" (or something like that). Below is the code I am using, which is executed "onkeyup":
function checkforNumber()
{
if (isNaN(sInput || dInput || pInput) == true) {
alert("You entered an invalid character. Please reset the form.");
}
else {
return(false);
}
}
Am I using this function incorrectly? Is there something wrong with the syntax?
Thanks
if (isNaN(sInput) || isNaN(dInput) || isNaN(pInput)) {
alert("You entered an invalid character. Please reset the form.");
}
also make sure that those 3 variables sInput, dInput and pInput are not strings but were obtained by using parseFloat or parseInt methods.
var sInput = parseFloat(document.getElementById('sinput').value);
var dInput = parseFloat(document.getElementById('dinput').value);
var pInput = parseFloat(document.getElementById('pinput').value);
if (isNaN(sInput) || isNaN(dInput) || isNaN(pInput))
This is what I think you intended. You need to pass each value you want to test in to the isNaN function one at a time. Also note that you don't need the == true part, because isNaN returns true or false so the condition will evaluate to the return value.