0

Hi guys i m new to jquery and got stuck at a point ,i need ur help to Validate the textbox that only accept the numerics value on onclick function of the submit button

Here is the jquery code

<script>
function validate()
 {
   var number=$('#number').val();
   if(number=="")
  {
   alert("only number");
   return false;
  }
 }

and html

  <input type="text" id="number"value="" />
  <input type="submit" onclick="return validate();"/> 
Krish taylor
  • 5
  • 1
  • 3
  • What is not working about what you have tried? If you are asking how to check if a value is a valid number, that question [already has an answer here](http://stackoverflow.com/questions/175739/is-there-a-built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number). – dehrg Sep 08 '14 at 16:42

2 Answers2

0

Just parse as number and check if It's number:

function validate()
{
   var number = $('#number').val();
   var cnumber = parseFloat(number);

   if(isNaN(cnumber))
   {
    alert("only number");
    return false;
   }
   return true;
 }

Or just:

function validate()
{
   var number = $('#number').val();

   if(isNaN(number))
   {
    alert("only number");
    return false;
   }
   return true;
 }

Working example here: http://jsfiddle.net/fals/Lbq9td9j/

Fals
  • 6,813
  • 4
  • 23
  • 43
0

Using jquery you can do:

 var currentValue = $(obj).val();
     if($.isNumeric(currentValue)){.....
Fabio
  • 116
  • 1