Click here to Skip to main content
15,909,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two text boxes, Address1 and POBox .It is optional for the users to fill in both, but is mandatory that the user fills in at least one of them.

My code for that is:
C#
private static bool Validation_Address1 ( CmpAddress target, 
                                          RuleArgs   e)
    {

    if ( target.IsDirty )
        {
        if ( target.Address1.IsNullOrEmpty ( ) && 
             target.PoBox.IsNullOrEmpty ( ) )
            {
            e.Description = "Must specify a P.O. Box or Street Address.";
            return ( false );
            }
        }
    return ( true );
    }

But this code is not working. It is still throwing the error message even if the pobox is filled but address1 not filled. Please help
Posted
Updated 7-Aug-14 13:35pm
v3
Comments
gggustafson 7-Aug-14 19:37pm    
Sure it shouldnt be String.IsNullOrEmpty ( target.Address1 ) and String.IsNullOrEmpty ( target.PoBox ) ?
Sergey Alexandrovich Kryukov 7-Aug-14 19:59pm    
There is much more elegant check which I wanted to share. Please see my answer. :-)
—SA

1 solution

Taking into account that the value of TextBox.Text can be empty but cannot be null, you can write a very simple condition:
C#
bool isValid = Address1.Text + POBox.Text != string.Empty;

Perhaps you also need to treat a text which contains only blank space characters also as a control with missing data. In this case (which is really reasonable in most practical situations), the check would be a bit more complex:
C#
bool isValid = Address1.Text.Trim() + POBox.Text.Trim() != string.Empty;

Good luck.
—SA
 
Share this answer
 
v2
Comments
gggustafson 7-Aug-14 20:06pm    
cute +5
Sergey Alexandrovich Kryukov 8-Aug-14 0:39am    
Thank you very much.
—SA
[no name] 8-Aug-14 0:25am    
My vote of 5, Simple and humble :)
Sergey Alexandrovich Kryukov 8-Aug-14 0:40am    
Thank you very much, Sibeesh.
—SA
[no name] 8-Aug-14 0:43am    
mine 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900