• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

setting up multi-field ID verification

I am trying to set up a duplicate set of text fields that concatenate their values with hyphens as a string variable (id and id2). The second field set is for the user to re-enter the same ID to verify against the first. I need to know how to set this up. Here is the .cs and .aspx code:

protected void startBtn_Click(object sender, EventArgs e)

    {

        //Participant ID, concatenate three text boxes with "-"

        string id = ((TextBox)LoginView1.FindControl("TBid")).Text + "-"

                    + ((TextBox)LoginView1.FindControl("TBid1")).Text + "-"

                    + ((TextBox)LoginView1.FindControl("TBid2")).Text;

 

        //validate ID

        if (id == "--")//no ID entered

        {

            ((Label)LoginView1.FindControl("ErrorLabel1")).Text = "ID required!";

            return;

        }

        else

        {

            //regular expression to format ID

            Regex pattern = new Regex("[0-9]-[0-9][0-9]-[0-9][0-9][0-9]");

            if (!pattern.IsMatch(id)) //ID is not in the format

            {

                ((Label)LoginView1.FindControl("ErrorLabel1")).Text = "Invalid ID: format 9-99-999!";

                return;

            }

 

        //Re-entered participant ID, concatenate three text boxes with "-"

        string id2 = ((TextBox)LoginView1.FindControl("TextBox1")).Text + "-"

                    + ((TextBox)LoginView1.FindControl("TextBox2")).Text + "-"

                    + ((TextBox)LoginView1.FindControl("Textbox3")).Text;

 

 

 

      <tr>

                         <td align="left" class="generaltext" style="width: 479px; height: 58px" bgcolor="#ffefd5">

                             &nbsp;Study ID:<asp:TextBox ID="TBid" runat="server" onkeyup="return autoTab(this, 1, event);"   Style="left: 10px;

                                 position: relative; top: 0px" Width="18px" Height="14px" MaxLength="1" CssClass="watermark"></asp:TextBox>

                             &nbsp; <span style="font-size: 16pt">- &nbsp;</span><asp:TextBox ID="TBID1" runat="server" onkeyup="return autoTab(this, 2, event);" Height="14px" MaxLength="2" Width="24px" CssClass="watermark"></asp:TextBox>

                             <span

                                     style="font-size: 16pt">-&nbsp; </span><asp:TextBox

                                 ID="TBid2" runat="server" onkeyup="return autoTab(this, 3, event);" Height="14px" MaxLength="3" Width="36px" CssClass="watermark"></asp:TextBox>&nbsp;

                             <asp:Label ID="errorLabel1" runat="server" ForeColor="Red" Width="253px"></asp:Label></td>

                     </tr>

                     

                     <tr>

                         <td align="left" class="generaltext" style="width: 479px; height: 58px" bgcolor="#ffefd5">

                             &nbsp;Re-enter Study ID:<asp:TextBox ID="TextBox1" runat="server" onkeyup="return autoTab(this, 1, event);"   Style="left: 10px;

                                 position: relative; top: 0px" Width="18px" Height="14px" MaxLength="1" CssClass="watermark"></asp:TextBox>

                             &nbsp; <span style="font-size: 16pt">- &nbsp;</span><asp:TextBox ID="TextBox2" runat="server" onkeyup="return autoTab(this, 2, event);" Height="14px" MaxLength="2" Width="24px" CssClass="watermark"></asp:TextBox>

                             <span

                                     style="font-size: 16pt">-&nbsp; </span><asp:TextBox

                                 ID="TextBox3" runat="server" onkeyup="return autoTab(this, 3, event);" Height="14px" MaxLength="3" Width="36px" CssClass="watermark"></asp:TextBox>&nbsp;

                             <asp:Label ID="Label4" runat="server" ForeColor="Red" Width="253px"></asp:Label></td>

                     </tr>
0
devabarry
Asked:
devabarry
  • 5
  • 3
1 Solution
 
Pratima PharandeCommented:
You can try out required field validator for first one

and compare filed validator cobtrol for comparing first & second
0
 
devabarryAuthor Commented:
OK, is this what you mean? I'm especially not sure if my CompareValidators are set up correctly. Does anything need to be done to the C# code, or just the ASP?

<tr>
                         <td align="left" class="generaltext" style="width: 479px; height: 58px" bgcolor="#ffefd5">
                             &nbsp;Study ID:<asp:TextBox ID="TBid" runat="server" onkeyup="return autoTab(this, 1, event);"   Style="left: 10px;
                                 position: relative; top: 0px" Width="18px" Height="14px" MaxLength="1" CssClass="watermark"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="TBid"> </asp:RequiredFieldValidator>
                             &nbsp; <span style="font-size: 16pt">- &nbsp;</span><asp:TextBox ID="TBID1" runat="server" onkeyup="return autoTab(this, 2, event);" Height="14px" MaxLength="2" Width="24px" CssClass="watermark"></asp:TextBox>
                             <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TBid1"> </asp:RequiredFieldValidator>
                             <span
                                     style="font-size: 16pt">-&nbsp; </span><asp:TextBox
                                 ID="TBid2" runat="server" onkeyup="return autoTab(this, 3, event);" Height="14px" MaxLength="3" Width="36px" CssClass="watermark"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="TBid2"> </asp:RequiredFieldValidator>&nbsp;
                             <asp:Label ID="errorLabel1" runat="server" ForeColor="Red" Width="253px"></asp:Label></td>                                                      
                     </tr>
                     
                     <tr>
                         <td align="left" class="generaltext" style="width: 479px; height: 58px" bgcolor="#ffefd5">
                             &nbsp;Re-enter Study ID:<asp:TextBox ID="TextBox1" runat="server" onkeyup="return autoTab(this, 1, event);"   Style="left: 10px;
                                 position: relative; top: 0px" Width="18px" Height="14px" MaxLength="1" CssClass="watermark"></asp:TextBox>
                                  <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="Tbid" ErrorMessage="Value does not match above entry."> </asp:CompareValidator>
                             &nbsp; <span style="font-size: 16pt">- &nbsp;</span><asp:TextBox ID="TextBox2" runat="server" onkeyup="return autoTab(this, 2, event);" Height="14px" MaxLength="2" Width="24px" CssClass="watermark"></asp:TextBox>
                              <asp:CompareValidator ID="CompareValidator2" runat="server" ControlToCompare="Tbid1" ErrorMessage="Value does not match above entry."> </asp:CompareValidator>
                             <span
                                     style="font-size: 16pt">-&nbsp; </span><asp:TextBox
                                 ID="TextBox3" runat="server" onkeyup="return autoTab(this, 3, event);" Height="14px" MaxLength="3" Width="36px" CssClass="watermark"></asp:TextBox>&nbsp;
                                  <asp:CompareValidator ID="CompareValidator3" runat="server" ControlToCompare="Tbid2" ErrorMessage="Value does not match above entry."> </asp:CompareValidator>
                             <asp:Label ID="Label4" runat="server" ForeColor="Red" Width="253px"></asp:Label></td>
                           
                     </tr>
0
 
Pratima PharandeCommented:
only aspx page coe required.

the structure is like

<asp:CompareValidator
id="compval"
Display="dynamic"
ControlToValidate="txt1"
ControlToCompare="txt2"
ForeColor="red"
BackColor="yellow"
Type="String"
EnableClientScript="false"
Text="Validation Failed!"
runat="server" />

You have missed ControlToValidate property..........

and only one control is required

refer this link for example
http://www.w3schools.com/aspnet/showasp.asp?filename=demo_comparevalidator
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
devabarryAuthor Commented:
Thanks, that was very helpful. Here is what I think it should then look like; please verify. You mention only one control is required, I assume you mean for each textbox. Since I am validating 3 separate textboxes, 3 controls are included, correct? FYI the textboxes are concatenated with hyphens into a string variable that is sent to the database.

<tr>
                         <td align="left" class="generaltext" style="width: 479px; height: 58px" bgcolor="#ffefd5">
                             &nbsp;Study ID:<asp:TextBox ID="TBid" runat="server" onkeyup="return autoTab(this, 1, event);"   Style="left: 10px;
                                 position: relative; top: 0px" Width="18px" Height="14px" MaxLength="1" CssClass="watermark"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="TBid"> </asp:RequiredFieldValidator>
                             &nbsp; <span style="font-size: 16pt">- &nbsp;</span><asp:TextBox ID="TBID1" runat="server" onkeyup="return autoTab(this, 2, event);" Height="14px" MaxLength="2" Width="24px" CssClass="watermark"></asp:TextBox>
                             <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TBid1"> </asp:RequiredFieldValidator>
                             <span
                                     style="font-size: 16pt">-&nbsp; </span><asp:TextBox
                                 ID="TBid2" runat="server" onkeyup="return autoTab(this, 3, event);" Height="14px" MaxLength="3" Width="36px" CssClass="watermark"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="TBid2"> </asp:RequiredFieldValidator>&nbsp;
                             <asp:Label ID="errorLabel1" runat="server" ForeColor="Red" Width="253px"></asp:Label></td>                                                      
                     </tr>
                     
                     <tr>
                         <td align="left" class="generaltext" style="width: 479px; height: 58px" bgcolor="#ffefd5">
                             &nbsp;Re-enter Study ID:<asp:TextBox ID="TextBox1" runat="server" onkeyup="return autoTab(this, 1, event);"   Style="left: 10px;
                                 position: relative; top: 0px" Width="18px" Height="14px" MaxLength="1" CssClass="watermark"></asp:TextBox>
                                  <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox1" ControlToCompare="Tbid" ErrorMessage="Value does not match above entry."> </asp:CompareValidator>
                             &nbsp; <span style="font-size: 16pt">- &nbsp;</span><asp:TextBox ID="TextBox2" runat="server" onkeyup="return autoTab(this, 2, event);" Height="14px" MaxLength="2" Width="24px" CssClass="watermark"></asp:TextBox>
                               <asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="TextBox2" ControlToCompare="Tbid1" ErrorMessage="Value does not match above entry."> </asp:CompareValidator>
                             <span
                                     style="font-size: 16pt">-&nbsp; </span><asp:TextBox
                                 ID="TextBox3" runat="server" onkeyup="return autoTab(this, 3, event);" Height="14px" MaxLength="3" Width="36px" CssClass="watermark"></asp:TextBox>&nbsp;
                                   <asp:CompareValidator ID="CompareValidator3" runat="server" ControlToValidate="TextBox3" ControlToCompare="Tbid2" ErrorMessage="Value does not match above entry."> </asp:CompareValidator>
                             <asp:Label ID="Label4" runat="server" ForeColor="Red" Width="253px"></asp:Label></td>
                           
                     </tr>
                     
0
 
Pratima PharandeCommented:
You have 3 text boxes ang you want to compare the 2nd and 3rd with the 1st one right ??

then you need Required fild validator with ech text box and Compare fild validator with 2nd & 3rd only.

and for 2nd text box  compare validator
ControlToValidate="txt2"
ControlToCompare="txt1"

and for 3rd text box  compare validator
ControlToValidate="txt3"
ControlToCompare="txt1"

0
 
Pratima PharandeCommented:
<tr>
                         <td align="left" class="generaltext" style="width: 479px; height: 58px" bgcolor="#ffefd5">
                             &nbsp;Study ID:<asp:TextBox ID="TBid" runat="server" onkeyup="return autoTab(this, 1, event);"   Style="left: 10px;
                                 position: relative; top: 0px" Width="18px" Height="14px" MaxLength="1" CssClass="watermark"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="TBid"> </asp:RequiredFieldValidator>
                             &nbsp; <span style="font-size: 16pt">- &nbsp;</span><asp:TextBox ID="TBID1" runat="server" onkeyup="return autoTab(this, 2, event);" Height="14px" MaxLength="2" Width="24px" CssClass="watermark"></asp:TextBox>
                             <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TBid1"> </asp:RequiredFieldValidator>
 <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TBID1" ControlToCompare="TBid" ErrorMessage="Value does not match above entry."> </asp:CompareValidator>
                         


   <span
                                     style="font-size: 16pt">-&nbsp; </span><asp:TextBox
                                 ID="TBid2" runat="server" onkeyup="return autoTab(this, 3, event);" Height="14px" MaxLength="3" Width="36px" CssClass="watermark"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="TBid2"> </asp:RequiredFieldValidator>&nbsp;
                             
 <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TBid2" ControlToCompare="TBid" ErrorMessage="Value does not match above entry."> </asp:CompareValidator>



<asp:Label ID="errorLabel1" runat="server" ForeColor="Red" Width="253px"></asp:Label></td>                                                      
                     </tr>
                     
0
 
devabarryAuthor Commented:
I have 3 textboxes in the first ID entry section: Tbid, Tbid1 and Tbid2. Then I have 3 textboxes in a 'repeat' ID entry section  to re-enter the ID: TextBox1, TextBox2 and TextBox3. What should happen is Tbid = TextBox1, Tbid2 = TextBox2, and Tbid2 = TextBox3. Then I know the user has entered an exact match and the initial ID can go to the database.
0
 
Pratima PharandeCommented:
ok,

Your code is correct
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now