TextMode.Phone doesn't do any validation? - c#

On my aspx page I have a textbox, which is placed on a modal window, together with an OK button:
<asp:TextBox runat="server" ID="contactInfo" TextMode="Phone" ToolTip="Please enter your phone number">01234/567890</asp:TextBox>
<asp:Button ID="btnOK" runat="server" Text="Ok" OnClick="btnOk_Click" />
However, the TextMode="Phone" doesn't seem to do any validation at all. Even if I enter absolute nonesense like "Hi, this is a text and definitely not a phone number" the page will not complain. If, for example, I change the TextMode to TextMode="Email", then a message will appear asking me to enter a valid e-mail address and the modal window stays open, no matter how often I try to click the OK button.
Right now I have to validate the phone number on the client side (as changing the TextMode to Number introduces a whole lot of other problems), but I would like to use the "framework solution" just as I am able to with TextMode.Email.
Is the validation check really not implemented for TextMode.Phone? I couldn't find anything helpful regarding this topic.

<asp:TextBox TextMode="Phone"> is a .NET element that gets rendered into <input type="tel"> HTML element and this HTML element does not automatically validate.
From MDN:
<input> elements of type "tel" are used to let the user enter a
telephone number. Unlike <input type="email"> and <input type="url"> ,
the input value is not automatically validated to a particular format
before the form can be submitted — this is because formats for
telephone numbers vary so much around the world.
You can validate using a <asp:RegularExpressionValidator> with your desired regex.
For example:
<asp:RegularExpressionValidator ID="revPhone" runat="server"
ErrorMessage="Not a valid phone" ControlToValidate="contactInfo"
ValidationExpression="^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$">
</asp:RegularExpressionValidator>

Related

Validating Social Security Numbers through Regular Expressions

I want to do like this -
I want to do when I put cursor in the text box it should be empty and how I can show 000-00-0000 format while page load.
I have tried this-
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="TextBox7" ErrorMessage="Social Security No. is wrong."
ValidationExpression="^\d{3}-\d{2}-\d{4}$">
</asp:RegularExpressionValidator>
but its not showing 000-00-0000 when I load the page and when I put cursor in the text box it should disappear the 000-00-0000.
Thanks in advance.
Are you looking for a placeholder? Given an HTML input as
<input type="text" placeholder="000-00-0000" />
you will see an input box with ghosted letters 000-00-0000 which disappear and reappear when the user focuses or blurs the element. To do this in Asp.net, you might try
<asp:textbox placeholder="000-00-0000" runat="server" />
Additionally, if a user does not enter the - characters, could the ValidationExpression be modified to be optional?
ValidationExpression="^\d{3}-?\d{2}-?\d{4}$"
Then either 000000000 and 000-00-0000 would be accepted, which you could deal with server-side when the form is submitted.

use Javascript or C# to validate a webform

Would like to validate more than one control on one button click. I would like something to validate whether a textbox has contents if a checkbox is checked or not but the checkbox doesn't necessarily have to be checked and in that case I don't want to check the textbox. I tried validation group but each button needs to control the different groups and i need this all to be under one button.
I'm open to ideas of how to do this c#,javascript...etc. Heres some code: Button3 is the save which validates whether checkbox 1 is checked and if so textbox10 cant be empty. I have about four other instances of this but are independent of each other.
<asp:Button ID="Button3" runat="server" Height="24px"
Text="Save" Visible="False" Width="67px" Font-Bold="True"
causesvalidation="true"
validationgroup="required"
runat="Server" />
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" Text=" Breach Letter Sent"
ValidationGroup="required" AutoPostBack="True" Enabled="False" />
You want to use the CustomValidator control which can validate both on the server and the client. There is an example in the docs here - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
I would never do form validation in JavaScript. Believe it or not, but some people actually turn off JavaScript! Use validators to validate the field content. Of course this means a round trip to the server in most cases, but you get reliable and well integrated validation.
you can use validation with Ajax (in Ajax postback occures but you will not sense)

Restricting user entering html tag in textbox using validation control in asp.net c#

