Reputation: 974
I have an MVVM application where users register a person. To register a person, the user has to supply at least 2 valid telephone numbers, in the three textboxes provided.
A "valid" telephone number consists of at least 10 numbers.
I've implemented IDataErrorInfo, and my textboxes are validating whenever the bound property changes.
My question relates to how to actually validate the content?
This constitutes valid inputs :
Textbox 1 : 0123456789
Textbox 2 : 0123456789
Textbox 3 : {null}
as is
Textbox 1 : 0123456789
Textbox 2 : 0123456789
Textbox 3 : 0123456789
These, however, are not valid :
Textbox 1 : 0123456789
Textbox 2 : 012345678
Textbox 3 : {null}
and
Textbox 1 : 0123456789
Textbox 2 : {null}
Textbox 3 : {null}
So this is actually more of a coding question than a validation question.
I have so far :
private string ValidateTelephoneNumbers()
{
var hasNumber1 = BoundProperty1 != string.Empty;
var hasNumber2 = BoundProperty2 != string.Empty;
var hasNumber3 = BoundProperty3 != string.Empty;
if (hasNumber1 && (hasNumber2 || hasNumber3) || (hasNumber2 && hasNumber3))
{
return null;
}
else
{
return "Specify at least 2 valid telephone numbers."
}
}
which covers the "specify at least two" part of the requirement... but is there a more elegant way of checking the length of each of the supplied numbers, rather than checking each one?
Upvotes: 1
Views: 1383
Reputation: 17063
I would do this:
private string ValidateTelephoneNumbers()
{
int number = 0;
int validNumbers = 0;
if (int.TryParse(BoundProperty1, out number) && number > 999999999)
validNumbers++;
if (int.TryParse(BoundProperty2, out number) && number > 999999999)
validNumbers++;
if (int.TryParse(BoundProperty3, out number) && number > 999999999)
validNumbers++;
if (validNumbers > 1)
{
return null;
}
else
{
return "Specify at least 2 valid telephone numbers.";
}
}
Upvotes: 1