0

I want to compare two inputs minimumN and maximumN, and display an alert if the logic is not satisfied, I have the following code:

HTML:

<table>
    <tr>
        <th>Minimum N</th>
        <td>
            <input id="minN" onkeyup="MinimumNValidate()" type="text" maxlength="50">
        </td>
        <th>Maximum N</th>
        <td>
            <input id="maxN" onkeyup="MaximumNValidate()" type="text" maxlength="50">
        </td>
    </tr>
</table>

Javascript:

function MinimumNValidate() {
    var min = document.getElementById("minN").value;
    var max = document.getElementById("maxN").value;
    if (min > max) {
        alert("Minimum value must be lesser than maximum value.");
    }
}

function MaximumNValidate() {
    var min = document.getElementById("minN").value;
    var max = document.getElementById("maxN").value;
    if (max < min) {
        alert("Maximum value must be greater than minimum value.");
    }
}

but this is not working, and the alert is being displayed even when the minN is less than maxN, can you point out the mistake in my code ?

9
  • 2
    This question appears to be off-topic because it is about a typo Commented Sep 20, 2013 at 20:03
  • 1
    It is not a typo, it is parse Int issue but yes.. it has a typo there. Commented Sep 20, 2013 at 20:08
  • It's a simple typo, but really, off-topic / not about programming? Commented Sep 20, 2013 at 20:09
  • sorry about the typo, i have corrected the typo, but still it doesn't seem to be working, can you help ? Commented Sep 20, 2013 at 20:13
  • @Sachin Can you tell us which inputs are causing the fail? I bet it is a parseInt issue, try this for both validation methods: var min = parseInt(document.getElementById("minN").value); var max = parseInt(document.getElementById("maxN").value); Commented Sep 20, 2013 at 20:13

3 Answers 3

4

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 );
       } 
  }
Sign up to request clarification or add additional context in comments.

Comments

3

In the second function MaximumNValidate(), you have the line of code

if(maxN<min) {

which should be

if(max<min) {

4 Comments

+1, but it's annoying as hell ;) jsfiddle.net/FyAnR (@Sachin, use onchange or add some more checks);
even after that, i am getting the same output, is the logic correct ?
@Sachin, the output is correct, but ''<1, so it alerts before you even start to type the max value. Same for typing 100 (three alerts!) in the min, and then typing the max 1 then 0 (two alerts!) then `1' (no alert).
i have used the onchange event, the number of alerts have decreased.
1

function checknumber(theForm) {   
 if (parseInt(theForm.num2.value) != (parseInt(theForm.num1.value)+1)) 
    { 
    alert('enter the correct year in intermediate');
            return false;
            } 
     return true;
            
}
<!doctype html>
<head> </head> 
    <body> 
        <form action="../" onsubmit="return checknumber(this);">     
<label> tenth graduation </label> 
      <input type="number" name="num1"  autocomplete="off"><br> <br>
<label> Intermediate </label>
       <input type="number" name="num2" autocomplete="off">
     <input type="SUBMIT" value="year validation">
       </form> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.