I have page user creation and it contains textbox control. I want to restrict user entering html tag in i.e < and > sign in textbox using .net validation control.
Can any one help me about in this?
I also want to restrict double quote i.e " and caret sign ^ can you please tell me how to write expression for that???
Use a regularexpressionvalidator...
<asp:textbox id="theTextbox" runat="server" />
<asp:regularexpressionvalidator id="regexValiator" runat="server"
controltovalidate="theTextbox"
errormessage='<, >, ", and ^ not allowed'
display="Dynamic"
validationexpression='([^<>\"\^])*' />
Actually, by default ASP.Net dissallow HTML content to be entered in form fields. No need for further validation.
you can try the following code in your aspx code:
<asp:textbox id="txtBox" runat="server" />
<asp:RegularExpressionValidator controltovalidate="txtBox" ValidationExpression="([a-z]|[A-Z]|[0-9]|[ ]|[-]|[_])" ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"></asp:RegularExpressionValidator>
and now you can modify the regular expression to fit your case.

Handling the submit action of two TextBoxes

I have an ASP.net page.
That has an Ajax Toolkit Tab Control.
That has tabs.
That have custom ascx controls I wrote.
I have a text box that perform a search action. It is declared like this:
<asp:TextBox ID="txtPrereqSearch" runat="server"
ontextchanged="txtPrereqSearch_TextChanged"></asp:TextBox>
Nothing fancy. This format has been working for months. There's no submit button. It just posts back when I hit enter. The problem appeared when I added a second custom control using the same type of feature. Now browsers don't postback when I type something in either of these textboxes and press enter.
It seems that browsers have a default way of handling one textbox in one form, but that behavior changes when the number reaches two.
Is there an easy way around this? I guess I can create a hidden submit button but it seems like there is probably a better way to deal with this when the functionality is in two separate custom controls.
Your feedback is appreciated!
Check this out: http://www.allasp.net/enterkey.aspx
The default behavior with no submit button seems to depend on the browser, and the behavior can indeed depend on the number of input controls.
I would add hidden "submit" button (e.g. style="display:none;") which should ensure that it always gets submitted.
The answer was a little different than I expected, but philosophically like my original idea that #jamietre reinforced.
I had to surround the controls with an <asp:Panel> tag with a DefaultButton attribute. A-like-a so:
<asp:Panel ID="ButtonPanel" runat="server" DefaultButton="btnSubmit">
<asp:Label ID="Label1" runat="server" Text="Course:"></asp:Label>
<asp:TextBox ID="txtPrereqSearch" runat="server"
ontextchanged="txtPrereqSearch_TextChanged"></asp:TextBox>
<asp:TextBoxWatermarkExtender ID="txtPrereq_TextBoxWatermarkExtender"
runat="server" Enabled="True" TargetControlID="txtPrereqSearch"
WatermarkCssClass="Watermark" WatermarkText="e.g., MATH201"></asp:TextBoxWatermarkExtender>
<asp:Button ID="btnSubmit" CssClass="InvisibleSubmit" runat="server" Text="Submit" OnClick="txtPrereqSearch_TextChanged"/>
</asp:Panel>

.NET two "forms" on a page and their validators

I have got a page that has 2 "forms" (What I mean by a form here is a Panel consisting of: textboxes, validators and a button).
(I got 2 here because one of them is actually on the MasterPage, shown all the time)
The problem is when you try to submit to one of the form, it will validate the other form, which of course is blank and invalid.
How do you solve this problem?
Thank you.
Your problem can be solved with asp.net ValidationGroups.
http://weblogs.asp.net/scottgu/archive/2004/10/24/246945.aspx
Basically, you group the controls to be validated using a uniquely named validation group. Like so:
<asp:Textbox ID="txt" runat="server" />
<asp:RequiredFieldValidator id="rfv" runat="server" ControlToValidate="txt" ValidationGroup="masterGroup">* Required!</asp:RequiredFieldValidator>
<br />
<asp:Button id="btnSubmitMaster" runat="server" Text="Submit!" ValidationGroup="masterGroup" />
If you group your inputs like this, then assign the validation group to the control that submits the form, the inputs in the other validation groups won't be validated.

Categories