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!

Validation of text box

Status
Not open for further replies.

AlastairP

Technical User
Feb 8, 2011
286
AU
I have a text box which I need to validate for allowed filename characters.

Does anyone have a list of non allowed charactors or some code I can use to validate the text box entry?
 
Now that you have your list of non-valid characters, do you need help constructing the validation?

Personally, I'd do it in Keypress and only accept valid characters in the first place.
 
I'd use GetFile() or PutFile() and avoid the issue altogether.

Tamar
 
A bit of trial and error and google and I came up with this.
I also added removal of the offending charactor and returns the cursor to the original position in the text box
Works well in 'interactivechange'

Credit to the original poster in another forum


Code:
lcFilename = ALLTRIM(this.Value)

For xx = 1 to len(lcFilename)
  lcChar = substr(lcFilename,xx,1)
	
	IF  asc(lcChar)=39 OR asc(lcChar)=42 OR asc(lcChar)=44 ;
		OR asc(lcChar)=47 OR asc(lcChar)=59 OR asc(lcChar)=61;
		OR BETWEEN(asc(lcChar), 91, 93) OR asc(lcChar)=96
    	MESSAGEBOX("INVALID CHARACTER FOUND. ",16,"INVALID")
    	  
    	lcReplace=STRTRAN(lcFilename, lcChar)
    	this.Value=lcReplace
    	this.selstart=xx-1
	ENDIF
ENDFOR

ORIGINAL POST:

Code:
lcFilename = "C:\TESTFILE.EXE"
For xx = 1 to len(justfname(lcFilename))
  lcChar = substr(lcFilename,xx,1)
  if between(asc(lcChar),65,90) or between(asc(lcChar),97,122)

  else
    ?lcChar
    messagebox("INVALID CHARACTER FOUND. ",16,"INVALID")
  endif
  if xx > 12 
    messagebox("INVALID FILENAME LENGTH FOUND. ",16,"INVALID")
  endif
endfor



 
You can forget about this function, if you use Putfile(), for letting the user enter a file name interactively, it will only allow the user to choose or enter a valid file name, no need to check it.

Besides that, you rather would not only check for some invalid chars, like you do, there's more to it actually, as unicode names are allowed and foxpro will not handle this nicely at all.

If you are fine with allowing english alphabet, spaces and numbers and perhaps a few other chars, you can simply check for them via CHRTRAN() in one step, not such a loop and asc(), it's much more elegant:

if a string consists of the chars you want to allow, a chrtran of all allowed chars with the empty string will yield an empty string, thereore a check of allowed chars is as simple as:

#define allowedchars "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789-+~."

llAllowed = (Len(chartran(cFilename,allowedchars,""))=0)

But in spite of the simplicity of that, using PUTFILE() you will get a valid file name with every char the OS allows without any knowledge about the allowed or disallowed chars at all, it's really the easiest method to use Putfile().

Just check if the return value is empty, denoting cancel and check if the filename is existing already via FILE() or ADIR().

Bye, Olaf.
 
I agree with Tamar and Olaf about using GETFILE() and/or PUTFILE(.

That said, there might be times when that's not appropriate. For example, in one of my apps, I create a PDF of a report. The filename is based on the report name, which is supplied by the user. Since the report name can contain any characters, I need to look out characters that would not be allowed in the filename.

This is the function I use. It simply replaces the invalid characters with underscores.

Code:
FUNCTION StripInvalidChars
* Removes all invalid characters from a filename 
* and replaces them with underscores.
LPARAMETERS tcIn

LOCAL lcBadChars
lcBadChars = [<>:"/\|?*]
RETURN CHRTRAN(tcIn, lcBadChars, REPLICATE("_", LEN(lcBadChars)))

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Thanks vey much, this give me plenty of scope to work with.
In this case, the text box is for the heading in a report, which also forms part of the filename.

The report is generated into a PDF with no option for the user to alter the name of the file other than this text box.

The filename contains other information such as quote reference #, and is formatted in a consistant manner for sending on to clients.

 
agreed, Mikes code then will simply avoid unwantd chars and replace them with underlines.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top