Getting uncaught exception in javascript - javascript

I am getting "Uncaught SyntaxError: Unexpected token '}" error in browser console and the below message popup is not showing though all conditions are satisfied.
Can someone point out the error here
*
IF (dmd.is_txt(videntifier) = 'TRUE' AND gb = 'S' AND hn AND (vp IS NULL OR vp = '')) THEN
htp.p('<script language = "javascript"> if(ghb != null && eff != "" && ghg != "NULL")
{
var fhh = confirm("223?");
if (answer_preorder)
{
theform.ghb.value = ghy;
</script>');
bsave := true;
htp.p('<script language = "javascript">
}
else
{
theform.ghh.value = ghh;
</script>');
gh := false;
htp.p('<script language = "javascript">
}
}
</script> </script>');
END IF;
*

You cannot split a JavaScript block over multiple script tags; you need to put the entire code into a single script.
You appear to want something like:
IF (
dmd.is_gpid_packaged_txt(videntifier) = 'TRUE'
AND dbcode = 'S'
AND bsave
AND vpreorderdt IS NULL
)
THEN
htp.script(
'if (voldpreorderdate != null && voldpreorderdate != "" && voldpreorderdate != "NULL")'
|| '{'
|| 'var answer_preorder = confirm('
|| '"The preorder is currently live, this will not remove it from Apple.'
|| ' Do you want to proceed?"
|| ');'
|| 'theform.vpreorderdt.value = answer_preorder?vstreetdt:voldpreorderdate;'
|| '}',
'javascript'
);
bsave := FALSE;
END IF;

On the first line, it says dmd.is_gpid_packaged_txt(videntifier) = 'TRUE' but if you want to test if something equals something else, you should use two equal signs, or ==, or three equal signs, or ===, if you want to make sure the types are equal too.

Related

Javascript alert coming up when not expected

I am trying to ensure that all fields of a form are not empty. When there are empty fields this alert comes up as expected, however when all fields are full the alert still comes up. Am I missing something?
var sn = document.myForm.address.length;
var sna = document.myForm.street.length;
var su = document.myForm.city.length;
var st = document.myForm.state.length;
var usn = document.myForm.username.length;
if (sn || sna || su || st || usn == null) {
alert("All fields are required. Please ensure you leave no fields blank.");
return false;
} else {
}
Since you initialized all your variables, your if statement is evaluating true like this:
if (true || true || true || true || true || false)
Only one true makes the entire if condition above evaluate to true because all the || operators are OR operators.
Consider further, if you simply declare but do not initialize a variable for example var sn; //declared as opposed to var sn = document.myForm.address.length; //initialized then its condition evaluates to false because if(sn) is declared but not initialized = false`.
Moreover, to check the value inside each variable rather than whether or not they are initialized, you must do this:
if (sn == null || sna == null || su == null || st == null || usn == null)
or possibly since you're assigned a length you want this
if (sn > 0 || sna > 0 || su > 0 etc...

If/Else Statement Returning false positives

In my code, I have this if/else statement to deal with a situation in which the numbers and letters both return cont = false. I have tried running just this code, with the same result. Obviously, it should execute the code in the else statement. Does anyone have any ideas?
var input = prompt()
if (input == null || " ") {
//Cont determines whether or not to continue
console.log("cont = false");
var cont = false;
}else{
console.log("cont = true");
var cont = true;
}
Because that code is not how you check one input against two values.
if ( input == null || " " )
should be
if (input==null || input == " ")
input == null || " "
evaluates to (result of your comparison) || " ". Now since " " (a non-empty string) is a truthy value this always evaluates to true.
For order of evaluation -
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
Adding to the other replies which are all correct about the || operator and precedence.
Using == is not a good thing to do in most cases, not least because 0 == null etc - if you're checking if it's actually null or an empty string then something like this is safer:
if (input === null || input.trim() === "") {
...
This checks type as well as content, so won't be able to give false positives. though if input isn't a string it will complain.
Thank's so much! As a summary of all of the answers recieved so far:
The OR operator (||) is comparing input == null to " ", the latter of which always evaluates to true
=== is better than ==
Thanks again for all the help and support!

Multiple OR Strings

I cannot find out how to pass multiple strings in an If statement.
Here is my Code :
var date = new Date();
if (document.getElementById("postcode-entry").value == ("G74" || "G75")) {
if (date.getHours() < 8 ) {
window.alert("Sorry we are not open at the moment, please try again later.");
} else {
window.open("http://http://stackoverflow.com");
}
} else {
window.alert("Sorry we do not Delivery to your area, please collect from store");
}
How can i do this ?
The phrase ("G74" || "G75") forces a boolean evaluation on each string, and both will return true always.
So you would need to do something like this:
var myvar = document.getElementById("postcode-entry").value;
if(myvar === "G74" || myvar === "G75")
i am not sure if you want to follow this approach but try using the following-
var strArr = [ 'G74', 'G75' ];
if( strArr.indexOf( document.getElementById("postcode-entry").value ) !== -1 ) {
// Your normal code goes here
}
Using this, you can have n number of string tested in a single statement inside if.
This should do
var post_code = document.getElementById("postcode-entry").value;
if (post_code == "G74" || post_code == "G75")
I have never seen this before. Perhaps you can use the switch statement
But in your case I would recomment the following:
var poscode = document.getElementById("postcode-entry").value
if (postcode === "G74" || postcode === "G75") ......

How to check if the elements of an array are identical in javascript (more than 2 elements)

I am trying to make sure that a phone# is not all identical characters, example 1111111111
The code I am using works but there has to be a cleaner way. I've tried loops but that only compares two consecutive characters at a time. This is what I am using now:
if (MainPhone.value != "")
{
if ((MainPhone.value == 1111111111) || (MainPhone.value == 2222222222) || (MainPhone.value == 3333333333) || (MainPhone.value == 4444444444) || (MainPhone.value == 5555555555) || (MainPhone.value == 6666666666) || (MainPhone.value == 7777777777) || (MainPhone.value == 8888888888) || (MainPhone.value == 9999999999) || (MainPhone.value == 0000000000))
{
window.alert("Phone Number is Invalid");
MainPhone.focus();
return false;
}
}
I found this recommendation for someone else' question but could not get it to work.
var dup = MainPhone.value.split('');
if all(dup == dup(1))
I would try something like this:
var phone = '11111211';
var digits = phone.split('').sort();
var test = digits[0] == digits[digits.length - 1];
Simply sort the array and compare first and last element..
You can use a regular expression like this to check if all characters are the same:
^(.)\1*$
Example:
var phone = '11111111';
if (/^(.)\1*$/.test(phone)) {
alert('All the same.');
}
Demo: http://jsfiddle.net/Guffa/3V5en/
Explanation of the regular expression:
^ = matches start of the string
(.) = captures one character
\1 = matches the first capture
* = zero or more times
$ = matches end of the string
So, it captures the first character, and matches the rest of the characters if they are the same.

C# String.IsNullOrEmpty Javascript equivalent

I want to try to do string call equivalent to the C# String.IsNullOrEmpty(string) in javascript. I looked online assuming that there was a simple call to make, but I could not find one.
For now I am using a if(string === "" || string === null) statement to cover it, but I would rather use a predefined method (I keep getting some instances that slip by for some reason)
What is the closest javascript (or jquery if then have one) call that would be equal?
You're overthinking. Null and empty string are both falsey values in JavaScript.
if(!theString) {
alert("the string is null or empty");
}
Falsey:
false
null
undefined
The empty string ''
The number 0
The number NaN
If, for whatever reason, you wanted to test only null and empty, you could do:
function isNullOrEmpty( s )
{
return ( s == null || s === "" );
}
Note: This will also catch undefined as #Raynos mentioned in the comments.
if (!string) {
// is emtpy
}
What is the best way to test for an empty string with jquery-out-of-the-box?
If you know that string is not numeric, this will work:
if (!string) {
.
.
.
You can create one Utility method which can be reused in many places such as:
function isNullOrEmpty(str){
var returnValue = false;
if ( !str
|| str == null
|| str === 'null'
|| str === ''
|| str === '{}'
|| str === 'undefined'
|| str.length === 0 ) {
returnValue = true;
}
return returnValue;
}
you can just do
if(!string)
{
//...
}
This will check string for undefined, null, and empty string.
To be clear, if(!theString){//...} where theString is an undeclared variable will throw an undefined error, not find it true. On the other hand if you have: if(!window.theString){//...} or var theString; if(!theString){//...} it will work as expected. In the case where a variable may not be declared (as opposed to being a property or simply not set), you need to use: if(typeof theString === 'undefined'){//...}
My preference is to create a prototype function that wraps it up for you.
Since the answer that is marked as correct contains a small error, here is my best try at coming up with a solution. I have two options, one that takes a string, the other takes a string or a number, since I assume many people are mixing strings and numbers in javascript.
Steps:
-If the object is null it is a null or empty string.
-If the type is not string (or number) it's string value is null or empty. NOTE: we might throw an exception here as well, depending on preferences.
-If the trimmed string value has a length that is small than 1 it is null or empty.
var stringIsNullOrEmpty = function(theString)
{
return theString == null || typeof theString != "string" || theString.trim().length < 1;
}
var stringableIsNullOrEmpty = function(theString)
{
if(theString == null) return true;
var type = typeof theString;
if(type != "string" && type != "number") return true;
return theString.toString().trim().length < 1;
}
you can say it by logic
Let say you have a variable name a strVal, to check if is null or empty
if (typeof (strVal) == 'string' && strVal.length > 0)
{
// is has a value and it is not null :)
}
else
{
//it is null or empty :(
}

Categories

Resources