2

I am trying to make a function using jquery. And what i am trying to do is that i have a text field and submit button so when i insert numeric value from 1 to 10 it open up a link if the input value is from 11-20 on button click it should open different link.

html

<input type="text" Id="number" />
<input type="submit" id="button" />

jquery

 $( document ).ready(function() {
   if ( $("#number").val() >= 19200 && num <= 19276 ) {
    $( "#button" ).click(function() {
      alert( "u enterd the number " );
    });  
   }
 });

http://jsfiddle.net/2nppc/1/

Umar Khan
  • 454
  • 7
  • 30

5 Answers5

4

You're going to need something along these lines:

$(document).ready(function(){
    $('#button').click(function(){
         var number = $('#number').val();
         //check if number
         if($.isNumeric(number)){
             if(number >= 1 && number <= 10){
                 //1-10 action
             }elseif(number >= 11 && number <= 20){
                 //11-20 action
             }
         }else{
             //Not a number
         }
    });
});

EDIT: sorry mixed up the format for checking numeric, sorted now :)

James
  • 788
  • 2
  • 10
  • 28
  • got my +1. Your the only one testing if the entered data is numeric. Thanks ! – TCHdvlp Jan 07 '14 at 10:43
  • double check the edit i made, plus keep in mind this had no testing, but i think it's right – James Jan 07 '14 at 10:45
  • 1
    Hi, I looked at your fiddle, you've misspelt `alert`, that's causing the issue. Are you using chrome? If so, press F12, it will give you error messages for JS and JQuery. – James Jan 07 '14 at 10:53
  • Is there an interest to check for real values ? If I enter `6.5` it is accepted. So If I only want integer values, how to check it ? And, if real values are allowed, `6,5` will be considered as a string and wont fired the link. Is there a good way to handle those kind of situations ? – TCHdvlp Jan 07 '14 at 10:57
  • Have a look at: http://stackoverflow.com/questions/3885817/how-to-check-if-a-number-is-float-or-integer to get if integer. As for 6,5, you could either replace the ',' with a '.' then convert to a number. Though you're then making an assumption about what the user wants to input. I'd let the `if($.isNumeric(number)){` catch it as a string (and not fire the link) and ask the user to enter a valid number – James Jan 07 '14 at 11:10
1

use this code:

$( document ).ready(function() {
    $( "#button" ).click(function() {  
      var num = $("#number").val();
      if(!isNAN(num)){
      if (num >= 1 && num <= 10) {
         window.location.href="your url for 1 to 10";
      } else if(num >= 11 && num <= 20) {
         window.location.href="your url for 11 to 20";
      } else {
         //your other code or action
      }
      } else {
         alert("Enter the numeric value");
      }
    });  
});
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

Without testing:

    $(function(){
       $('#button').on('click', function(){
         if ($('#number').val() >= 0 && $('#number').val() <= 10)
           location.href = 'blabla';
         else if ($('#number').val() > 10 && $('#number').val() <= 20)
           location.href = 'blabla';
       });
    });
GuyT
  • 4,316
  • 2
  • 16
  • 30
0
 <script>
      if ( parseInt($("#number").val()) <11) {
         window.location.href = '/link1';
      } 
      else{
        window.location.href = '/link2';
      }
</script>
edonbajrami
  • 2,196
  • 24
  • 34
  • What if the string entered isn't numerical ? Should you not test `.isNumeric()` first ? – TCHdvlp Jan 07 '14 at 10:42
  • You can check or you can restrict an input only to write numbers with the script in keydown event like this in http://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery – edonbajrami Jan 07 '14 at 10:49
0
 $( document ).ready(function() {
          $( "#button" ).click(function() {   // Replace with Id of the button
          var num = $("#number").val();       // Replace with Id of the textbox
          if(!isNaN(num)){
          if (num >= 1 && num <= 10) {
            window.location.href="your url for 1 to 10";
          } else if(num >= 11 && num <= 20) {
            window.location.href="your url for 11 to 20";
          } else {
            //your other code or action
          }
         }
     });  
 });
Developer
  • 759
  • 2
  • 9
  • 24
  • AS you and other suggested i am doing it the same but its not working here is the fiddle link http://jsfiddle.net/2nppc/2/ – Umar Khan Jan 07 '14 at 10:54