Multiple TextModes in ASP.NET - c#

I have a form of three text boxes having all three inputs only be numbers:
<asp:TextBox ID="TextBox1" runat="server" TextMode="Phone" />
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" />
<asp:TextBox ID="TextBox3" runat="server" TextMode="Password" />
I was wondering if there is a possibility of having two (or more) TextMode for an <asp:TextBox />.
For example:
<asp:TextBox ID="TextBox2" runat="server" TextMode="Phone Password" />

This isn't possible.
ASP.NET TextBox's render as HTML input elements. The TextMode property is used to determine the type attribute.
The input's type attribute cannot contain a mixture of values, and therefore neither can the ASP.NET TextBox.

TextMode attribute accepted only three name
SingleLine - one line textbox
Multiline - Textbox with multiple lines
Password - One line textbox that masks the input
you can use .net Validation Control or jquery validation :)

Related

HTML Required field not working as needed

In am using htm required="required" validation but it is not working properly as needed.
I have 2 button on single page and I want that on 1 button click it validate for textbox1 and on click of 2nd button it will validate for textbox2.
Please review attached image for more detail
You are looking for ValidationGroup. By giving a collection of Validators and a Button the same id, the corresponding button will only validate those fields.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="group1"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox2" ValidationGroup="group1"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button Group 1" ValidationGroup="group1" />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox3" ValidationGroup="group2"></asp:RequiredFieldValidator>
<asp:Button ID="Button2" runat="server" Text="Button Group 2" ValidationGroup="group2" />
I don't think HTML does that natively (grouped validation).
If server-side is not an option, then you'll need to use some JavaScript.
I have found some inspiration in this answer for a related question (Triggering HTML5 Form Validation) : https://stackoverflow.com/a/39689115/370786
If you group each set of inputs that you want to validate together in a form (so you will have several forms), then you can trigger validation through Javascript. Then you can stop the form from posting. This can be done through Javascript - maybe it can be done in HTML but I'm not sure.
Then once everything has validated you will need to post the data to the server, using some Javascript. It might be as simple as just triggering the submit() method on a form element.

how to put validation for textbox with asp.net

I have a Textbox on my webform, for instance 2012 entered in single text box the following text box must enter 2014 or else should give error.Iam trying to validate it by using compare validation but am unable to meet the exact condition what i want.Can i know how can it be done?thnks in advance
I cant fine wha'ts the hard part here...
int number=Convert.ToInt32( textBox1.Text);
if(number==2014)
Response.Write("good");
else
Response.Write("Bad number");
More easy then that?
If your validator should ensure that the text in two TextBoxes is equal use a CompareValidator with appropriate ControlToValidate and ControlToCompare:
<asp:TextBox id="Txt1" runat="server">
</asp:TextBox>
<asp:TextBox id="Txt2" runat="server">
</asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="Txt2"
ControlToCompare="Txt1"
ErrorMessage="Text in second textbox must be equal to text in first textbox!">
</asp:CompareValidator>
If you also want to ensure that only integers can be inserted, use DataTypeCheck and Integer:
<asp:CompareValidator ID="CompareValidator2" runat="server"
ControlToValidate="Txt2"
Type="Integer" Operator="DataTypeCheck"
ErrorMessage="Text in second textbox must be an integer!">
</asp:CompareValidator>

unable to set value from server-side to a Textbox in client side

