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

Entering and validating a 2-digit month

Status
Not open for further replies.

AndrewMozley

Programmer
Oct 15, 2005
621
GB
I am revisiting some old code. There is a 2-digit text box which is to specify a month. So ‘03’ would represent March. What is other users' practice for specifying the properties of such a field on the form, and for validating it?

Thanks - Andrew
 
I don't use separate fields for day, month and year, but either a calendar control or a single field for a date, which includes validation simply by data type.

If you'd separate this the month validation is simple, unless you take the day into account, because if it is 31, not all months are valid, even 29 could depend on the year, when it comes to February because of leap days. So why not rely on the date type taking care of this?

PS: In the bigger picture, consider validating the whole form instead of validating each single control. Some checks to make a record valid also require to compare two fields, that can only be checked in a wholesome way anyway, a three part date entry is agood example, as already teased with some special cases, in the end any input could turn out to be right, when finally day, month, and year are entered, while the day, month, or year could result in a wrong date during the input of the date parts, so why waste individual checks? The limits 1-31, 1-12 and maybe even a lower and upper year (spinner control) could guide the use to not get into ridiculous values territory, but the date check in the end is an overall check whether DATE(year,month,day) is a valid date.

Chriss
 
andrew,

What exactly are you asking here? Clearly, you already know how to check that a given number is a valid month number. You simply check that it lies between one and 12. So I assume there is more to your question than that.

Your mention of "03" as an example value suggests that the textbox is of character type. If it was numeric, the leading zero would not exist: the number would simple be 3. If it is a character string, why?

Is the month number just a field on its own? Or is one of several fields that together represent a complete date? If the latter, is there a good reason for not simply using a date or datetime data type?

Finally, you say this is old code. Presumably it is code that has been working up to know. If so, why do you need to change it?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thank you Chris and Mike, for your replies.

To Mike’s point, I am revisiting an old report to make a new version with different options. The control into which I am entering the month is not part of a date : it is just part of specifying a range for the report.

The control used to just have an input mask of ‘99’ and the validation routine did not alter the displayed value.
I am now considering changing the validation method to include :

Code:
WITH Thisform
   This.Value = RIGHT(STR(VAL(This.value) + 100,3),2)
   ENDWITH
RETURN .T.

So that this means that when the user keys in ‘3’ <Enter>, the control shows ‘03’ on the screen. I just wondered if there was a way of doing this by specifying one of the properties of the control.

I just wondered if there was a way of organising this by specifying one of the properties of the textbox. It’s not a big thing!

Thanks again.
 
For padding you have the pad functions, no need for such complicated trickery. Why insist on a 2 digit display anyway?

Even if this is month standalone you'd want to check it's betwenn 01 and 12 only, for that a spinner is the best control.

Chriss
 
when the user keys in ‘3’ <Enter>, the control shows ‘03’ on the screen.

But why do you need to do that? Why not just let them enter "3"? That would work regardless of whether the field is numeric or a character string. You would then just need to check that it is in the range 1 - 12.

Chris suggested using a spinner. Personally, I'm not that keen on spinners. But if you do decide to do that, you can do the validation simply by settings its KeyboardHighValue, KeyboardLowValue, SpinnerHighValue and SpinnerLowValue properties.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
As well as the textbox and spinner, there is a third option: a combo box. You could use a combo box that displays the actual names of the months, and let the user pick the one they require. The ListIndex property would then give you the month number. No validation required.

This might or might not be the best approach. It would depend on your users' preferences. But it's something to consider.

(If you did decide to use a combo box, setting its IncrementalSearch property .T. would be a good move.)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top