473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Validation of Inputs to Text Boxes

In the Tax computation Software I am developing, I want only numeric characters to be accepted by the Text Boxes. Bellow is my sample code that refuses to work:
Private Sub txtBasicSalary_KeyPress(keyAscii As Integer)
If IsNumeric(txtBasicSalary.Text) = True Then
txtBasicSalary.Locked = False
Else
txtBasicsalary.Locked = True
End If
End Sub

Thank You.

Akinyemi
Dec 21 '06 #1
3 1537
devonknows
137 100+
Ok you can you the Text1_KeyPress(KeyAscii as Integer) that will stop people pressing cerain keys in your text box

Example:
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtdelay_keypress(KeyAscii As Integer)
  2. 'check to see if keyascii is between 48 & 57 (0-9)
  3. 'and allow 8 (Backspace)
  4. If Not ( _
  5.         (KeyAscii >= "48" And KeyAscii <= "57") Or _
  6.         (KeyAscii = "8") _
  7.     ) Then
  8.         ' Cancel the character.
  9.         KeyAscii = 0
  10.     End If
  11. End Sub
  12.  
but the problem is with that people can still use paste in text which can play hell with coding, as it took me a long time to figure out <lol>.
(CRTL+V / Shift + Ins / Right Click + Paste)

so i would suggest removing your Text1_KeyPress Code and use something along these lines...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Text1_Change()
  2.  Static OldText As String
  3.  Static InRoutineFlag As Boolean
  4.  
  5.  If InRoutineFlag Then Exit Sub
  6.  InRoutineFlag = True
  7.  
  8.  Select Case LS_FailNumeric(Text1.Text)
  9.  Case True: Text1.Text = OldText
  10.  Case False: OldText = Text1.Text
  11.  End Select
  12.  
  13.  InRoutineFlag = False
  14. End Sub
  15.  
  16. Private Function LS_FailNumeric(Text$) As Boolean
  17.  Dim L9%
  18.  ' Note: "" passes the test
  19.  For L9 = 1 To Len(Text)
  20. 'Insert Characters you want to allow just after the instr
  21. 'Current set up to allow 0123456789 and . (Period)
  22.  If InStr("0123456789.", Mid$(Text$, L9, 1)) = 0 Then
  23.  LS_FailNumeric = True
  24.  Exit Function
  25.  End If
  26.  Next
  27.  
  28. End Function
  29.  
Hope this helps you

Kind Regards
Devon.
Dec 21 '06 #2
devonknows
137 100+
Slight Update on the code above

just replace this segment of code

Expand|Select|Wrap|Line Numbers
  1.  Select Case LS_FailNumeric(Text1.Text)
  2.  Case True: Text1.Text = OldText
  3.  Case False: OldText = Text1.Text
  4.  End Select
  5.  
with

Expand|Select|Wrap|Line Numbers
  1. Select Case LS_FailNumeric(Text1.Text)
  2.  Case True: Text1.Text = OldText: Text1.SelStart = Len(OldText)
  3.  Case False: OldText = Text1.Text
  4.  End Select
  5.  
This change will move the cursor to the end of the text in in the text box instead of reseting to the begining everytime of the text LS_FailNumeric = True so that the client/user doesnt have to move to the end again everytime.

Kind Regards
Devon
Dec 21 '06 #3
Killer42
8,435 Expert 8TB
You might also investigate the use of the "masked edit control". It allows you to specify a mask for the input (Eg. phone number, etc.) and I think you can limit it to only numeric input.
Dec 22 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Shankar Mani via DotNetMonster.com | last post by:
Hi folks, I am facing a problem while using a DataGrid. I have a webform with a datagrid containing two text boxes for edit viz., countrycode and country name. Like wise I have two more text boxes...
2
by: Doc Dunning | last post by:
Hi all, This seems such a simple problem to me but I can't find the answer after *much* searching. I have an application using bound controls to browse / update a database table of club...
4
by: jedimasta | last post by:
Good evening all, I'm a relatively new to javascript, but I've been working with ColdFusion and PHP for years so I'm not necessarily ignorant, just stuck and frustrated. Using ColdFusion I'm...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
5
by: kevinmajor1 | last post by:
Hello, all. I'm currently trying to write a script that will perform form validation upon submission. I know that more validation should be performed on the server's side...this is just a test to...
0
by: dhurwitz | last post by:
Hi, In my ASP.NET 2.0 web app, I have a user control, ucBinEdges, with several text boxes. The user control has several validation controls validating the Text of these text boxes -...
4
by: sanou | last post by:
Hi No doubt this question, or a similar version of it, has been asked before. But, I was looking around the forums and i couldn't find anything. I'm using VC++ to create a simple calculator...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
2
by: mrutyunjaya | last post by:
hello, i have two textbox column and one button control in gridvie when i click button it will ask plese enter price in first textbox . when enter values it is asking again. how to pass...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.