Ahsan Amin


I am trying to implement validation on textboxes placed on a form using ISNull function. I placed the code in a procedure and calling it on the click event of a button but does not work. I want also want to check for numeric and character data. here is my code

IF ISNULL(thisform.text2.Value) then
MESSAGEBOX("Please enter customer name")
ENDIF




Re: Validations on Textbox

fvp4ever


Use this code:

IF ISBLANK(thisform.text2.value)

MESSAGEBOX("please...")

RETURN .f. &&abort execution of rest of the code

ENDIF

it works only for the text fields, for numerical fields you should change the code, anyway I don't think that you are going to have both text and numeric data types in single textbox





Re: Validations on Textbox

Ahsan Amin

i am checking numeric and character data types in single textbox. also tell me for both of them. can i find all such like functions to check following data types in VFPs help.
Data type
Character
Numeric (also float, double, and integer)
Currency
Date
DateTime
Logical
Memo
Object
General
Screen (created with SAVE SCREEN)
Undefined type of expression






Re: Validations on Textbox

Naomi Nosonovsky

I believe in your case you're looking for EMPTY() function which correctly works for every data type.

In other words:

if empty(thisform.myTextbox.value)

=messagebox(...)

endif





Re: Validations on Textbox

Naomi Nosonovsky

To check type of the expression you can use either vartype() or type functions. Check both of them in Help.





Re: Validations on Textbox

Christof Wollenhaupt

and if the value can be empty or NULL, you can use IF EMPTY(NVL(Thisform.myTextbox.Value,""))





Re: Validations on Textbox

MarciaAkins

Ahsan Amin wrote:

I am trying to implement validation on textboxes placed on a form using ISNull function. I placed the code in a procedure and calling it on the click event of a button but does not work. I want also want to check for numeric and character data.

In the Valid of the textbox:

Code Snippet

lcType = VARTYPE( This.Value )

IF EMPTY( NVL( This.Value, Str2Exp( ' ', lcType ) ) )

MESSAGEBOX( [Value cannot be empty], 48 )

RETURN 0

ENDIF

This code in Str2Exp:

Code Snippet
*-- Passed a string and a data type, return the expression after conversion to the specified data type
FUNCTION Str2Exp( tcExp, tcType )
*** Convert the passed string to the passed data type
LOCAL luRetVal, lcType

*** Remove double quotes (if any)
tcExp = STRTRAN( ALLTRIM( tcExp ), CHR( 34 ), "" )
*** If no type passed -- display error message
*** the FUNCTION is not clairvoyant
IF TYPE( 'tcType' ) = 'C'
lcType = UPPER( ALLTRIM( tcType ) )
ELSE
*** Type is a required parameter. Let the developer know
ERROR 'Missing Parameter: Expression type is a required parameter to Str2Exp'
ENDIF
*** Convert from Character to type
DO CASE
CASE INLIST( lcType, 'I', 'N' ) AND INT( VAL( tcExp ) ) == VAL( tcExp ) && Integer
luRetVal = INT( VAL( tcExp ) )
CASE INLIST( lcType, 'N', 'Y') && Numeric or Currency
luRetVal = VAL( tcExp )
CASE INLIST( lcType, 'C', 'M' ) && Character or memo
luRetVal = tcExp
CASE lcType = 'L' && Logical
luRetVal = IIF( NOT EMPTY( tcExp ), .T., .F. )
CASE lcType = 'D' && Date
luRetVal = CTOD( tcExp )
CASE lcType = 'T' && DateTime
luRetVal = CTOT( tcExp )
OTHERWISE
*** There is no otherwise unless, of course, Visual FoxPro adds
*** a new data type. In this case, the function must be modified
ENDCASE
*** Return value as Data Type
RETURN luRetVal
ENDFUNC






Re: Validations on Textbox

Naomi Nosonovsky

This is an overkill for the question asked.

You can simply create xEmpty function, which would be

Code Snippet

function xEmpty

* Checks if the value passed is either empty or null

lparameters tuVal

return empty(m.tuVal) or IsNull(m.tuVal)





Re: Validations on Textbox

MarciaAkins

Naomi Nosonovsky wrote:

This is an overkill for the question asked.

IMHO, that is part of the reason that I have been awarded the MVP award since 1999. My friend, Dan Freeman, once said that one of the things that makes one an MVP is the ability to answer, not only the question that is currently being asked, but also the next 2 or 3 that might follow it.

My Str2Exp function handles conversion of any string value to the specified data type, so it can be used for much more than just checking empty values. As a matter of fact, I use it constantly in the 3 productions apps on which I am currently working.






Re: Validations on Textbox

Naomi Nosonovsky

No question the function can convert character value into the requested type representation. My point is, that for the question asked, this function is not needed and in this particular expression is an overkill.

If the question is whether data is empty or null we can either use simple UDF or use

#define XEMPTY empty(m.tuVal) or Isnull(m.tuVal)

tuVal = thisform.txtTest.value

if XEMPTY

=messagebox(...)

endif

The Str2Exp function may come handy in other situations, no doubt.





Re: Validations on Textbox

MarciaAkins

Naomi Nosonovsky wrote:

It may come handy in other situations, no doubt.

Then, my point is that it is of value. I would also like to point out that this is a technical forum and your non-technical editorial comments do not add value to this thread.






Re: Validations on Textbox

Naomi Nosonovsky

BTW, in your function this line

CASE lcType = 'L' && Logical
luRetVal = IIF( NOT EMPTY( tcExp ), .T., .F. )

can be re-written to

luRetVal = not empty(tcExp)





Re: Validations on Textbox

Naomi Nosonovsky

I think my comments are important for this thread. We're talking about code efficiency.





Re: Validations on Textbox

Phil Brammer

Naomi Nosonovsky wrote:
I think my comments are important for this thread. We're talking about code efficiency.



Naomi,
Code efficiency is part of the conversation. The other, as stated earlier, is to provide alternative options to the question at hand and to provide a learning environment. A more better long term solution might be Marcia's function (with or without your code efficiency change). The goal here is not necessarily to provide one line of obfuscated code that "works efficiently" to the original poster, but to post something that helps them *learn* what it is that you are trying to convey.

Even when a user wants a simple, concise answer, it is often beneficial for other users who might view this thread to see other alternatives. It is this point that you need to work on when responding to posts. Please keep in mind that your answers are going to be viewed by other, future users and while they may have the EXACT same problem, most will have a problem slightly similar in which case having alternatives posted may very well be the answer they need. I'm not suggesting you post alternatives; keep doing what you're doing as you have quite a bit of VFP knowledge to share. But don't ever critisize another user's post when it's technically correct. Just let the forums work as they are supposed to.





Re: Validations on Textbox

Phil Brammer

Naomi Nosonovsky wrote:

This is an overkill for the question asked.



That declaration is not yours to declare.