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/

4
  • How u want to open the link? On submit button click? Commented Jan 7, 2014 at 10:37
  • First thing you can do is check wether the data entered is numerical or not. Check isNaN() (javascript) or .isNumeric() (jQuery) functions. Commented Jan 7, 2014 at 10:37
  • 1
    You should move the if statement to the click handler. Commented Jan 7, 2014 at 10:39
  • @ParthoGanguly yes I want to open the link on submit button Commented Jan 7, 2014 at 10:40

5 Answers 5

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 :)

Sign up to request clarification or add additional context in comments.

5 Comments

got my +1. Your the only one testing if the entered data is numeric. Thanks !
double check the edit i made, plus keep in mind this had no testing, but i think it's right
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.
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 ?
Have a look at: stackoverflow.com/questions/3885817/… 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
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");
      }
    });  
});

2 Comments

here is the fiddle jsfiddle.net/2nppc/2 and its not working what i am doing wrong
@UmarKhan every this is fine in the fiddle but your spelling of the alert is not correct.
0
 <script>
      if ( parseInt($("#number").val()) <11) {
         window.location.href = '/link1';
      } 
      else{
        window.location.href = '/link2';
      }
</script>

2 Comments

What if the string entered isn't numerical ? Should you not test .isNumeric() first ?
You can check or you can restrict an input only to write numbers with the script in keydown event like this in stackoverflow.com/questions/995183/…
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
          }
         }
     });  
 });

1 Comment

AS you and other suggested i am doing it the same but its not working here is the fiddle link jsfiddle.net/2nppc/2
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';
       });
    });

2 Comments

What if the string entered isn't numerical ? Should you not test .isNumeric() first ?
Then it will do nothing because there is no 'else' statement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.