Skip to content

Instantly share code, notes, and snippets.

@Zenger
Created June 9, 2012 07:04
Show Gist options
  • Save Zenger/2899902 to your computer and use it in GitHub Desktop.
Save Zenger/2899902 to your computer and use it in GitHub Desktop.
Javascript/PHP numbers Only
/* Without jQuery */
/* Example
<input id="myInput" name='credit-card' value='' />
*/
document.getElementById('myInput').onkeyup = function() {
this.value = this.value.replace(/[^\d]/gi , '');
};
/* With jQuery */
jQuery('myInput').bind('keyup' , function() {
$(this).val ( $(this).val().replace(/[^\d]/gi , '') );
});
/* Without jQuery */
/* Example
<input id="myInput" name='credit-card' value='' />
*/
document.getElementById('myInput').onkeyup = function() {
this.value = this.value.replace(/[^\d]/gi , '');
};
/* With jQuery */
jQuery('myInput').bind('keyup' , function() {
$(this).val ( $(this).val().replace(/[^\d]/gi , '') );
});
<?php
function onlynumbers ($var = null) {
return preg_replace('/[^\d]/i', '', $var);
}
echo onlynumbers("good123");
?>
/* This gist was created for a specific task, and mainly to keep the non jquery that it won't be lost somewhere. */
/* This makes use of regular expressions */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment