Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating numbers (or letters) typed in a textbox within a form 3

Status
Not open for further replies.

Phil Thoms

Programmer
Oct 31, 2005
245
GB
Hello,
I use VFP version 6 and I am trying validate a textbox where the user may type 1, 2, 3 or 4 but how do you restrict the user to just typing in one of these 4 numbers.
Any tips on validation as I don't have any manuals.

Thanks
 
You can do a validation of the typed values in the Textbox's VALID method.

Generally I save off the original Textbox value by using the WHEN method ((I add a property ThisForm.WhenValue = This.Value) and then only do a validation if the value has Changed (IF !(This.Value == ThisForm.WhenValue) etc).

In the event of an invalid entry, I notify the user with either a WINDOW message or a MESSAGEBOX warning and then the last code line replaces the entered value with the saved off original value or, if you want, some other default value.

There are other ways of limiting the Textbox you might want to look at:
1. The MaxLength can be used to limit the number of characters
2. The InputMask & Format can be used to limit the types of characters entered
3. RangeLow Event & RangeMax Event can be used (see VFP Help)

Good Luck,
JRB-Bldr

 
You can do it by adding this code to the Valid event of the textbox:

Code:
IF BETWEEN(this.value, 1, 4)
  RETURN .t.
ELSE
  MESSAGEBOX("Please enter a number 1 to 4")
  RETURN .f.
ENDIF

When the Valid returns .F., the user is prevented from leaving the textbox. You are forcing them to enter a number in the valid range.

Note that the above code assumes that the textbox is of a numeric data type. If it is a character data tupe, change the first line as follows:

Code:
IF BETWEEN([b]VAL([/b]this.value[b])[/b], 1, 4)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Maybe consider using a different control. For this example, you have lots of choices of controls that would limit the user to the values 1 through 4. A spinner, a dropdown, option buttons.

Tamar
 
Thanks for all your help, much clearer now.
Is there a property for declaring the textbox as numeric or character and what if the choices were 1,2,3 or 6 ?? I seem to remember $'123479' as an example from the old FoxPro 2.6 days.

Thanks once again.
 
To specify if the textbox is numeric or a character, you simply give it an initial value of the appropriate type. So if you do this (in the textbox's Init):

Code:
this.value = ""

it will be character, whereas:

Code:
this.value = 0

it will be numeric.

You can also set the value interactively, in the property sheet.

The above doesn't apply if the textbox has a ControlSource. In that case, the control has the same data type as the underlying field.

The $ operator will still work, as before. Alternatively, you can use INLIST():

Code:
IF INLIST(this.value, "1", "2", "3", "4", "6")

or
Code:
IF INLIST(this.value, 1, 2, 3, 4, 6)

as appropriate.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
If the choices are not sequential, then the spinner is out, but you could still use a dropdown or an option group.

That said, I recommend the option group only if the list is really fixed. If the list of choices can vary over time, a dropdown is a better choice. Store the list of choices somewhere and then fill the dropdown based on that stored list.

Tamar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top