Unable to check multiple if else condition in JQueryIs there an “exists” function for jQuery?Add table row in jQueryHow do I check if an element is hidden in jQuery?Setting “checked” for a checkbox with jQuery?How can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?How can I select an element with multiple classes in jQuery?How to check whether a string contains a substring in JavaScript?jQuery scroll to element“Thinking in AngularJS” if I have a jQuery background?
Why can linguists decide which use of language is correct and which is not?
I multiply the source, you (probably) multiply the output!
Why can't some airports handle heavy aircraft while others do it easily (same runway length)?
Why do the British opposition parties not want a new election?
Are programming languages necessary/useful for operations research practitioner?
How do Scrum teams manage their dependencies on other teams?
How to reference a custom counter that shows section number?
Can multiple public keys lead to the same shared secret in x25519?
After a few interviews, What should I do after told to wait?
Why is the the worst case for this function O(n*n)
Do aarakocra have arms as well as wings?
The pirate treasure of Leatherback Atoll
Methods and Feasibility of Antimatter Mining?
Who is the uncredited actor leading the squad in the Valerian movie?
Strategies for dealing with chess burnout?
Is every sentence we write or utter either true or false?
is it possible to change a material depending on whether it is intersecting with another object?
The meaning of "offing" in "an agreement in the offing"
Capacitors with same voltage, same capacitance, same temp, different diameter?
Quick Shikaku Puzzle: Stars and Stripes
How can faith be maintained in a world of living gods?
Isn't that (two voices leaping to C like this) a breaking of the rules of four-part harmony?
Chandrayaan 2: Why is Vikram Lander's life limited to 14 Days?
Does the 2019 UA artificer need to prepare the Lesser Restoration spell to cast it with their Alchemical Mastery feature?
Unable to check multiple if else condition in JQuery
Is there an “exists” function for jQuery?Add table row in jQueryHow do I check if an element is hidden in jQuery?Setting “checked” for a checkbox with jQuery?How can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?How can I select an element with multiple classes in jQuery?How to check whether a string contains a substring in JavaScript?jQuery scroll to element“Thinking in AngularJS” if I have a jQuery background?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to check some multiple conditions in JQuery but it doesn't return the desired result. whatever I input it always went to else condition.
$(function()
$('#installment').on("keydown keyup", check);
function check()
var inst = Number($("#installment").val());
if (inst === 2
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
javascript jquery html if-statement
add a comment |
I am trying to check some multiple conditions in JQuery but it doesn't return the desired result. whatever I input it always went to else condition.
$(function()
$('#installment').on("keydown keyup", check);
function check()
var inst = Number($("#installment").val());
if (inst === 2
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
javascript jquery html if-statement
What exactly are you expecting and doesn’t happen?
– Radu Diță
Mar 28 at 7:12
If entering 12 doesn’t work, that’s because when entering the digit 1 you clear the input. With this type of approach you’ll only be able to validate 1 char input, or inputs where you add the entire prefixes. Another note, val returns a string and you are strict checking.
– Radu Diță
Mar 28 at 7:15
@RaduDiță "val returns a string" - that's why it hasNumber()
around it
– freedomn-m
Mar 28 at 7:17
@freedomn-m My bad, missed that
– Radu Diță
Mar 28 at 7:23
add a comment |
I am trying to check some multiple conditions in JQuery but it doesn't return the desired result. whatever I input it always went to else condition.
$(function()
$('#installment').on("keydown keyup", check);
function check()
var inst = Number($("#installment").val());
if (inst === 2
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
javascript jquery html if-statement
I am trying to check some multiple conditions in JQuery but it doesn't return the desired result. whatever I input it always went to else condition.
$(function()
$('#installment').on("keydown keyup", check);
function check()
var inst = Number($("#installment").val());
if (inst === 2
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
$(function()
$('#installment').on("keydown keyup", check);
function check()
var inst = Number($("#installment").val());
if (inst === 2
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
$(function()
$('#installment').on("keydown keyup", check);
function check()
var inst = Number($("#installment").val());
if (inst === 2
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
javascript jquery html if-statement
javascript jquery html if-statement
edited Mar 28 at 7:13
Jack Bashford
34k7 gold badges29 silver badges53 bronze badges
34k7 gold badges29 silver badges53 bronze badges
asked Mar 28 at 7:10
Mohammad NadeemMohammad Nadeem
53 bronze badges
53 bronze badges
What exactly are you expecting and doesn’t happen?
– Radu Diță
Mar 28 at 7:12
If entering 12 doesn’t work, that’s because when entering the digit 1 you clear the input. With this type of approach you’ll only be able to validate 1 char input, or inputs where you add the entire prefixes. Another note, val returns a string and you are strict checking.
– Radu Diță
Mar 28 at 7:15
@RaduDiță "val returns a string" - that's why it hasNumber()
around it
– freedomn-m
Mar 28 at 7:17
@freedomn-m My bad, missed that
– Radu Diță
Mar 28 at 7:23
add a comment |
What exactly are you expecting and doesn’t happen?
– Radu Diță
Mar 28 at 7:12
If entering 12 doesn’t work, that’s because when entering the digit 1 you clear the input. With this type of approach you’ll only be able to validate 1 char input, or inputs where you add the entire prefixes. Another note, val returns a string and you are strict checking.
– Radu Diță
Mar 28 at 7:15
@RaduDiță "val returns a string" - that's why it hasNumber()
around it
– freedomn-m
Mar 28 at 7:17
@freedomn-m My bad, missed that
– Radu Diță
Mar 28 at 7:23
What exactly are you expecting and doesn’t happen?
– Radu Diță
Mar 28 at 7:12
What exactly are you expecting and doesn’t happen?
– Radu Diță
Mar 28 at 7:12
If entering 12 doesn’t work, that’s because when entering the digit 1 you clear the input. With this type of approach you’ll only be able to validate 1 char input, or inputs where you add the entire prefixes. Another note, val returns a string and you are strict checking.
– Radu Diță
Mar 28 at 7:15
If entering 12 doesn’t work, that’s because when entering the digit 1 you clear the input. With this type of approach you’ll only be able to validate 1 char input, or inputs where you add the entire prefixes. Another note, val returns a string and you are strict checking.
– Radu Diță
Mar 28 at 7:15
@RaduDiță "val returns a string" - that's why it has
Number()
around it– freedomn-m
Mar 28 at 7:17
@RaduDiță "val returns a string" - that's why it has
Number()
around it– freedomn-m
Mar 28 at 7:17
@freedomn-m My bad, missed that
– Radu Diță
Mar 28 at 7:23
@freedomn-m My bad, missed that
– Radu Diță
Mar 28 at 7:23
add a comment |
4 Answers
4
active
oldest
votes
The problem is in the keydown
triggered before the value
update. So if you enter '2'. The value will return ''
And Number('')
is 0
so it always go to else
Just remove the keydown event only use keyup
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
As @Radu Diță mentioned in the comment that 12
can never be valid if 1
is not so to fix that you can use String.prototype.startsWith()
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
Or useinput
event to also pick up other input methods (just notkeydown
as detailed here)
– freedomn-m
Mar 28 at 7:17
3
Key up will not solve the entire problem. 12 will never be validated as 1 is not valid.
– Radu Diță
Mar 28 at 7:17
@Radu Diță You are correct any suggested solution?
– Maheer Ali
Mar 28 at 7:24
@Radu Diță I have attempted to fix that problem. Please see the edit. Thanks for pointing out
– Maheer Ali
Mar 28 at 7:30
Not much else you can do without a major change to how this works, but OP needs to be aware that there needs to be a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:34
add a comment |
Try this:
$(function()
$('#installment').on("keydown keyup", check);
function check(event) inst == 4
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
In this code if
starts working but only for 2
, 4
and 6
. It will not work for 12
as the first input is 1
which you doesn't validate instead of validating the input in keydown and keyup try to use blur
once user finish writing something then validation works as you you like to.
$(function()
$('#installment').on("blur", check);
function check(event)
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
add a comment |
The issue is that your keydown
event fires before the value of the input is modified. Use keyup
(or better yet, input
) instead:
$(function()
$('#installment').on("input", check);
function check()
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
But required if12
should be allowed as a valid answer @freedomn-m.
– Jack Bashford
Mar 28 at 7:28
OK then @freedomn-m I'll remove that.
– Jack Bashford
Mar 28 at 7:32
TBH, in this case, I'm not sure what else can be done with OPs limit code (so I've removed my previous comments) - it would need to have a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:35
add a comment |
$(function()
var val = $("#installment").val();
$('#installment').on("keydown keyup",
function () inst === 12)
alert("OK");
return false;
else
$("#installment").val('');
alert("you can not set wrong value");
//return false;
);
);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391952%2funable-to-check-multiple-if-else-condition-in-jquery%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is in the keydown
triggered before the value
update. So if you enter '2'. The value will return ''
And Number('')
is 0
so it always go to else
Just remove the keydown event only use keyup
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
As @Radu Diță mentioned in the comment that 12
can never be valid if 1
is not so to fix that you can use String.prototype.startsWith()
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
Or useinput
event to also pick up other input methods (just notkeydown
as detailed here)
– freedomn-m
Mar 28 at 7:17
3
Key up will not solve the entire problem. 12 will never be validated as 1 is not valid.
– Radu Diță
Mar 28 at 7:17
@Radu Diță You are correct any suggested solution?
– Maheer Ali
Mar 28 at 7:24
@Radu Diță I have attempted to fix that problem. Please see the edit. Thanks for pointing out
– Maheer Ali
Mar 28 at 7:30
Not much else you can do without a major change to how this works, but OP needs to be aware that there needs to be a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:34
add a comment |
The problem is in the keydown
triggered before the value
update. So if you enter '2'. The value will return ''
And Number('')
is 0
so it always go to else
Just remove the keydown event only use keyup
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
As @Radu Diță mentioned in the comment that 12
can never be valid if 1
is not so to fix that you can use String.prototype.startsWith()
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
Or useinput
event to also pick up other input methods (just notkeydown
as detailed here)
– freedomn-m
Mar 28 at 7:17
3
Key up will not solve the entire problem. 12 will never be validated as 1 is not valid.
– Radu Diță
Mar 28 at 7:17
@Radu Diță You are correct any suggested solution?
– Maheer Ali
Mar 28 at 7:24
@Radu Diță I have attempted to fix that problem. Please see the edit. Thanks for pointing out
– Maheer Ali
Mar 28 at 7:30
Not much else you can do without a major change to how this works, but OP needs to be aware that there needs to be a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:34
add a comment |
The problem is in the keydown
triggered before the value
update. So if you enter '2'. The value will return ''
And Number('')
is 0
so it always go to else
Just remove the keydown event only use keyup
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
As @Radu Diță mentioned in the comment that 12
can never be valid if 1
is not so to fix that you can use String.prototype.startsWith()
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
The problem is in the keydown
triggered before the value
update. So if you enter '2'. The value will return ''
And Number('')
is 0
so it always go to else
Just remove the keydown event only use keyup
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
As @Radu Diță mentioned in the comment that 12
can never be valid if 1
is not so to fix that you can use String.prototype.startsWith()
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
$(function()
$('#installment').on("keyup", check);
function check()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
edited Mar 28 at 7:35
answered Mar 28 at 7:15
Maheer AliMaheer Ali
23.8k4 gold badges21 silver badges38 bronze badges
23.8k4 gold badges21 silver badges38 bronze badges
Or useinput
event to also pick up other input methods (just notkeydown
as detailed here)
– freedomn-m
Mar 28 at 7:17
3
Key up will not solve the entire problem. 12 will never be validated as 1 is not valid.
– Radu Diță
Mar 28 at 7:17
@Radu Diță You are correct any suggested solution?
– Maheer Ali
Mar 28 at 7:24
@Radu Diță I have attempted to fix that problem. Please see the edit. Thanks for pointing out
– Maheer Ali
Mar 28 at 7:30
Not much else you can do without a major change to how this works, but OP needs to be aware that there needs to be a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:34
add a comment |
Or useinput
event to also pick up other input methods (just notkeydown
as detailed here)
– freedomn-m
Mar 28 at 7:17
3
Key up will not solve the entire problem. 12 will never be validated as 1 is not valid.
– Radu Diță
Mar 28 at 7:17
@Radu Diță You are correct any suggested solution?
– Maheer Ali
Mar 28 at 7:24
@Radu Diță I have attempted to fix that problem. Please see the edit. Thanks for pointing out
– Maheer Ali
Mar 28 at 7:30
Not much else you can do without a major change to how this works, but OP needs to be aware that there needs to be a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:34
Or use
input
event to also pick up other input methods (just not keydown
as detailed here)– freedomn-m
Mar 28 at 7:17
Or use
input
event to also pick up other input methods (just not keydown
as detailed here)– freedomn-m
Mar 28 at 7:17
3
3
Key up will not solve the entire problem. 12 will never be validated as 1 is not valid.
– Radu Diță
Mar 28 at 7:17
Key up will not solve the entire problem. 12 will never be validated as 1 is not valid.
– Radu Diță
Mar 28 at 7:17
@Radu Diță You are correct any suggested solution?
– Maheer Ali
Mar 28 at 7:24
@Radu Diță You are correct any suggested solution?
– Maheer Ali
Mar 28 at 7:24
@Radu Diță I have attempted to fix that problem. Please see the edit. Thanks for pointing out
– Maheer Ali
Mar 28 at 7:30
@Radu Diță I have attempted to fix that problem. Please see the edit. Thanks for pointing out
– Maheer Ali
Mar 28 at 7:30
Not much else you can do without a major change to how this works, but OP needs to be aware that there needs to be a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:34
Not much else you can do without a major change to how this works, but OP needs to be aware that there needs to be a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:34
add a comment |
Try this:
$(function()
$('#installment').on("keydown keyup", check);
function check(event) inst == 4
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
In this code if
starts working but only for 2
, 4
and 6
. It will not work for 12
as the first input is 1
which you doesn't validate instead of validating the input in keydown and keyup try to use blur
once user finish writing something then validation works as you you like to.
$(function()
$('#installment').on("blur", check);
function check(event)
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
add a comment |
Try this:
$(function()
$('#installment').on("keydown keyup", check);
function check(event) inst == 4
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
In this code if
starts working but only for 2
, 4
and 6
. It will not work for 12
as the first input is 1
which you doesn't validate instead of validating the input in keydown and keyup try to use blur
once user finish writing something then validation works as you you like to.
$(function()
$('#installment').on("blur", check);
function check(event)
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
add a comment |
Try this:
$(function()
$('#installment').on("keydown keyup", check);
function check(event) inst == 4
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
In this code if
starts working but only for 2
, 4
and 6
. It will not work for 12
as the first input is 1
which you doesn't validate instead of validating the input in keydown and keyup try to use blur
once user finish writing something then validation works as you you like to.
$(function()
$('#installment').on("blur", check);
function check(event)
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
Try this:
$(function()
$('#installment').on("keydown keyup", check);
function check(event) inst == 4
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
In this code if
starts working but only for 2
, 4
and 6
. It will not work for 12
as the first input is 1
which you doesn't validate instead of validating the input in keydown and keyup try to use blur
once user finish writing something then validation works as you you like to.
$(function()
$('#installment').on("blur", check);
function check(event)
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
$(function()
$('#installment').on("keydown keyup", check);
function check(event) inst == 4
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
$(function()
$('#installment').on("keydown keyup", check);
function check(event) inst == 4
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
$(function()
$('#installment').on("blur", check);
function check(event)
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
$(function()
$('#installment').on("blur", check);
function check(event)
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
edited Mar 28 at 7:29
answered Mar 28 at 7:19
Sami Ahmed SiddiquiSami Ahmed Siddiqui
1,8651 gold badge9 silver badges20 bronze badges
1,8651 gold badge9 silver badges20 bronze badges
add a comment |
add a comment |
The issue is that your keydown
event fires before the value of the input is modified. Use keyup
(or better yet, input
) instead:
$(function()
$('#installment').on("input", check);
function check()
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
But required if12
should be allowed as a valid answer @freedomn-m.
– Jack Bashford
Mar 28 at 7:28
OK then @freedomn-m I'll remove that.
– Jack Bashford
Mar 28 at 7:32
TBH, in this case, I'm not sure what else can be done with OPs limit code (so I've removed my previous comments) - it would need to have a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:35
add a comment |
The issue is that your keydown
event fires before the value of the input is modified. Use keyup
(or better yet, input
) instead:
$(function()
$('#installment').on("input", check);
function check()
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
But required if12
should be allowed as a valid answer @freedomn-m.
– Jack Bashford
Mar 28 at 7:28
OK then @freedomn-m I'll remove that.
– Jack Bashford
Mar 28 at 7:32
TBH, in this case, I'm not sure what else can be done with OPs limit code (so I've removed my previous comments) - it would need to have a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:35
add a comment |
The issue is that your keydown
event fires before the value of the input is modified. Use keyup
(or better yet, input
) instead:
$(function()
$('#installment').on("input", check);
function check()
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
The issue is that your keydown
event fires before the value of the input is modified. Use keyup
(or better yet, input
) instead:
$(function()
$('#installment').on("input", check);
function check()
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
$(function()
$('#installment').on("input", check);
function check()
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
$(function()
$('#installment').on("input", check);
function check()
);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
<td>Number of Installment</td>
<td><input type="text" name="installment" id="installment"></td>
</tr>
edited Mar 28 at 7:32
answered Mar 28 at 7:18
Jack BashfordJack Bashford
34k7 gold badges29 silver badges53 bronze badges
34k7 gold badges29 silver badges53 bronze badges
But required if12
should be allowed as a valid answer @freedomn-m.
– Jack Bashford
Mar 28 at 7:28
OK then @freedomn-m I'll remove that.
– Jack Bashford
Mar 28 at 7:32
TBH, in this case, I'm not sure what else can be done with OPs limit code (so I've removed my previous comments) - it would need to have a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:35
add a comment |
But required if12
should be allowed as a valid answer @freedomn-m.
– Jack Bashford
Mar 28 at 7:28
OK then @freedomn-m I'll remove that.
– Jack Bashford
Mar 28 at 7:32
TBH, in this case, I'm not sure what else can be done with OPs limit code (so I've removed my previous comments) - it would need to have a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:35
But required if
12
should be allowed as a valid answer @freedomn-m.– Jack Bashford
Mar 28 at 7:28
But required if
12
should be allowed as a valid answer @freedomn-m.– Jack Bashford
Mar 28 at 7:28
OK then @freedomn-m I'll remove that.
– Jack Bashford
Mar 28 at 7:32
OK then @freedomn-m I'll remove that.
– Jack Bashford
Mar 28 at 7:32
TBH, in this case, I'm not sure what else can be done with OPs limit code (so I've removed my previous comments) - it would need to have a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:35
TBH, in this case, I'm not sure what else can be done with OPs limit code (so I've removed my previous comments) - it would need to have a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
– freedomn-m
Mar 28 at 7:35
add a comment |
$(function()
var val = $("#installment").val();
$('#installment').on("keydown keyup",
function () inst === 12)
alert("OK");
return false;
else
$("#installment").val('');
alert("you can not set wrong value");
//return false;
);
);
add a comment |
$(function()
var val = $("#installment").val();
$('#installment').on("keydown keyup",
function () inst === 12)
alert("OK");
return false;
else
$("#installment").val('');
alert("you can not set wrong value");
//return false;
);
);
add a comment |
$(function()
var val = $("#installment").val();
$('#installment').on("keydown keyup",
function () inst === 12)
alert("OK");
return false;
else
$("#installment").val('');
alert("you can not set wrong value");
//return false;
);
);
$(function()
var val = $("#installment").val();
$('#installment').on("keydown keyup",
function () inst === 12)
alert("OK");
return false;
else
$("#installment").val('');
alert("you can not set wrong value");
//return false;
);
);
answered Mar 28 at 7:36
SAVeSAVe
7802 silver badges19 bronze badges
7802 silver badges19 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391952%2funable-to-check-multiple-if-else-condition-in-jquery%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What exactly are you expecting and doesn’t happen?
– Radu Diță
Mar 28 at 7:12
If entering 12 doesn’t work, that’s because when entering the digit 1 you clear the input. With this type of approach you’ll only be able to validate 1 char input, or inputs where you add the entire prefixes. Another note, val returns a string and you are strict checking.
– Radu Diță
Mar 28 at 7:15
@RaduDiță "val returns a string" - that's why it has
Number()
around it– freedomn-m
Mar 28 at 7:17
@freedomn-m My bad, missed that
– Radu Diță
Mar 28 at 7:23