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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating a textbox

Status
Not open for further replies.

AndrewMozley

Programmer
Oct 15, 2005
621
GB
I would like to validate a single-character textbox when the user tabs onto the next field.

If the user enters a valid value (which I determine by looking up in a table) focus passes to the next control. If the value is invalid I display an error message and want focus to remain on the original field.

I can get this to happen by returning .F. from the Valid() method, but in this case the message "Invalid input" is displayed after my own error message (which I display using the Messagebox() function.

How can I suppress the "Invalid input" message, but still leave focus on the original text box?

Thanks. Andrew
 
SET NOTIFY OFF

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Thanks Olaf, bborisov and markros. I feel I ought to have known that ! I now see that this is documented under the "Valid event" where I should have looked.

A related question. Can I over-ride the effect of this validation in the control that I navigate to? For example, when this validation fails, I don't want to let the user go on to the next data entry field. But it may be reasonable to let the user click on a 'Close' button to close the screen; at present my validation insists that I enter a valid code in the original box before I can click on the close button.

Andrew
 
A solution to that is to Set an exceptional button you still want to allow users to click as a Cancel button, set it's cancel property to .T.

This does not introduce and "magic" of reverting data changes or such, but it means, that pressing ESC is a hotkey for that button and clicking that button also creates a virtual keypress of ESC. And this is, what you can check in the valid event:

Code:
IF Lastkey()=27 && ESC key pressed or Cancel Button clicked
   Return .T.
ENDIF
* normal validation code

Another general solution is to create a central validation method and not use the control.valid event at all.

Bye, Olaf.



 
IMHO do not use valid event at all. You have other (and IMHO much better lightweight) options:

1) Since you would allow to enter one of preset characters you could use
Format: M
Inputmask: < your single letter list here >

user wouldn't be able to select anything other than those letters. The good side of this he wouldn't be blocked by validation. This one is like a Combo without a Combobox control.

2) Instead of Valid event use Lostfocus event. Using LostFocus the control is in your hands. You can invoke another form, change the entered value, display your own message and of course could stay on that field or move to an object of your heart's content as you wish. ie:

Code:
* LostFocus

* Do some checks ...

if DataIsValid
   * Nothing - let it go as if valid returned .T.
   * or anObject.SetFocus()
else
   * Display your own message in whatever way you want
   * unlike Valid, you can even use a separate form
   * for a detailed description + anything you want
   
   * Clear out the field if you want
   this.Value = ''

   * If should stay on this field
   NODEFAULT
   this.SetFocus()
endif

Cetin Basoz
MS Foxpro MVP, MCP
 
You can use this method for your form
Code:
*************************************************************  Description.......: CancelValidation
*  Calling Samples...:
*  Parameter List....:
*  Created by........: ideas by John Koziol /Cetin Basoz
************************************************************local llReturn, loObject
local array laMouseObj[1]

llReturn = lastkey() = 46 && Alt+C - shortcut for Cancel button

if not m.llReturn
	amouseobj(laMouseObj,1) && object under mouse
	loObject = laMouseObj[1,1]

	if vartype(m.loObject) = "O"
		if not thisform.lValidateOnClose
			llReturn = thisform.releasetype = 1 or ;
				(inlist(lower(justext(sys(1272, m.loObject))),"cmdcancel", "cmdclose") ;
				and mdown()) && We're checking for Release type and Close button
		else
			llReturn = lower(justext(sys(1272, m.loObject))) = "cmdcancel" ;
				and mdown()
		endif
	else
		llReturn = .f.
	endif
endif

return m.llReturn

Now, in the textbox's valid put:
Code:
if not thisform.CancelValidation()
* Continue with the valid logic
endif
Therefore if a user clicks on Cancel button or tries to close the form using upper right x button, he/she can do it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top