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

restrict Alphabets input in text box

Status
Not open for further replies.

mstrcmtr

Programmer
Nov 14, 2007
103
PK
How can restrict only Alphabets input in text box or Numeric input in a text box
 
This is about as easy as it gets. You just need to use the textbox's InputMask property. A setting of A restricts entry to alphabetic characters; 9 restricts it to digits and plus/minus. For other settings, see the Help file.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Just to be clear, the input mask is applied on a character-by-character basis.

So, setting the mast to AAA will allow three alphabetic characters. Setting it to A999 will allow one alpha followed by three numerics. NXXX permits an alpha or numeric followed by any three further charactes. And so on.

You should also look at the Format property, which has further options.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
some days before i have seen in this forum of example using of ChrTrans function for this problem . any one help finding that solution
 
The other way to predetermine the input type is setting an initial value. If you set a textbox value to 0 at design time, a user can't change the value to a text. Set it to a date, and it remains a date. So very many such restrictions work even without any inputmask. And it's even more restrictive or precise, if you use controlsource to bind the control to a tanle field. Table fields are strongly typed by definition.

There also are specific controls for numbers, eg spinner.

Inputmasks are specifically helpful about conditions Mike showed within a string value otherwise allowing all cahracters.

Then you can also act on any single input with InteractiveChange() event. For example let's tame the behavior of maxlength. First of all, if you set a textbox.maxlength=3 you can only enter three characters, that's already another tip. The textbox will automatically tab to the next control if you rreach maxlength. That may not be what you want. You can SET CONFIRM ON to stay in the textbox, but next keydown will just change the last letter and you get an annoying ping sound.

Let's do that better with InteractiveChange:
Code:
#Define maxlen 3

IF LEN(ALLTRIM(this.value))>maxlen
   this.Value = LEFT(ALLTRIM(this.Value),maxlen)
   this.SelStart=maxlen-1
ENDIF
Now the same behavior without the pinging.

Or this one:
Code:
#Define maxlen 3

IF LEN(ALLTRIM(this.value))>maxlen
   this.Value = RIGHT(ALLTRIM(this.Value),maxlen)
   this.SelStart=maxlen
ENDIF
Now what you type shifts previous value out.

You can do pretty much anything you want, filter out unwanted input, change color to red to visually warn the user etc. etc.

Bye, Olaf.

 
i have seen in this forum of example using of ChrTrans function for this problem

No. CHRTRAN() doesn't have anything to do with restricting input to particular types of data. Rather, it changes individual characters in a string to other characters. For example, you could use it convert all occurrences of the letter A to the digit 9, or to change all dollar signs to yen signs.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Well, the target of CHRTRAN() can also be no target, so you can remove unwanted characters.
here are still better solutions for many specific cases like only allowing numbers.

mstrcmtr, I can't help you find a specific thread about CHRTRAN out of 253 threads containing that keyword. Look into your browser history. CTRL+H.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top