Cara menggunakan javascript key number

Terkadang untuk mencegah user memasukan nilai yang lain selain yang dimaksud diperlukan. Tujuannya adalah untuk mencegah kesalahan input.

Berikut ini adalah salah satu trik pada javascript untuk mencegah pengguna memasukan nilai lain selain angka pada browser.

function onlyNumbers(evt) { // Mendapatkan key code var charCode = (evt.which) ? evt.which : event.keyCode; // Validasi hanya tombol angka if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; }

Penjelasan:

  • var charCode = (evt.which) ? evt.which : event.keyCode;
    Berfungsi untuk mendapatkan nilai key code.
    Pada browser berbasis IE, pada saat pengguna menekan tombol, event yang diaktifkan adalah event.keyCode.
    Sedangkan pengguna non-IE, kebanyakan menggunakan event.which
  • if (charCode > 31 && (charCode 57))
    Kode tombol numerik/angka pada Javascript adalah 48-57. Selain itu, tombol lain tidak akan diterima.
    Untuk lebih lanjut mengenai Javascript Key Code masuk ke tulisan sebelumnya (Javascript Char Codes (Key Codes)).

Penggunaan

Dengam memanfaatkan script di atas, tambahkan atribut onKeyPress pada tag <input /> dengam mengaktifkan fungsi di onlyNumbers tersebut.

<input type="text" name="nomor" onKeyPress="return onlyNumbers(event);" value="0" />

Example

Get the Unicode value of the pressed keyboard key:

var x = event.charCode;

Try it Yourself »

More "Try it Yourself" examples below.

Definition and Usage

Note: The charCode property is deprecated. Use the key property instead.

The charCode property returns the Unicode character code of the key that triggered the onkeypress event.

The Unicode character code is the number of a character (e.g. the number "97" represents the letter "a").

Tip: For a list of all Unicode characters, please study our Complete Unicode Reference.

Tip: If you want to convert the Unicode value into a character, use the fromCharCode() method.

Note: If this property is used on onkeydown or onkeyup events, the returned value is always "0".

Note: This property is read-only.

Note: The charCode property is not supported in IE8 and earlier. However, for these browser versions, you can use the keyCode property. Or, for a cross-browser solution, you could use the following code:

var x = event.charCode || event.keyCode; // Use either charCode or keyCode, depending on browser support

Tip: You can also use the keyCode property to detect special keys (e.g. "caps lock" or arrow keys). However, both the keyCode and charCode property is provided for compatibility only. The latest version of the DOM Events Specification recommend using the key property instead (if available).

Tip: If you want to find out whether the "ALT", "CTRL", "META" or "SHIFT" key was pressed when a key event occured, use the altKey, ctrlKey, metaKey or shiftKey property.

Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Property
charCode Yes 9.0 Yes Yes Yes

Syntax

Technical Details

Return Value:DOM Version:
A Number, representing the Unicode character code
DOM Level 2 Events

More Examples

Example

A cross-browser solution to get the Unicode value of the pressed keyboard key:

// Use charCode if the browser supports it, otherwise use keyCode (for IE8 and earlier)
var x = event.charCode || event.keyCode;

Try it Yourself »


Example

Alert some text if the user presses the "O" key:

function myFunction(event) {
  var x = event.charCode || event.keyCode;
  if (x == 111 || x == 79) { // o is 111, O is 79
    alert("You pressed the 'O' key!");
  }
}

Try it Yourself »


Example

Convert the Unicode value into a character:

var x = event.charCode || evt.keyCode;   // Get the Unicode value
var y = String.fromCharCode(x);          // Convert the value into a character

Try it Yourself »

Related Pages

HTML DOM reference: KeyboardEvent key Property

HTML DOM reference: KeyboardEvent keyCode Property

HTML DOM reference: KeyboardEvent which Property


Postingan terbaru

LIHAT SEMUA