-4

I have a input field with number and text , i want to split only number and use it for next actions how to do ?

<input type="text" value="remain 24" name="none">
i need only 24 from value remain 24
Nambi N Rajan
  • 491
  • 5
  • 15

3 Answers3

1

Working Fiddle

html:

<input id="txtInput" type="text" value="remain 24" name="none">

Jquery :

var Number = $("#txtInput").val().split(' ')[1];
alert(Number);

Also you can use Regex.

Number  = $("#txtInput").val().match(/\d+/); 

Updated fiddle

4b0
  • 21,981
  • 30
  • 95
  • 142
1
<input type="text"  value="remain 24" name="none">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        var yourTxt = $('input').val();
        var number = yourTxt.replace(/[^0-9]/g, '');
        $('input').val(number);
    });
</script>
Gayan
  • 2,845
  • 7
  • 33
  • 60
1

you can use regex to get only numbers from the string.

  1. First get the value from that input, and it will be a string

    var inputVal = $('input').val();

  2. var extractedNum = inputVal.match(/\d+/)[0];

Rohith K P
  • 3,233
  • 22
  • 28