How to compare two numbers from a form?
Your numbers might be in string forms. You might try using a function such as Number();
in JavaScript and pass it through that, just to make sure, typeof your form progress values are number.
Maybe, try something similar to:
<script type="text/javascript" language="javascript">
function Validator_edit(frm){
if (frm.progress.value=="")
{
alert("progess must not be empty");
frm.progress.focus();
return false;
}
if (Number(frm.progress.value) > Number(frm.weight.value) )
{
alert(" You have exceed the weight.");
frm.progress.focus();
return false;
}
}
</script>
Or you might pass it through a regex, maybe similar to:
function is_number(form_progress_number) {
return /^\d+$/.test(form_progress_number);
}
This link might help you.
Comparing two numbers from a form in JavaScript
The values a
and b
are both strings. If you want to compare them like numbers, use parseInt()
or parseFloat()
on them before you perform the comparison.
So, before the if statement:
a = parseInt(a);
b = parseInt(b);
Compare values in two input fields
Use type="number"
, As per my knowledge there is type as such numeric
Code
if (+$("#end").val() > +$("#start").val()) {
//do something
} else {
alert('Wrong Input');
}
Here I have use +
to convert value to integer
compare two input values in input validation html javascript?
Few things here
close the input elements:
if(maxN<min) {
Should be
if(max<min) {
Finally, you are not comparing integers but strings so..
5<9
555<9
1000<20
Its "alphabetic"
You need to parse them to int.
parseInt(max) and parseInt(min)
...
function MinimumNValidate(){
var min = parseInt(document.getElementById("minN").value);
var max = parseInt(document.getElementById("maxN").value);
if(min > max) {
alert("Minimum value must be lesser than maximum value. " + min + " > " + max );
}
}
function MaximumNValidate(){
var min = parseInt(document.getElementById("minN").value);
var max = parseInt(document.getElementById("maxN").value);
if(max<min) {
alert("Maximum value must be greater than minimum value." + min + " > " + max );
}
}
Comparing two input fields
A tidy way to do it which is easy to read:
var firstInput = document.getElementById("first").value;
var secondInput = document.getElementById("second").value;
if (firstInput === secondInput) {
// do something here if inputs are same
} else if (firstInput > secondInput) {
// do something if the first input is greater than the second
} else {
// do something if the first input is less than the second
}
This allows you to use the values again after comparison as variables (firstInput), (secondInput).
Javascript Compare Numbers
Your value from the input is still returned so you must convert the input value to a number.
You can do it like so:
var UpperLimit = Number(document.getElementById("UpperLimit").value);
var LowerLimit = Number(document.getElementById("LowerLimit").value);
Compare input and number
Javascript is case sensitive so you check against userBet
which is set to 0 and never changes.
In you getNr
function you set userbet
- change this to userBet
and it will work:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Web Roulette</title></head>
<body> <p id="number" value=""> </p> <form id="form3" name="form3" method="post" action=""> <label> <input type="radio" name="rollSelection" value="0" id="rollSelection_0" />0 </label> <br /> <label> <input type="radio" name="rollSelection" value="1" id="rollSelection_1" />1 </label> <br /> </form> <form name="form2" method="post" action="" onsubmit="genNr(); return false"> <label>Bet: <input name="betInput" type="text" id="betInput" size="5" /> </label> <input type="submit" name="placeBet" id="placeBet" value="Roll" onclick="genNr();" /> <p id="roll">???</p> <!--<p id="money">Money:</p>//--> </form>
<script type="text/javascript"> var userBet = 0;
function genNr() { if (document.getElementById('rollSelection_0').checked) { userBet = 0; } else if (document.getElementById('rollSelection_1').checked) { userBet = 1; } document.getElementById('number').innerHTML = Math.floor(Math.random() * 2) + 0;
if (parseInt(document.getElementById('number').innerHTML) == userBet) { document.getElementById('roll').innerHTML = "You Win!"; //document.getElementById('money').innerHTML = "Money: " + betInput * 2;
} if (parseInt(document.getElementById('number').innerHTML) != userBet) { document.getElementById('roll').innerHTML = "You Lost!"; }
} </script></body>
</html>
Related Topics
Googlemaps Does Not Load on Page Load
Bootstrap 3: How to Make Head of Dropdown Link Clickable in Navbar
React Native Flexbox - How to Do Percentages || Columns || Responsive || Grid etc
Safari Bug :First-Child Doesn't Update Display:Block When Items Are Removed with Js
Make View 80% Width of Parent in React Native
Auto Image Resize Based on Browser Window Dimensions
Set a Font Specifically for All Numbers on the Page
Remove CSS Rules by JavaScript
How to Animate the Opacity of the Background of a Div
D3 Bar Graph Example Not Working Locally
Include CSS and JavaScript in My Django Template
Select Element by CSS Style (All with Given Style)
Bootstrap 3 Compatible with Current Angularjs Bootstrap Directives
Copy All Styles from One Element to Another