0
<!DOCTYPE html>
<html>
<body>    
<div id="text" class="CommentBox">
    Some text :
    <input type="text" />
</div>   
<script>
$(document).ready(function () {
    jQuery("#text").on("change", function () {
        var x = $('#text').value;
        if (isNaN(x)) 
        {
            window.alert("You have entered not a number");
            return false;
        });
    });        
});
</script>    
</body>
</html>

I am trying to write javascript code to check if the given value is not number.If not i would like to give error message? If it is number I would like to check if it is integer and between 0 and 100.

2
  • var x = $('#text').val() ? Commented Oct 9, 2017 at 12:29
  • To check if it's between 0 and 100: if (x > 0 && x < 100) Commented Oct 9, 2017 at 12:31

5 Answers 5

1

Basically you need to convert to an Int before compare it with NaN which means something like:

var x = $('#text').value;
if ( isNaN( parseInt(x) ) ) {
   // Not a decimal number.
}
Sign up to request clarification or add additional context in comments.

Comments

1

There are a lot of syntax errors in your code.

Your selector checks your div for the change event instead of your input, which means it will never trigger the code.
You should use .val() to get the value of an element when using jQuery selectors instead of .value.
You can also use the this keyword inside the event handler to get the referenced element.
Besides that there were some misplaced ) and } in your code.

Below I have included an working sample of your code.

$(document).ready(function() {
  jQuery("#text > input").on("change", function() {
    var x = $(this).val();
    if (isNaN(x)) {
      window.alert("You have entered not a number");
      return false;
    } else if (x > 0 && x < 100) {
      alert("number in between 0 and 100");
    }
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="text" class="CommentBox">
  Some text :
  <input type="text" />
</div>

Comments

0
function numberOrNot(var input)
{
    try
    {
        Integer.parseInt(input);
    }
    catch(NumberFormatException ex)
    {
        return false;
    }
    return true;
}

this will return true if your input is number, otherwise it will return false

1 Comment

This is JS question. Please provide answer that is makes sense in JavaScript. 1st values from input come as strings - always. Meaning OP needs to use parseInt(x) to get the integer value and then use isNaN() to check if the value is number indeed. This is the convention.
0

try this code

you enter direct input on change or write id for input and pass it to javascript

$(document).ready(function() {
  jQuery("input").on("change", function() {
    var x = $('#text').val();
    if (isNaN(x)) {
      window.alert("You have entered not a number");
      return false;
    }
    else{
      if(x>=0 && x<=100)
      {
         window.alert("You have enter write number");
      }else{
         window.alert("You enter number between 0 to 100");
      }
    }

  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="CommentBox">
  Some text :
  <input type="text" id="text" />
</div>

1 Comment

so you can use id for that
0

You can use try-catch and put as many conditions you want in try block Like this. I have put three conditions for now.

<script type="text/javascript">
function Validation(){
    var number1=document.LoginForm.number1.value;
    try{
        if(number1==""){
            throw "Empty";
        }
        if(isNaN(number1)){
            throw "Not a Number";
        }
        if(number1<0 || number1>100){
            throw "Out of range";
        }
    }
    catch(err){
        if (err=="Empty"){
            alert("Number 1 and Number 2 fields cannot be empty");
        }
        if (err=="Not a Number"){
            alert("Please enter a number");
        }
        if(err=="Out of Range"){
            alert("Out of Range");
        }
        return false;
    }
    //return true;
}
</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.