My code is like this
<asp:TextBox type="text" name="txtEndDate" Text="<%#library.GetDictionaryItem("ProfilePages_CVR nummer")%>" runat="server"></asp:TextBox>
Everything looks okay to me, But i don't know why its throwing an error
The server tag is not well formed.
Use single quotes like:
<asp:TextBox type="text" name="txtEndDate" Text='<%#library.GetDictionaryItem("ProfilePages_CVR nummer")%>' runat="server"></asp:TextBox>
The issue is that because of the ASP.net brackets which contain double quotes inside them
<asp:TextBox Text="<%# someMethod("someValue") %>" />
inside the Text field, you need to use single quotes instead of double quotes on that property, like this:
<asp:TextBox type="text" name="txtEndDate"
Text='<%# library.GetDictionaryItem("ProfilePages_CVR nummer")%>'
runat="server">
</asp:TextBox>
And it will work.
Note also that you are using the DataBinding notation ( <%# ) which will only work if your TextBox is inside a DataBound control, or you call DataBind on the control or page containing this TextBox.

asp.net range validator on textbox

I have an asp:textbox with both required and range validators attached to it, where the code looks like this:
ASP:
<asp:TextBox ID="textBox1" runat="server" CausesValidation="true"></asp:TextBox>
<asp:RangeValidator ID="rangeValidator1" runat="server" ControlToValidate="textBox1" MaximumValue="1" MinimumValue="0"
ValidationGroup="valid" ForeColor="Red" ErrorMessage="Out of Range" />
<asp:RequiredFieldValidator ID="requiredValidator1" runat="server" ControlToValidate="textBox1"
ValidationGroup="valid" ForeColor="Red" ErrorMessage="Cannot be blank" />
And when the page is dynamically loaded (after a quick callback), I have code that is supposed to change the MaximumValue of the RangeValidator to more specific value. Here is the code for that:
rangeValidator1.MaximumValue = GetMaxValue(params).ToString();
Now, I have set a breakpoint, and rangeValidator1.MaximumValue is being set correctly, however, when the page loads, and I look at the compiled client side javascript, it appears that the maximum value is still only 1.
What confuses me more is that any integer typed in will pass, as long as the first digit is a '1'. So if the maxValue is supposed to be something like "1234567", "1" will match, as will "12345678910". But "2" will not. Nor will "3000" or "46000".
Has anyone else had a similar issue with RangeValidators on Textboxes?
The RangeValidator handles validation for multiple types. You should make sure to set the Type to Integer. or what ever is appropriate.

date and time control format

I have used Ajax calender extender to display date in Date_Box and then numeric up down extenders to chose time...
Here is my code:
<td bgcolor="#969ECD">
<asp:TextBox ID="Date_Box" runat="server" Width="85px"></asp:TextBox>
<br />
<div style="height: 4px"></div>
<asp:TextBox ID="txtHour" runat="server" ></asp:TextBox>
<ajaxToolkit:NumericUpDownExtender ID="txtHour_NumericUpDownExtender"
runat="server" Enabled="True" Maximum="12" Minimum="1"
TargetControlID="txtHour" Width="70" >
</ajaxToolkit:NumericUpDownExtender>
<asp:TextBox ID="txtMinute" runat="server"></asp:TextBox>
<ajaxToolkit:NumericUpDownExtender ID="txtMinute_NumericUpDownExtender"
runat="server" Enabled="True" Maximum="59" Minimum="1"
TargetControlID="txtMinute" Width="70" >
</ajaxToolkit:NumericUpDownExtender>
<asp:TextBox ID="txtDayPart" runat="server"></asp:TextBox>
<ajaxToolkit:NumericUpDownExtender ID="txtDayPart_NumericUpDownExtender"
runat="server" Enabled="True" RefValues="AM;PM" TargetControlID="txtDayPart" Width="70">
</ajaxToolkit:NumericUpDownExtender>
<asp:Button ID="Update" runat="server" Text="Update"
onclick="Update_Click1" />
<br />
</td>
</tr>
</table>
</div>
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server"
TargetControlID="Date_Box" PopupPosition="TopRight" >
</ajaxToolkit:CalendarExtender>
Now functionality wise the only problem is when i write something in the textbox other than the normal format and click the update button it lets me accept the values instead it should not let it write anything else other than the date format.. eg no alphabets in the textbox..
This i can still figure out, using some exception message...
but now the main problen is the look of the control. The numeric extender buttons are too big and the line format is not good, also there is lots of space between the three time textboxes... is there a way to make this look neat>>>
How can i solve the problem... any suggestions???!
here is the image link http://www.freeimagehosting.net/image.php?05945773e0.jpg
alt text
The easiest thing to do is to assign your numeric extenders a CssClass property, and use css to fix the layout issues.
You can use asp:RegularExpressionValidator to validate textbox contents:
http://msdn.microsoft.com/en-us/library/eahwtc9e.aspx
The documentation for NumericUpDown specifies you can use custom images for the updown buttons:
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/NumericUpDown/NumericUpDown.aspx
You may want to try the FilteredTextbox from the ajax control kit found here.
It will prevent typing into the box if they do not meet certain criteria.
Hope this helps
Tom

Categories

Resources