views:

712

answers:

1

I am creating an address control which would use some address validation APIs( google/bing etc) to validate the user entered address. I would also like to validate the fields in the client side (like required fields, Zip format etc) with asp.net validation controls. I am trying to build this with CSS and am using float to align the text boxes (or is there a better way ?) The validation controls have dynamic display and want to place them next to the text boxes when they are not validated. Right now it wraps around/messes up with the placement of other controls. May be there is a better way of doing it ? Here is my code

<style type="text/css">
 .Wrapper{
   width:auto;
   height:auto;
 } 

 .address{
   width:500px;
   height:auto;
 }

.validator{

  }
  .textbox  {
   float:right; 

  }

  label{
    float:left;
  }

  .button-right{
    float:right;
    margin-right:290px;
  }

  div{
    margin:5px;
  }

  .field
  {
    width:200px;
   }
</style>


<div id="addressUI" class="address">
      <div class="field" > 
        <label ID="lblStreet"  for="txtStreet">Street: </label><label style="color:Red">*</label>
        <asp:TextBox ID="txtStreet"  runat="server"  CssClass="textbox" Text=""  ></asp:TextBox> 
        <asp:RequiredFieldValidator ID="rqdStreet" runat="server" ErrorMessage="Street is required" ControlToValidate="txtStreet" Display="Dynamic">
        </asp:RequiredFieldValidator>
      </div>
      <div class="field">
        <label ID="lblCity"  for="txtCity">City: </label><label style="color:Red">*</label>
        <asp:TextBox ID="txtCity"  runat="server"  Text="" CssClass="textbox"  >    </asp:TextBox> 
        <asp:RequiredFieldValidator ID="rqdCity" runat="server" ErrorMessage="City is required" ControlToValidate="txtCity" Display="Dynamic">
        </asp:RequiredFieldValidator>
      </div>
      <div class="field">
        <label ID="lblState" for="ddlState" >State: </label><label style="color:Red">*</label>
        <asp:DropDownList ID="ddlState" runat="server" style="width:80px;margin-left:12px;" CssClass="">
        </asp:DropDownList>  
        <asp:RequiredFieldValidator ID="rqdState" runat="server" ControlToValidate="ddlState"  ErrorMessage="State is required" Display="Dynamic">
        </asp:RequiredFieldValidator>
      </div>

      <div class="field">
      <label ID="lblZip" for="txtZip">Zip: </label><label style="color:Red">*</label>
      <asp:TextBox ID="txtZip"  runat="server" CssClass="" Text="" style="width:50px;margin-left:22px;" ></asp:TextBox> 
      <asp:RequiredFieldValidator ID="rqdZIP" ControlToValidate="txtZip" runat="server" ErrorMessage="ZIP is required" Display="Dynamic">
      </asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="regexValidator" runat="server"  
          ErrorMessage="Zip is invalid" Display="Dynamic" ControlToValidate="txtZip" 
          ValidationExpression="\d{5}(-\d{4})?"></asp:RegularExpressionValidator>
     </div>

      <div class="button-right">
        <asp:Button ID="btnValidate" runat="server" Text="validate" OnClick="btnValidate_Click" CausesValidation="true" />
      </div>
  </div>
+1  A: 
<head>
    <style type="text/css"> 
    label 
    {
     display:inline;
     float:left;
     width:75px;
    } 
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="addressUI" class="address">
         <div class="field">
          <label id="lblStreet" for="txtStreet">
           Street: <span style="color: Red">*</span> 
          </label>
          <asp:TextBox ID="txtStreet" runat="server" CssClass="textbox" Text=""></asp:TextBox>
          <asp:RequiredFieldValidator ID="rqdStreet" runat="server" ControlToValidate="txtStreet"
           Display="Dynamic" ErrorMessage="Street is required">
          </asp:RequiredFieldValidator>
         </div>
         <div class="field">
          <label id="lblCity" for="txtCity">
           City:<span style="color: Red">*</span>
          </label>
          <asp:TextBox ID="txtCity" runat="server" CssClass="textbox" Text="">    </asp:TextBox>
          <asp:RequiredFieldValidator ID="rqdCity" runat="server" ControlToValidate="txtCity"
           Display="Dynamic" ErrorMessage="City is required">
          </asp:RequiredFieldValidator>
         </div>
         <div class="field">
          <label id="lblState" for="ddlState">
           State:<span style="color: Red">*</span>
          </label>       
          <asp:DropDownList ID="ddlState" runat="server" CssClass="" Style="width: 80px">
          </asp:DropDownList>
          <asp:RequiredFieldValidator ID="rqdState" runat="server" ControlToValidate="ddlState"
           Display="Dynamic" ErrorMessage="State is required">
          </asp:RequiredFieldValidator>
         </div>
         <div class="field">
          <label id="lblZip" for="txtZip">
           Zip:<span style="color: Red">*</span>
          </label>       
          <asp:TextBox ID="txtZip" runat="server" CssClass="" Style="width: 50px"
           Text=""></asp:TextBox>
          <asp:RequiredFieldValidator ID="rqdZIP" runat="server" ControlToValidate="txtZip"
           Display="Dynamic" ErrorMessage="ZIP is required">
          </asp:RequiredFieldValidator>
          <asp:RegularExpressionValidator ID="regexValidator" runat="server" ControlToValidate="txtZip"
           Display="static" ErrorMessage="Zip is invalid" ValidationExpression="\d{5}(-\d{4})?"></asp:RegularExpressionValidator>
         </div>
         <div class="button-right">
          <asp:Button ID="btnValidate" runat="server" CausesValidation="true"
           Text="validate" />
         </div>
        </div>
        </div>
    </form>
</body>
</html>
Phaedrus
Superb !! Always new things to learn :)
ram
Actually I dont think we need Display:inline, setting a width to label will do the trick
ram
Your right, the display inline is unnecessary. You do, however, need the float:left for it to work in FireFox.
Phaedrus
yup, float left + divs around each field (label for city, city text box + the validators) are needed
ram