To allow negative values you can use the regex /^-?\d[.]?\d
$/.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery Allow Only Numeric Input in HTML Text Input Field</title> <style> .success {//from ww w . j a v a 2 s . com outline: 2px solid green; } .error { outline: 2px solid red; } </style> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script> $(document).ready(function(){ var regex = /^-?\d*[.]?\d*$/; $("#myInput").on("input", function(){ var inputVal = $(this).val(); if(regex.test(inputVal)) { $(this).removeClass("error").addClass("success"); } else{ $(this).removeClass("success").addClass("error"); } }); }); </script> </head> <body> <input type="text" id="myInput"> </body> </html>