-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
  • 471
  • 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
  • 20,627
  • 30
  • 92
  • 137
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>
Mak
  • 2,765
  • 7
  • 31
  • 56
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
  • 2,805
  • 20
  • 27