|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionBuilding database data entry applications raises many questions. The one I want to discuss here is, "When should one validate user input?" Catching invalid data entry at the earliest possible point - while the user is typing - can have great advantages. Validating after entry such as during the With the purpose of stopping invalid data entry before it happens, let's take a look at the Key ConsiderationsI wanted to be able to easily handle a variety of different .NET basic numeric data types such as various size floating point and integer types as well as Currency (internally treated as First, digit-only filtering doesn't allow the decimal point or the negation "-" character. With a few changes we can allow negative numbers and a the decimal point, but the code rapidly becomes messy dealing with duplicate decimals and negative characters only in the first position. Second, and possibly more important, is the numeric value entered. If we are working with .NET 32 bit integer values for example, then we need to stop further entry once the maximum value of an integer is typed. Otherwise we will be dealing with overflow errors. For this reason, I chose to use the Last is pasting. Using Using the ControlThe beauty of this control is that you never need to think about numeric data entry again. Simply add the control, set the The private bool IsValid(string val)
{
// this method validates the ENTIRE string
// not each character
// Rev 1: Added bool user param. This bypasses preliminary checks
// that allow -, this is used by the OnLeave event
// to prevent
bool ret = true;
if(val.Equals("")
|| val.Equals(String.Empty))
return ret;
if(user)
{
// allow first char == '-'
if(val.Equals("-"))
return ret;
}
// if(Min < 0 && val.Equals("-"))
// return ret;
// parse into dataType, errors indicate invalid value
// NOTE: parsing also validates data type min/max
try
{
switch(m_inpType)
{
case NumEditType.Currency:
decimal dec = decimal.Parse(val);
int pos = val.IndexOf(".");
if(pos != -1)// 2 decimals + "."
ret = val.Substring(pos).Length <= 3;
//ret &= Min <= (double)dec && (double)dec <= Max;
break;
case NumEditType.Single:
float flt = float.Parse(val);
//ret &= Min <= flt && flt <= Max;
break;
case NumEditType.Double:
double dbl = double.Parse(val);
//ret &= Min <= dbl && dbl <= Max;
break;
case NumEditType.Decimal:
decimal dec2 = decimal.Parse(val);
//ret &= Min <= (double)dec2 && (double)dec2 <= Max;
break;
case NumEditType.SmallInteger:
short s = short.Parse(val);
//ret &= Min <= s && s <= Max;
break;
case NumEditType.Integer:
int i = int.Parse(val);
//ret &= Min <= i && i <= Max;
break;
case NumEditType.LargeInteger:
long l = long.Parse(val);
//ret &= Min <= l && l <= Max;
break;
default:
throw new ApplicationException();
}
}
catch
{
ret = false;
}
return ret;
}
Points of InterestAfter adding the Ctrl+V pasting code I discovered a default EnhancementsAfter completing the
HistoryRelease 2/17/2003 Rev 1 4/4/2003
|
||||||||||||||||||||||