Ahsan Amin


I am trying to validate numeric and character type data, entered by user in textboxes placed on the form. Can any expert tell me which functions to use or you could tell me what should i type index button of VFPs help to find all type of data validation functions



Re: Validations

dni


Are you using for validation, "valid" method There are no functions dedicated to validate, but you may build validation methods depending on your needs for each field. Can you show a exemple for validation







Re: Validations

Ahsan Amin

lcCustName = ALLTRIM(thisform.text2.value)

IF ISBLANK(lcCustName) then
MESSAGEBOX("Please enter customer name")
RETURN .f.
ENDIF

IF LEN(lcCustName) > 15 then
MESSAGEBOX("Please enter correct name")
thisform.text2.SetFocus
RETURN .f.
ENDIF






Re: Validations

dni

The code seems to be correct, I don't understand what are doing return .f., you may try just return. You may put it in valid method.






Re: Validations

Ahsan Amin

dear the code is fine and is working but what i want to know how to validate character and numeric data. In other language i have uses functions like ISChar(), IsNum(), ISDate() etc...





Re: Validations

Naomi Nosonovsky

Code Snippet

lcCustName = ALLTRIM(thisform.text2.value)

IF Empty(lcCustName) then
MESSAGEBOX("Please enter customer name")
RETURN 0
ENDIF

IF LEN(lcCustName) > 15 then
MESSAGEBOX("Please enter correct name")
thisform.text2.SetFocus
RETURN 0
ENDIF

The only change I made is EMPTY instead of IsBlank and return 0.

Also note, that you can not SetFocus in Valid event.





Re: Validations

Ahsan Amin

that's fine, but first if condition will check wether the text box is empty or not and second will check the length but how to check that user has entered correct customer name like i don't want to alow other than alphabets for customer name field and i don't want to alow other than numeric in phone number field





Re: Validations

Naomi Nosonovsky

Ok, there are several ways to accomplish your result.

1. Use InputMask and Format properties of the textbox.

E.g. you would use A for Format property to allow only character (no digits)

In phone you may use (999) 999-9999 as an InputMask and R as a Format.

Check Help on Inputmask and format.

If you don't like this approach, here are some other ideas.

Code Snippet

lcAllowedChars = [ABCDEFGHIJKLMNOPQRSTUVWXYZ]

if empty(chrtran(upper(m.lcTextVal), m.lcAllowedChars, space(26)))

* The lcTextVal only contains letters, no digits and wrong characters

endif

You may also check IsAlpha and IsDigit functions in Help. They only check one character in a time.





Re: Validations

Ahsan Amin

ok one more thing i need to ask you is i have a textbox which is niether bound nor it's maxlenght property is set, when it completes entering 30 character it automatically jumps to other controls. what's the reason





Re: Validations

Naomi Nosonovsky

See answers in your second thread. Are you satisfied with the answers for this thread's topic





Re: Validations

FoxCreek

Here is an example of what you could do. I tried using TRANSFORM() and VAL(), using ISALPHA() and ISDIGIT(); however those did not work for all conditions. All I wanted to do was validate that a character string was a number with two decimal places. However, I also wanted to have error checking for errors in data entry. This code was developed for later use in validating data imported from a CSV file. The current condition is only for doing research and testing. It should be easy to integrate it in your program.

Code Block

*------------------------------------------------------------------------------
*- Program..: my_validate
*------------------------------------------------------------------------------
*- Synopsis...: The test program below was written for validating numeric data.
*- The code can easily be altered to validate whatever conditions
*- or criteria desired.
*------------------------------------------------------------------------------
*- Passed:
*- Variables..: None
*- Parameters.: None
*- Return Value: None
*------------------------------------------------------------------------------
lc_test = INPUTBOX("Please enter something to test.")
lc_test = CHRTRAN(lc_test, ", ", "")
*-- to remove commas from numbers in a text field in a table, uncomment below
*!* REPLACE <fieldname> WITH CHRTRAN(fieldname, ",", "") ALL
ll_badNumber = .f.
ln_countTotal = LEN(lc_test)
FOR ln_count = 1 TO ln_countTotal
*-- In the IF statement below, you can specify the criteria for what is not allowed
*-- Everything not part of the "not allowed criteria" is considered correct
IF NOT INLIST(SUBSTR(lc_test, ln_count, 1), ;
"-", ;
"0", ;
"1", ;
"2", ;
"3", ;
"4", ;
"5", ;
"6", ;
"7", ;
"8", ;
"9", ;
".")
ll_badNumber = .t.
EXIT
ENDIF
ENDFOR
IF NOT ll_badNumber
*-- This is to check if the minus sign ("-") occurs only once, and if so,
*-- is it the first character
ln_minus = OCCURS("-", lc_test)
IF ln_minus > 1
ll_badNumber = .t.
ELSE
IF ln_minus = 1 AND LEFT(lc_test,1) <> "-"
ll_badNumber = .t.
ENDIF
ENDIF
IF OCCURS(".", lc_test) > 1
ll_badNumber = .t.
ENDIF
ENDIF
IF ll_badNumber THEN
MESSAGEBOX(lc_test + " is a bad number!")
ELSE && lc_test is a good number, does not contain any of the wrong criteria
MESSAGEBOX(lc_test + " is a good number!")
ENDIF
RETURN






Re: Validations

CetinBasoz

Ahsan,

Code Block

GetExpr to m.test

would show a dialog with functions are grouped into char,math,date,logical. You may use that to see what functions are available roughly. The full list is more than what you'd see there but you can learn others as you go.

For example to validate if a textbox used for firstName has data made up of 2-15 alpabetic characters you could use an expression like:

IsValid = chrtran(upper(thisform.myTextBox.Value), "ABCDEFGHIJKLMNOPQRSTUVWXYZ ", "") == "" and ;

between(len(alltrim(thisform.myTextBox.Value)), 2, 15)

That is a rough one that would work for most EN-US names (but of course there are names that use letters not in English alphabet, that may have apostrophe, dot and even numbers etc in it). A better check would be by using regular expressions. And maybe a much better check would be no check and trust in users that they know how to write names (have you ever got angry with interfaces that get your address and blindly insist that you should be in one of US states).

For numeric entries a simple check is to do a transformation using val() and transform(). Further you don't need to do any check if you simply initialize the value to a numeric. Similarly for dates initialize the value to a date (ie: {}) and VFP does the rest. You may further validate it against valid ranges. However keep in mind over validation do not lead to appreciated but boring interfaces.

Anyway, VFP is full of functions almost for anything.