I wrote a short function to do some error checks for a form and am stuck at a portion of code where the final 'elseif' clause in the code below keeps getting executed, even when there is text in the textbox...
could you please advise...thank you..
function errorCheck(){
if(!isInteger(document.getElementById("appleQty").value)){
alert('Please key in an integer in the Apple Quantity text box.');
document.getElementById("appleQty").value="";
document.getElementById("appleQty").focus();
return false;
}
else if(!isInteger(document.getElementById("orangeQty").value)){
alert('Please key in an integer in the Orange Quantity text box.');
document.getElementById("orangeQty").value="";
document.getElementById("orangeQty").focus();
return false;
}
else if(!isInteger(document.getElementById("bananaQty").value)){
alert('Please key in an integer in the Banana Quantity text box.');
document.getElementById("bananaQty").value="";
document.getElementById("bananaQty").focus();
return false;
}
else if(document.getElementById("user").value = " "){ /!-Problem, keeps getting repeated-->
document.getElementById("user").focus();
alert('Please key in your name.');
return false;
}
return true;
}
One should use == to compare, = to assign. You are assigning, so add an extra = to compare.
You are using = as assignment, not equivalence. Use == or ===.
Two problems:
Use == for comparison, not =.
The empty string is "", not " ". Notice the extra space.
Result:
else if (document.getElementById("user").value == "") {
Related
Good day everyone; I am running into an error I can't explain. The scenario is as follows, I have two input boxes that collect information. If no value is entered, I want the if statement to handle it and cause a break. The Input box also has an "x" to close the box, which returns a value of "Cancel". What I am trying to do is capture a condition where if no value is entered OR cancel is passed through, a break will occur. Right now, the problem is Google completely ignores the Or statement. I know individually, my IF logic works, but when coupled with OR it doesn't recognize the condition.
This is my current code:
var propnumber = Browser.inputBox('Enter RFI/RFQ Number', Browser.Buttons.OK);
if(propnumber != "" || propnumber != 'cancel'){} else{
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
};
var myName = Browser.inputBox("Enter the Component Name",Browser.Buttons.OK_CANCEL);
if(myName != 'cancel')
{
I do something
}
As I mentioned in my description, my propnumber condition ignores the or and always accepts the value of cancel or blank. If I remove the or ( || ) then it works with one condition at a time.
I am sure this is something trivial any help appreciated.
What's wrong
The logic in the following part of your code
if(propnumber != "" || propnumber != 'cancel'){
// I assume some code will go here
} else{
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
};
does not match the logic you've described here:
if no value is entered OR cancel is passed through, a break will occur.
Consider the case where propnumber is 'cancel':
propnumber != "" evaluates to true
propnumber != 'cancel' evaluates to false
Therefore the if(... || ...) condition in your code evaluates to true and the (currently empty) if block runs, rather than the else.
How to fix it
Option 1: A literal translation of the logic
if no value is entered OR cancel is passed through, a break will occur
would be
if(propnumber == "" || propnumber == 'cancel') {
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
} else {
// Some action
}
Option 2: If you wish to swap the if and else clauses, you must negate the entire condition. So this will also work:
if(!(propnumber == "" || propnumber == 'cancel')) {
// Some action
} else {
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
}
Note the added parentheses and single negation.
Option 3: use AND instead of OR in your existing code.
The expression !(A || B) is NOT logically equivalent to !A || !B. Instead, it is equivalent to !A && !B (see DeMorgan's Law). So this will also work:
if(propnumber != "" && propnumber != 'cancel') {
// Some action
} else {
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
}
I have a username text field in the login.html file.
I have javascript code as follows
loginbtn.addEventListener("click", function() {
if(username.value != null) {
socket.emit("login", {username:username.value});
$("#name").hide();
$("#mario-chat").show();
} else {
err.innerHTML = document.write("please enter username");
}
});
In the login page i have not entered any name but still my code goes to the if condition and executing the 1$("#name").hide();$("#mario-chat").show();.
Please tell me why my if condition is executing rather than else part. thanks in advance.
If you console.log(username.value) the result will probably not be null or undefined. That is why != null does not the trick.
username.value might be an empty string so you can add && username.value != '' to the if statement. if(username.value != null && username.value != ''){}
My code works, except it is requiring and extra "Space" at the end to be placed in order for the button to activate? Any ideas? I obviously don't have the space at the end of the user names or passwords in the code. This happens on another frame as well where I have the user type in a web address, I have the conditional set as == "md.website.com" but it is requiring "md.website.com " (extra space at the end) in order for the button to activate.
This code is expecting "AB1234 " and "newuser " instead of "AB1234" "newuser" like I need and I am telling it... I'm sorry, I'm new to AS3 and learning ALL I can, this site rocks for all the help I've already gotten!
username_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
password_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
next_btn.enabled = false;
next_btn.alpha = .5;
function paramChanged3(event:TextEvent):void
{
if (username_txt.text == "AB1234" && password_txt.text == "newuser" )
{
trace("go")
next_btn2.enabled = true;
next_btn2.alpha = 1;
next_btn2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlay_20)
}
else
{
next_btn2.enabled = false;
next_btn2.alpha = .5;
}
}
function fl_ClickToGoToAndPlay_20(event:MouseEvent):void
{
gotoAndPlay(20);
}
The problem is that the TextEvent.TEXT_INPUT fires before the text field is actually updated. Try using Event.CHANGE instead (or using two TextEvent.TEXT_INPUT callbacks and appending the input character with event.text within each).
I don't know why AS3 is requiring the extra space, but I removed the exact conditional, and just did a minimum character count. Of course the trainee can then type in anything as long as it matches the minimum requirement, but again, the actual usernames and passwords don't matter, its all simulation anyways, here is the code with the character count....
username_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
password_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
next_btn.enabled = false;
next_btn.alpha = .5;
function paramChanged3(event:TextEvent):void
{
if (username_txt.text != "" && username_txt.length >=5 &&
password_txt.text != "" && password_txt.length >=6)
{
trace("go")
next_btn2.enabled = true;
next_btn2.alpha = 1;
next_btn2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlay_20)
}
else
{
next_btn2.enabled = false;
next_btn2.alpha = .5;
}
}
function fl_ClickToGoToAndPlay_20(event:MouseEvent):void
{
gotoAndPlay(20);
}
I have created a program for adding items to an inventory connected to a MySQL database. When the user clicks "Add" a JOptionPane will come up asking them if they wish to add the item to the database. The coding seems correct but when I click on "No" it still adds the item to the database. Here is the code:
int dialogButton = JOptionPane.YES_NO_OPTION;
JOptionPane.showConfirmDialog (null, "Are you sure you want to add this item?",null, dialogButton);
if(dialogButton == JOptionPane.YES_OPTION)
{
Statement stmt=conn.createStatement();
stmt.executeUpdate(query);
if(dialogButton == JOptionPane.NO_OPTION)
{
remove(dialogButton);
}
}
Firstly, the if(dialogButton == JOptionPane.NO_OPTION) is inside the if(dialogButton == JOptionPane.YES_OPTION) block. It should never execute. Try moving it just outside of that block.
if(dialogButton == JOptionPane.YES_OPTION)
{
Statement stmt=conn.createStatement();
stmt.executeUpdate(query);
}
else if(dialogButton == JOptionPane.NO_OPTION)
{
remove(dialogButton);
}
Secondly, JOptionPane.showConfirmDialog returns an integer, representing the button that was pressed. You need to check whether this result is JOptionPane.YES_OPTION or JOptionPane.NO_OPTION. You should have:
int dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure you want to add this item?",null, 0);
I have function :
public static function validate(value:*):Boolean
{
...
if(field_counter < FIELD_LIMIT){
field_counter++;
}else{
return false;
}
return true;
}
I want to make it one line, but it shown Syntax error on "return false":
field_counter < FIELD_LIMIT ? field_counter++ : return false;
If field_counter is not a negative number, you can forget that if and compute everything in a single instruction:
public static function validate(value:*):Boolean
{
return (field_counter < FIELD_LIMIT && ++field_counter)
}
The instruction ++field_counter will not be executed if field_counter is not lower than FIELD_LIMIT.
Edit
Here's a preview:
http://wonderfl.net/c/c7lA
Why make it hard on yourself and any other developer when you can make it simple?
if(field_counter >= FIELD_LIMIT)
return false;
field_counter++;
return true;
You can try working around this by testing something about the field_counter (not the nicest way but should work):
return (field_counter < FIELD_LIMIT ? (field_counter++!=null) : false);
You are attempting to stuff a return statement into a conditional. The trick is, the ?: operator returns a value, so you can do say x= y>z ? 1 : z-y; and return statement does not return a value in terms of an expression. You'd better leave the original if statement intact.