Learn the Rules, Break The Rules, and Create the New Ones...

Hi... My name is Rizky Prihanto. You can call me RQ, or Rizky either. I am currently living on Bandung, Indonesia. Had a lot of works and research about Enterprise Information Systems (majoring on education and e-governments). I have bunch of interests (some friends call it 'freakz') about MySQL Opensource Database and now I am one of the administrator of MySQL Indonesia User Group - the opensource community initialized by Sun Microsystems Indonesia.

My Company PT Cinox Media Insani, Bandung, Indonesia. I work here since 2008 and I take responsibility as Chief of Software Architect. My job is about planning, imaginating, fantasy-ing, concepting, and build the infrastructure of the new information systems (or app engines) which going to be implemented.

This blog This is my blog that represent my current opinion, research and experiences about anything in Software Engineering. Written since 2007 (actually) and has been vaccum for a lot of while. And now I wanna ressurrect this blog (optimistically) from the long-long-hibernation with something fresh and new ideas -- still about MySQL, software engineering, development, and may be something managerial here.

About the tagline I've learned the statement above from some paper written by Kent Beck about Extreme Programming (XP) methodology -- some sort of practical software development methods which have no boundaries. That's very inspiring me a lot. I have written some article on this blog that tell my interpretation about that statement here.

My Another Blogs I have classifying my blogs into some sort of genre. The blog that you read here right now is my primary blog that tell you (majoring) about IT stuff. But if you wanna look another side of me, you can visit here, here, here,or here. Hope it'll be interesting for some of you.

Credits I would thanks to Blogger for this great blog platform. Skinpress who designed this Wordpress template (which is bloggerized by Free Blogger Templates). My appreciate is also going to you who give your generously time for visiting my blog.

Beberapa Cara Validasi Input Numerik

Bagi ente-ente pade programmer VB mungkin udah sering makae/bikin suatu routine untuk validasi input numerik pada textbox. Maksutnya biasanya ya supaya user cuman bisa ngetikin angka di textbox. Pendekatan2 yang dipake macem2, dan di sini ogut akan mencoba membahasnya. ^_^ hehehe, ga mutu bangedd nih postingan (habis, pengen nulis blog tp ngga da bahan siih...)

PERTAMA
ada yang "cukup" makae function bawaan VB, isnumeric(). Biasanya ditaroh di event lostfocus() atau di event validate(). contoh codingnya mungkin begini :

Private Sub Entri_LostFocus(Index As Integer)
If IsNumeric(Entri(Index).Text) = False Then
MsgBox "Sori, input cuman boleh angka doank!", _
vbCritical, "Pesan"
Entri(Index).SetFocus
End If
End Sub


cara ini punya ciri : pencegahan inputan selain angka dilakukan *setelah* user mengisikan nilai textbox (ngetikin textboxnya).. dan untuk proses validasinya, perlu dilakukan 2 kali : sekali di event lostfocus/validate, dan *musti* di cek sekali lagi pas event tombol simpan di klik (misalnya). Klo ngga gtu, masih ada peluang untuk kelolosan tuh...

KEDUA
ada juga yang nyoba nge-trap inputan pada event textbox_keypress()
caranya dengan cuman mengijinkan karakter dengan kode ascii tertentu yang bisa diketikin di dalam tu textbox. contoh codingnya seperti ini :

Private Sub Entri_KeyPress(Index As Integer, _
KeyAscii As Integer)
If Not ((KeyAscii >= Asc("0") And _
KeyAscii <= Asc("9") Or _
KeyAscii = vbKeyBack)) Then
Beep KeyAscii = 0
End If
End Sub


cara ini cukup shiepp.. apalagi bisa dimodifikasi dengan nambahin list-exclusion karakter yang boleh diijinin utk diketik di dalam textbox (misal : tanda koma ato tanda titik)


KETIGA
Ada lagi cara laen yang memanfaatkan API, dimana *secara paksa* textbox akan di-inisialisasi-kan hanya bisa nerima input angka thox. Kebutuhan dasar pemanggilan API Calls-nya seperti ini (udah kubuatkan function nya sekalian jadi tinggal make' doank)
'-- di module :
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

Private Const ES_NUMBER = &H2000&
Private Const GWL_STYLE = (-16)

Public Function SetNumerik(NamaControl As Control, _
Optional Flag As Boolean = True) As Long
Dim curstyle As Long
curstyle = GetWindowLong(NamaControl.hWnd, GWL_STYLE)
If Flag Then
curstyle = curstyle Or ES_NUMBER
Else
curstyle = curstyle And (Not ES_NUMBER)
End If
SetNumerik = SetWindowLong(NamaControl.hWnd, _
GWL_STYLE, curstyle)
NamaControl.Refresh
End Function


'-- contoh pemanggilan di form, cukup di FORM_LOAD doank.. :

Public Sub Form_Load()
SetNumerik entri(0)
SetNumerik Entri(1)
End Sub


Cara ini, tampaknya keren dan mudah. Emang sih! Tapi tetep ada aja konsekuensinya... textbox (ato combo, maskedit juga bisa) akan *bener2* cuman bisa nerima inputan angka. Ngga bs custom, ngijinin titik ato koma utk nulis pemisah desimalnya.

Dalam beberapa kasus, aku lebih seneng pake teknik KEDUA. Tapi dalam *banyak kasus*, bagiku pendekatan API yang kucontohin di teknik KETIGA itu cukup sering ku-implements dalam code ku...

3 comments:

cakyus mengatakan...

kalo masalah verifikasi input,
aku lebih seneng pake regexp,
lebih flexible, eg

digit aja -> ^[0-9]+$

6 digit + 1 alphabeth -> ^[0-9]{6,6}[a-zA-Z]{1,1}$

Anonim mengatakan...

itukan di text box kalo di penginputan terjadi datagrid bagaimana? tolong pencerahannya...

wawa_punya mengatakan...

maaf om kalo di cell tertentu di datagrid bisa ga hanya boleh diinput numerik saja seperti di kolom jumlah gt?

trims

Posting Komentar