kindaquestion
kindaquestion

Reputation: 5

How to check if textbox value is not number?

<!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.

Upvotes: 0

Views: 2405

Answers (5)

Muskan Agarwal
Muskan Agarwal

Reputation: 388

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>

Upvotes: 0

kappo
kappo

Reputation: 282

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

Upvotes: 0

Kevin Kloet
Kevin Kloet

Reputation: 1086

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>

Upvotes: 1

Bhargav Chudasama
Bhargav Chudasama

Reputation: 7171

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>

Upvotes: 0

Crisoforo Gaspar
Crisoforo Gaspar

Reputation: 3830

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.
}

Upvotes: 1

Related Questions