Welcome to Abdul Malik Ikhsan's Blog

Javascript cek TextField Numeric

Posted in Uncategorized by samsonasik on June 25, 2008
Code:
<script>
//cek numeric : http://codingforums.com/archive/index.php?t-75239.html
function valid(f) {
var re = /^[0-9]*$/;
if (!re.test(f.value)) {
f.value = f.value.replace(/[^0-9]/g,””);
}
}
</script>

<input type=”text” name=”no_hp” id=”no_hp” maxlength=”20″ onkeyup=”valid(this)“>
—————–
atau pakai yang ini 🙂

Code:
<script>
function jsOnlyNumber(evn) {
var keynum;
var validKeyCode = new Array(8,9,13,16,17,18
,27,32,35,36,37,38,39,40,46,
48,49,50,51,52,53,54,55,56,57,
96,97,98,99,100,101,102,103,104,105);if (evn.shiftKey == 1) { return false; }

if (window.event) { keynum = evn.keyCode } //IE euy
else if(evn.which) { keynum = evn.which }; //Netscape/Firefox/Opera

if (arguments.length > 1) {
for (var i=1; i
validKeyCode[validKeyCode.length] = arguments[i].charCodeAt(0);
}
}

for (var i=0; i< validKeyCode.length; i++) {
if (keynum == validKeyCode[i]) { return true; }
}

return false;
}
</script>

<input type=”text” name=”no_hp” id=”no_hp” maxlength=”20″ onkeydown=”return jsOnlyNumber(event)”>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: