Limit Input Type in a Textbox - asp.net

I'm new to all this and trying to only allow numeric values to be entered into an existing TextBox in ASP.Net. I've got the code for this, but can't figure out where to put it in. If I do it like the below, it creates an additional box in the textbox (that works). How can I apply this limitation to the current textbox?
<asp:TableCell>
<asp:TextBox ID="txtS2" runat="server" Width="95%" AutoPostBack="true" OnTextChanged="WerklikChanged"></asp:TextBox>
<input type="text" onkeypress="filterDigits(event)"/>
</asp:TableCell>
Update:
I ended up using the below, which is not really what I wanted as it still only checks the field once I leave the field. I'll look into blocking the entry of letters completely at a later stage and post the answer here.
<asp:CompareValidator
ID="ProgrammaticID"
ControlToValidate="txtS2KoringA"
Type="Double"
Operator="DataTypeCheck"
ErrorMessage="Error Message"
Text=" Net nommers word toegelaat in die 'Werklik' velde"
ForeColor="Red"
BackColor="White"
SetFocusOnError="false"
Display="Dynamic"
runat="server">
</asp:CompareValidator>

Related

validating input to the asp:CreateUserWizard control

I'm using the asp:CreateUserWizard control and have read on MSDN:
User input in a Web page can potentially contain malicious client
script. By default, ASP.NET Web pages validate user input to ensure
that the input does not contain HTML elements or script. As long as
this validation is enabled
This is the code I'm using:
<asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
<asp:TextBox runat="server" ID="UserName" CausesValidation="true"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="UserName"
CssClass="field-validation-error" ErrorMessage="User name is required." />
<asp:RegularExpressionValidator runat="server" ControlToValidate="UserName"
CssClass="field-validation-error" ErrorMessage="User name must not contain \ (backslashes)."
ValidationExpression="[^\\]*" />
Now if I try and create a user with a name like <script>alert('hello')</script> my app then crashes with a 500 internal error.
I've tried code in all these events to see if I can trap the error but the code never gets hit:
RegisterUser_CreateUserError()
RegisterUser_CreatingUser()
So my 2 questions are, why is the input not being validated and where could I trap this error when it is thrown (the 2nd question maybe less important once I know how to do the first part)?
I must be missing something really simple here.

asp.net validators ends up in same place

I'm writing an application with multiple validators (date, required, regexp for format) on the same page. They validate correctly, but they turn up in the same place. That is, they don't turn up next to the validated field, they are appended to another validation error message next to a completely other field.
Here goes example code snippets:
Number:
<asp:RegularExpressionValidator class="IMSIValidators" runat="server" ErrorMessage="Enter digits only, no other characters" ControlToValidate="IMSIRangeTextBox" Display="Dynamic" ValidationGroup="AllValidators" ValidationExpression="[0-9]+"></asp:RegularExpressionValidator>
<p class="IMSIEditLabels">Operator: <asp:TextBox ID="OperatorTextBox" runat="server"></asp:TextBox></p>
<asp:RequiredFieldValidator runat="server" class="IMSIValidators" Text="Enter the customer" ControlToValidate="OperatorTextBox" Display="Dynamic" ValidationGroup="AllValidators"></asp:RequiredFieldValidator>
<p class="IMSIEditLabels">Requested by:<asp:TextBox ID="RequestedTextBox" class="IMSIEditInputItems" runat="server" ValidationGroup="AllValidators"></asp:TextBox></p>
<asp:RequiredFieldValidator runat="server" Text="Enter a full name" ControlToValidate="RequestedTextBox" Display="Dynamic"></asp:RequiredFieldValidator>
I've tried all ways that I can think of, such as setting Display="Dynamic" and ValidationGroup="AllValidators". No combination works for me.
I've tried to set their position in a CSS class, but then they came up on top of each other (which is logical).
Any input is greatly appreciated. I'm getting SO frustrated :)
//Eva-Lotta

ASP.NET Multiple Validation Controls on a single asp.net input control?

I have the following textbox alongwith its validators, but when I enter the correct text it fires up the RegularExpressionValidator error message, can't really figure out what I am doing wrong
<asp:TextBox ID="txtName" runat="server" onkeypress="return keyRestrict(event,'abcdefghijklmnopqrstuvwxyz-0123456789')"
AutoCompleteType="Disabled" Width="199px"></asp:TextBox><span style="color: Red">*</span>
<asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtName"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revName" runat="server" ControlToValidate="txtName"
CssClass="text-error" Display="Dynamic" ValidationExpression="^[\p{L}\s]+$" ErrorMessage="Invalid Name"></asp:RegularExpressionValidator>
I also used the ValidationExpression ^[A-Za-z]+$ in order to test that whether there exist some error in the ValidationExpression, and then typed in Name in the textbox being validated and still it triggered the error message of the RegularExpressionValidator.
Regards
^[\p{L}\s]+$ would match 1 or more letters only(not digits) or space..
If you want to validate it with digits you have to use
^[\p{L}\p{N}\s]+$
This would match unicode letters,numbers and space 1 to many times

Possible to get the CompareValidator to accept numbers with commas?

I had been doing a type check for Double for an input field on a web page but now I need to allow commas. Can this be done using a CompareValidator or do I need to use a regex validator?
Rather than using Type="Double", try using Type="Currency". It should accept values with and without commas, however it will not accept more than 2 decimal places.
Here's an example:
<asp:TextBox runat="server" ID="TextBox1" />
<asp:CompareValidator runat="server" ID="cValidator" ControlToValidate="TextBox1"
Type="Currency" Operator="DataTypeCheck" EnableClientScript="true"
ErrorMessage="Invalid format!" Display="Dynamic" />
Otherwise a RegularExpressionValidator would work, coupled with a RequiredFieldValidator to validate empty entries (the regex validator doesn't prevent empty entries). You could use a CustomValidator, but you would need to come up with a client side validation routine in JavaScript if you don't want to rely solely on server side validation with a postback. Also, the client side solution may involve regex and it's more work to validate overall, although not too complicated.
This is an example using the RegularExpressionValidator:
<asp:TextBox runat="server" ID="TextBox1" />
<asp:RequiredFieldValidator runat="server" ID="rfValidator" Display="Dynamic"
ControlToValidate="TextBox1" ErrorMessage="Required!" />
<asp:RegularExpressionValidator ID="reValidator" runat="server"
ControlToValidate="TextBox1"
EnableClientScript="True"
ErrorMessage="Invalid Format!"
Display="Dynamic"
ValidationExpression="(\d{1,3}(,\d{3})*\.\d{2})|(\d+(\.\d{2})?)" />

RequiredFieldValidator not working

With the following simple mark-up, I get very strange behaviour in FF and IE8. If I give the textbox focus, and tab out, nothing happens. If I give a user name value, and erase it immediately, nothing happens. However, only when I supply a user name, tab away, the erase it and tab away again, do I finally get a red star "required" mark. The summary doesn't show at all.
This is the markup I was trying with. Looks like my issue was with EnableClientScript and ValidationGroup:
<asp:Label ID="userNameLabel" runat="server"
AssociatedControlID="userNameText">
User Name:
</asp:Label>
<asp:TextBox ID="userNameText" runat="server"
Width="200px">
</asp:TextBox>
<asp:RequiredFieldValidator ID="userNameRequired" runat="server"
ControlToValidate="userNameText"
Display="Dynamic"
EnableClientScript="true"
ValidationGroup="userValidation"
ErrorMessage="User Name is always required.">
*
</asp:RequiredFieldValidator>
Are you sure EnableClientScript="true" is even needed? I think it defaults to that.

Resources