We help IT Professionals succeed at work.

Help with Formating Text Box

Hello,

I am using the code below to force users to enter text in a TextBox in the following format: xx,xxx.

1. How do I modify the code, to alert the users as soon as they don't meet the format during a key press event or
when they leave the control and try to populate other fileds?

2. How do I Mask my TextBox as Follows A_B_C_D_    where _ is where they can only enter numbers? and also trigger an alert if the format is not followed?

I will need to Reterive the entire text, For example A1B2C3D4, not just the numbers they enter.

 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
            Display="Dynamic" ErrorMessage="Required"></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
            ErrorMessage="Invalid Format (xx,xxx)" ValidationExpression="\d{2},\d{3}" ControlToValidate="TextBox1"
            Display="Dynamic"></asp:RegularExpressionValidator>


Thanks,

Victor
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014

Commented:

Author

Commented:
Hello,

I tried the following but I'm getting a compilation error message, can you please help me with the proper javascript format.

Code in Header tag:
<script type="text/javascript" id="Validate" runat =server>
function validate() {
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
            Display="Dynamic" ErrorMessage="Required"></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
            ErrorMessage="Invalid Format (xx,xxx)" ValidationExpression="\d{2},\d{3}" ControlToValidate="TextBox1"
            Display="Dynamic"></asp:RegularExpressionValidator>
};
</Script>


Code in Form tag:
 <asp:TextBox ID="TextBox1" runat="server" onblur = "Validate()"></asp:TextBox>



Most Valuable Expert 2012
Top Expert 2014

Commented:
You have put asp.net validators in javascript. I thought you want to do custom validation

Author

Commented:
What should  I include in the Javascript section to enforce the format that I want using onblur?
Most Valuable Expert 2012
Top Expert 2014

Commented:
You can use some string manipulation or regular expressions etc.

http://www.javascriptkit.com/javatutors/re.shtml

Author

Commented:
Hi,

Thaks for the link, unfortunately it didn't help sokve the issue. I have two primary goals,

1, How do I  force the users to Enter "A" (0-9) B (0-9)(0-9)C (0-9) G(0-9) For example: A1B36C8D8

2. How do I force the users to Enter  ( 4 Didits) (Dash) (Three digits)(Dash)(4Digts) For example:
1206-00-027-3767

Victor
Most Valuable Expert 2012
Top Expert 2014

Commented:
I think your best bet is to use a Masked Textbox.

Author

Commented:
I don't see it in my ToolBox, does it come with VS2010 professional version?
Most Valuable Expert 2012
Top Expert 2014

Commented:

Author

Commented:
i AM TRYING TO INSTALL THE ajax EXTENSION BUT SO FAR NO LUCK, ANY HELP FROM YOU WOULD BE GREATLY APPRECIATED.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Download the correct version for your .NET framework, install it, then reference the DLLs. Which step are you stuck at?

Author

Commented:
I installed the Ajax extension library, but I don't see it in my toolbox or can't see in the section where I can choos the controls.

Author

Commented:
Hi again,

Does the Masked edit box have a datasource property? I need to bound it to a data source to view the data in my xml files.
Most Valuable Expert 2012
Top Expert 2014

Commented:
The maskededitbox itself wont have a datasource. Datasource will be set on the container (repeater, grid etc) level and then you can add databindings on the textbox level.

Author

Commented:
I can set the datasource to a Grid, can you please send me an example on how to use databindings on the textbox level. I will load my combobox with multiple records but will remove thre trigger for users no to see all the data at once, they will need to click the Nex/Previous buttons to move through the records, I wanted to use a combobox, but I can't format it to force users to enter data in a certain format.

Author

Commented:
I can set the datasource to a Grid, can you please send me an example on how to use databindings on the textbox level. I will load my combobox with multiple records but will remove thre trigger for users no to see all the data at once, they will need to click the Nex/Previous buttons to move through the records, I wanted to use a combobox, but I can't format it to force users to enter data in a certain format.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Use the Text property of the maskedtextbox.

Author

Commented:
Hi,

I used the following code, but nothing appears in the textbox:

 Repeater1.DataSource = dtDonor
  ' Repeater1.DataMember = "Countryorigin"
 C1Manufacturer.Text = Repeater1.DataSource

How do you properly connect the Textbox control with the repeater datasource?

Author

Commented:
Correction with code:
 Repeater1.DataSource = dtDonor
Repeater1.DataMember = "Countryorigin"
Repeater1.Databind()
C1Manufacturer.Text = Repeater1.DataSource
Most Valuable Expert 2012
Top Expert 2014

Commented:

Author

Commented:
Hi,

I tried to follow the example by using the code below, but the C1manufacturer Textbox control still doesn't display the data. Can you please help me come up with the proper code.

ASPX File:

 <asp:Repeater ID="RepeatControl" runat="server">
                    <ItemTemplate>
                    DataBinder.Eval(Container.DataItem,"CountryOrigin_ID")
                    C1manufacturer.Text="<%#DataBinder.Eval(Container.DataItem, "CountryOrigin")%>"  
                    </ItemTemplate>
                    </asp:Repeater>

Vb File Search Button:

 RepeatControl.DataSource = dtDonor
        RepeatControl.DataBind()
Most Valuable Expert 2012
Top Expert 2014

Commented:
My goodness. Try

 <asp:Repeater ID="RepeatControl" runat="server">
                    <ItemTemplate>
                    <asp:Textbox id="C1manufacturer" Text="<%#DataBinder.Eval(Container.DataItem, "CountryOrigin")%>"  />
                    </ItemTemplate>
                    </asp:Repeater>

Author

Commented:
I tried your code but still no data in my Textbox control, below is my code and also in part B, the code for dsiplaying the data in a combobox.

<asp:Repeater ID="RepeatControl" runat="server">
                    <ItemTemplate>
                    <asp:Textbox id="C1manufacturer" Text="<%#DataBinder.Eval(Container.DataItem, "CountryOrigin")%>"/>
                    </ItemTemplate>
                    </asp:Repeater>

Button Click event:

RepeatControl.DataSource = dtDonor
        RepeatControl.DataBind()
*****************************888
Part B:
        C1FuzeA.DataSource = dtDonor
        C1FuzeA.DataTextField = "CountryOrigin"
        C1FuzeA.DataValueField = "CountryOrigin_ID"
        C1FuzeA.DataBind()
Most Valuable Expert 2012
Top Expert 2014

Commented:
You need to set the datasource of the repeater similar to what you did for drop down.

Author

Commented:
Not sure what you mean, The Repeater Control.DataSource is set to DtDonor , similar to the Combobox, but it doesn't have a datatextField or dataValuefield property.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Oh yeah I missed that. I am at a loss now. Do you have teamviewer?

Author

Commented:
I don't have teamviewer.
For your req you need to write a script and assign the same to custom validator.

function fncvalidate(Src,Args)
{
  var strText = document.getElementById('yourtextboxid'
 
  if(strText.length == 8 && str.substring(0,1) == 'A' && parseInt(str.substring(1,2)) > 0 && str.substring(2,3) == 'B' && parseInt(str.substring(3,4)) > 0 && str.substring(4,5) == 'C' && 
parseInt(str.substring(5,6)) > 0 && str.substring(6,7) == 'D' && parseInt(str.substring(7,8)) > 0)
{
 Args.IsValid = true;
}
else
{
Args.IsValid = false;
}

}

assign this function as clientvalidatefunction to the customvalidator which will validate the textbox value.

Author

Commented:
Thank you , will test your code and get backup you.

Author

Commented:
Thanks You!