How to Check If a Value is Numeric or Not in jQuery
In this tutorial, we are going to see how to check if a value is numeric or not in jQuery. You can use jQuery’s isNumeric() method to check whether a value is numeric or not.
isNumeric() returns true only if the argument is of type “number” or if it is of type string and it can be converted to a number, otherwise it returns false.
HTML Code:
<!DOCTYPE html> <html> <head> <title>Check If a Value is Numeric or Not</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script type="text/javascript"> // Put the jQuery code here. </script> </head> <body> <input type="text" id="input" name="value" placeholder="Type a value"> <p id="output"></p> <input id="check" type="button" value="Check value" /> </body> </html>
jQuery Code:
$(document).ready(function() { $("#check").click(function() { if ($.isNumeric($('#input').val())) { $('#output').html('The value is a number.'); } else { $('#output').html('Value is not a number.'); } }); });
Result |
---|