Alex Rivera | Logout

How can get only numbers from the text?

Asked 2011-12-02T13:35:41.717
28

I want to get only the numbers(123) not the text(confirm), here is my code

<p>123confirm</p>

<script type="text/javascript">
$(document).ready(function(){  
  $('p').click(function(){  
    var sd=$(this).text();  
    alert(sd);
  });
});  
</script>
Edit
Report

1 Answer

6

You can also use this method:

$(document).ready(function(){  
  $(p).click(function(){  
    var sd=$(this).text();
    var num = sd.match(/[\d\.]+/g);
    if (num != null){
        var number = num.toString();
        alert(number );
    }
  });
});  
answered 2011-12-02T13:43:10.163

Your Answer