I have a javascript code that compares two values:
} else if (!(parseInt($('#form_value').val()) >= 1)){
alert("Error: You didn't pick a number!");
form_value in this case is 001, and I would like to compare it to the one, but it doesn't seem to work. I have tried using parseInt but it didn't work either. Any solutions?
Try:
if (!(Number(parseInt($('#form_value').val(),10)) >= 1)){
EDIT: try this shortened version:
if ( parseInt($('#form_value').val(),10) < 1){
Well, Number("001"); returns 1 and Number("000"); returns 0
based on your comment above
"I'm trying to display an error if the value is less than 1, the
lowest value a user can submit is 000 (which is loaded by default), if
you pick something, it becomes 001."
If the lowest possible value is 0 then just test for 0...
var thing = Number($('#form_Value').val());
if (isNaN(thing) || thing === 0) {
alert('an error message')'
}
May be you should change the condition to if ( +( $('#form_value').val() ) < 1 ) or just if (!+$('#form_value').val()).
Related
My loop is not quitting when i enter 10. Please help me.
let getGuessess = function(){
let guessedNum = null;
while(guessedNum !== 10){
guessedNum = prompt(`enter number $`);
if(guessedNum === "quit"){
break;
}
}
}
getGuessess();
Change from !== to !=. You're doing a strict equality check on 10 vs '10'.
or !== '10'
Maybe these links can help:
https://www.w3schools.com/js/js_comparisons.asp
I see there that:
!== means not equal value or not equal type
https://www.w3schools.com/jsref/met_win_prompt.asp
And here that, for the prompt function:
Return Value: A String.
I think that it doesn't work because you are comparing a string and an int, they are different types, so your comparison returns False even if you enter 10.
Very, VERY new to JavaScript here.. I've been trying to make a character sheet in acrobat auto-populating, and so far I've had good success with most things, until coming to comparing two values.
MaxDex can be an integer greater than 0, and DexTempMod and DexMod can be integers between -10 and 10. The code below works great if dexTempMod and DexMod are positive - if they're greater than MaxDex, it will output MaxDex instead.
But if DexTempMod or DexMod are negative values, it still outputs MaxDex, despite the others being lower. I've tried parsing them into floats after some research, thinking they might be strings, but it still doesn't work.
If it's just a limitation of Acrobat, that's fine, but I'm a little stumped.
if (this.getField("MaxDex").value == ""){if (this.getField("DEXTempMod").value == "")
{event.value = this.getField("DEXMod").value}
else {event.value = this.getField("DEXTempMod").value};
}
else {if (Float.parseFloat(this.getField("MaxDex").value) < Float.parseFloat(this.getField("DEXTempMod").value) || Float.parseFloat(this.getField("DEXMod").value))
{event.value = this.getField("MaxDex").value}
else {if (this.getField("DEXTempMod").value == "")
{event.value = this.getField("DEXMod").value}
else {event.value = this.getField("DEXTempMod").value};}
}
You don't need to use Float.parseFloat. Just be sure the fields are formatted to be numbers in the field properties.
There's no comparison after the || in your outer else statement so any value will evaluate as true in your else statement because even an empty field has a value.
I have an input field thats only supposed to take numbers inbetween 1 and 4. If the number is inbetween 1 and 4, it runs some code. If not, it shoots an alert that tells the user to try again with a number between 1 and 4. Here is my code
var number = document.getElementById("num").value;
if(Number(number) === 1 || Number(number) === 2 || Number(number) === 3 || Number(number) === 4 ){
//success code here///
}
else if(Number(number) !== 1 || Number(number) !== 2 || Number(number) !== 3 || Number(number) !== 4) {
} alert("Please type a whole number between(and including) 1 and 4 into the input field.");
I learned that the '.value;' function returns a string, even if the value is a number. So I put the var 'number' in the Number(); function that converts it to a number.
The problem is, when I type 1 into the input field. It shoots the alert even though it equals 1. None of the other numbers work either. I checked the console, and there are no syntax errors(also according to DreamWeaver). Help would be highly appreciated :)
I think you made a simple mistake of putting your alert outside the else if clause.
However there are a few other things you can do to make that a little more readable and efficient.
// Call Number() here so you only have to do it once
var number = Number(document.getElementById("num").value);
// You can also do something like parseInt(document.getElementById("num").value)
// Now check to see if Number() or parseInt() actually parsed an integer out of their input
// and then check that if it's outside your number range
if (isNaN(number) || number < 1 || number > 4) {
alert("Please type a whole number between(and including) 1 and 4 into the input field.");
} else {
// Do Successful code
}
we can write like this also
var patt1 = /[1-4]/g;
if(patt1.test(number)){
//success code here///
}
else{
alert("Please type a whole number between(and including) 1 and 4 into the input field.");
}
I have an object property that may or may not contain a number and that number may or may not be equal to 0. For the moment, I have this:
var TheVar = parseInt(SomeObject.SomeVar, 10);
if (!TheVar > 0) {
TheVar = "-";
}
I want TheVar to be either a positive number or "-". I'm just wondering if my conditional statement is going to cover every case?
Thanks for your suggestions.
No. You are missing parentheses.
if( !(TheVar > 0))
NaN > 0 returns false, so the if condition will go through.
jsFiddle: http://jsfiddle.net/Mw4j2/6/
Trying to change attributes to a selector if the seconds count is 10 or under.
I'm using
var returnSecondsNumber = $('.countdownSecond > .countSeconds > .position:first-child > .digit').text() + $('.countdownSecond > .countSeconds > .position:nth-child(2) > .digit').text();
To grab the numbers from both spans and returns it as 16/15/14/etc.
Now I need do something if this numeric is under 10.
e.g.
if (returnsSecondsNumber <= 10)
{ $(this).addClass('urgent');
}, else { //do something
}
I've tried taking a look at parseInt with no success. Any help would be much appreciated!
Thanks.
Two things:
First, you need to move the code that constructs "returnSecondsNumber" into the callback function(s) of whichever timers you want to monitor.
Second, just use parseInt():
var numericVal = parseInt(returnsSecondsNumber, 10);
if (!isNaN(numericVal) && numericVal < 10) {
// do whatever
}
Try
if(!isNaN(parseInt(returnsSecondsNumber )) && returnsSecondsNumber < 11)
//Do stuff...
One thing to keep in mind is that if the value of returnsSecondNumber is a starts with a number that will be returned instead of NaN