I am trying to write a loop to check whether a card number has the right format but I can not get it to work and debugging is just not working.
I need to check for the format: XXX-XXX-XXX1
Here is my javascript:
if(pieces[2] == ''){
$('#hcc_validated').value = 'incorrect';
showCardError();
$('#fname').value = '3';
}else if((pieces[0].length == 3 && typeof pieces[0] == "string") && (pieces[1].length == 3 && typeof pieces[1] == "string") && (thirdp.length == 3 && typeof thirdp == "string") && (fourthp.length == 1 && typeof fourthp == "number")){
$('#hcc_validated').value = 'validated';
showCardError();
$('#fname').value = '2';
}else{
$('#hcc_validated').value = 'not validated';
showCardError();
$('#fname').value = '1';
}
Can anyone see where I am going wrong? (This is just part of the code but I know for sure that this is where I am going wrong as the rest of the code works fine...
Here is my JSFiddle if you want full code:
JSFIddle Example
Thanks guys
This would be better solved using regular expressions.
Specifically;
[0-9]{3}-[0-9]{3}-[0-9]{3}1
Javascript:
var inputstring = "123-123-1231";
var pattern = new RegExp("[0-9]{3}-[0-9]{3}-[0-9]{3}1");
var result = pattern.test(inputstring);
// now result contains true or false
Given that you have:
> var fourthp = pieces[2].slice(-1);
then it is pretty much guaranteed that it's a string, so when will
> (fourthp.length == 1 && typeof fourthp == "number")
ever be true? Even if it's a Number, then fourthp.length will be undefined, so it will always be false either way.
Related
I am validating my time in this way
if (
timeInMins != 0 &&
timeInMins != "0" &&
timeInMins != undefined &&
timeInMins != "undefined" &&
timeInMins != null &&
timeInMins != "" &&
!isNaN(timeInMins)
) {
timeInMinsCumulative += parseFloat(timeInMins);
}
Is there any way to make this ugly if-check to sophisticated code?
There are 6 falsy values in javascript: undefined, null, NaN, 0, "" (empty string), and false of course.
So, you can just write
if (timeInMins && timeInMin !== '0') {
timeInMinsCumulative += parseFloat(timeInMins);
}
This uses the coercion behavior of JavaScript and the logical AND operator to simplify your code. The following is very nearly equivalent to your code, but it will also guard against the arguments false and 0n.
if (timeInMins &&
timeInMins !== '0' &&
timeInMins !== 'undefined') {
// whatever
}
Questions for you: do you really expect to ever get the string 'undefined' passed to you? Why do you want to guard against '0' being sent to parseFloat? Are you sure parseInt is not what you want?
It seems you want to check if timeInMins is precise Number type or not.
function isValidNumber(num) {
return typeof num === "number" && !isNaN(num);
}
console.log(isValidNumber(""));
console.log(isValidNumber(undefined));
console.log(isValidNumber(NaN));
console.log(isValidNumber("undefined"));
console.log(isValidNumber(true));
console.log(isValidNumber(false));
console.log(isValidNumber(0));
console.log(isValidNumber("0"));
console.log(isValidNumber(1.234));
This is apex code, but for the propose seems to work like in Java or JScript.
I've the following line of code (comment included):
// If GPMIR_fld_codigoOrigen__c 09 Fotofactura will not be assigned to agency for send csv
eLead.GPMIR_fld_assignAgency__c = (eLead.GPMIR_fld_codigoOrigen__c!='09') ? ((u.Contact != null && u.Contact.Account != null && rt == 'Agencia') ? u.Contact.Account.Id : null) : null;
Trying to translate that to regular if-else what i think it's happening here is (comment included):
// If GPMIR_fld_codigoOrigen__c != 09 then GPMIR_fld_assignAgency__c == null
if(eLead.GPMIR_fld_codigoOrigen__c!='09'){
if(u.Contact != null && u.Contact.Account != null && rt == 'Agencia'){
eLead.GPMIR_fld_assignAgency__c = u.Contact.Account.Id;
}else{
eLead.GPMIR_fld_assignAgency__c = null;
}
}else{
eLead.GPMIR_fld_assignAgency__c = null;
}
Probably i'm wrong but if anyone could help me to do the proper translation i would really appreciate it
Seems correct, but You can also do this inside a function
Public string MyFunction(){
if(eLead.GPMIR_fld_codigoOrigen__c!='09'){
if(u.Contact != null && u.Contact.Account != null && rt == 'Agencia'){
Return u.Contact.Account.Id;
}
}
Return null;
}```
//Then in main(), eLead.GPMIR_fld_assignAgency__c = MyFunction()
Hopefully I'm right as well.
I've the following code
var oDataEn = aData[0][aProperties[0].split('/')[0]];
if (!(oDataEn != null && oDataEn.results && oDataEn.results.length > 0)) {
...
else
...
This is working OK except when
aData[0] = 'undefined'
my question is if there a better way to write it instead of just adding before
if(aData[0] != null)
{
var oDataEn = aData[0][aProperties[0].split('/')[0]];
}
I dont want to have two if's if possible...
Start with a ternary - I assume aData[0] may be falsy (null, undefined, 0 or ""):
var oDataEn = aData[0]?aData[0][aProperties[0].split('/')[0]]:null;
if (oDataEn && oDataEn.results && oDataEn.results.length > 0) {
if(aData[0] === undefined){}
is the best way so far I know.
As per your comment
if(typeof aData[0] != 'undefined' /*&& other if statements*/){}
So it will not process if it is undefined.
By using ternary operator u can do this. Ternary operator will reduce the line of codes only.
var oDataEn = aData[0] === undefined ? '' : aData[0][aProperties[0].split('/')[0]];
How can I check the length of in String in JavaScript? Here is a small code example:
if(value != null && value != "" && value.length !== 10 && !value.match(/^\d*$/)){
// do something
}
The expression 'value.length !== 10' doesn´t work. A String must contain at least 10 characters. How can I solve this problem?
Instead of match, test can be used with proper regex \d{10,}.
if (value && /^\d{10,}$/.test(value.trim()))
To Get the string length of a value for example:
var value="This is a string";
var string_length=value.length;
/*The string length will result to 16*/
Hope this helps
var regex = new RegExp(/^\d*$/),
value = $.trim("1234567890");
if (value.length >= 10 && regex.exec(value)) {
// correct
} else {
// incorrect
}
I have absolutely no idea why this is not working. Makes no sense to me.
This returns a "syntax error: parse error":
if ($(this).attr("id") === 'search' || opening = true) return false;
For good measure, I also tried the following, which yielded the same result:
if (1 = 1 && 2 = 2) { return false; }
There are three different operators at play:
=: assignment
==: equality
===: strict equality
= actually modifies a variable, so you shouldn't use it inside if statements. That is, you should use ... || opening == true) instead of ... || opening = true).
In JavaScript = is used to assign values, while == and === are used to compare them.
When you put opening = true in your if statement, you aren't checking if opening is true, you are setting opening to true. Try using == instead.
For example,
var x = 5;
if (x == 10) {
alert("x is 10");
} else {
alert("x isn't 10");
}
will display "x isn't 10", while
var x = 5;
if (x = 10) {
alert("x is 10");
} else {
alert("x isn't 10");
}
will display "x is 10".
the first example should read:
if ($(this).attr("id") === 'search' || opening == true) return false;
and the second:
if (1 == 1 && 2 == 2) { return false; }
Note the double equals (==) sign for logic equals is not the same as the single equals (=) sign, that handles variable assignment.
You have an error in your condition
if ($(this).attr("id") === 'search' || opening = true) return false;
should be
if ($(this).attr("id") === 'search' || opening == true) return false;
the problem is with the equals sign
= is different to ==
the first one is the assignment operator. the second one is for comparison
When you test like this:
opening=true;
What you are really doing is setting opening to the value of true. Use == instead.
Finally, order of operations, even if correct, can get confusing. Put parenthesis around each part of the comparison.
if (($(this).attr("id") === 'search') || (opening == true)) return false;
My guess is that === does not exist.
== is for testing equality
so if ($(this).attr("id") === 'search' || opening == true) return false;
should be if ($(this).attr("id") == 'search' || opening == true) return false;