how to force TextBox to only accept strings - asp.net

Sorry for the Dummy Question , i know :( ,, but it's only the simple things that dont work with me :((
i have many text boxes and i want the user to only insert String and not Numeric numbers ,
how could i handle it in easy way ??
as it takes every thing and apply it to the database , or should i control it from the Database
PS. i have searched a lot but with no good answer

use [a-zA-Z]+ for ValidationExpression:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator"
ValidationExpression="[a-zA-Z]+"></asp:RegularExpressionValidator>

You could take a look at validation techniques for asp : http://msdn.microsoft.com/en-us/library/7kh55542.aspx
This provide a set of tools to check whether the input match what you expect.

You can do it easily in AJAX,just download it from here
first add a script manager to your page then add FilteredTextBoxExtender to the textbox and set it's properties as you wish.

A Regular Expression could be applied to the input
For the basics on RegEx : http://www.regular-expressions.info/tutorial.html
And also see
http://www.regular-expressions.info/dotnet.html

You can use regex to do that with jQuery.
In this example, I replace only digits.
You can adapt the regex to replace any set of characters with an empty string.

Related

RegularExpressionValidator to limit input length and allow empty strings

I'm really bad with regex and was looking at another question almost identical to this but can't get it to work as I need.
I was to use a RegularExpressionValidator that will allow any character up to 255 characters or nothing at all. I tried,
ValidateExpression="^.{255}$"
but it throws an unhanded exception when the textbox that I'm checking is empty.
I've also tried
ValidateExpression="^.{,255}$"
Thank you
Rodney
Did you try ^.{0,255}$? Not sure what exception you are getting though.
EDIT: Also, if struggling with .Net regex, Regex Hero tester is a great help. I know there are other sites, but this one is by far the best, if you ask me.
The proper expression is ^.{0,255}$.
^.{255}$ will only match if there is exactly 255 characters, while ^.{,255}$ will match the literal string "{,255}".
If there are still issues after trying that, can you tell us the exception?
If it throws an HttpRequestValidationException exception, you can use the members of the UnvalidatedRequestValues class.
Mind that "Validation succeeds if the input control is empty" (MSDN). You may add a RequiredFieldValidator field to ensure that the user does not leave a text box blank (if you use the RequiredFieldValidator control inside an UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel - MSDN):
<asp:textbox id="myTB"
runat="Server">
</asp:textbox>
// ... MAKE SURE YOU DO NOT USE TextMode="Number"!!!
<asp:RequiredFieldValidator
ID="Value1RequiredValidator"
ControlToValidate="myTB"
ErrorMessage="Please enter a number.<br />"
Display="Dynamic"
runat="server"/>
And as for regex, ^.{255}$ means match any character (except newline) exactly 255 times between string start and end. I think it makes sense to allow ^[1-9][0-9]{0,254}$ (for values like '34', '104', etc.).

how to restrict a textbox to accept only two string i.e True/false (case insensitive) using regular expression in asp.net

I want to restrict a text box to accept only two strings i.e true/false using regular expression.
My code is working partially. For lower case, it's working fine but I want it to be case insensitive.
my regular expression looks like this
<asp:RegularExpressionValidator ID="regAssign1" runat="server"
ControlToValidate="tb_Assign" ErrorMessage="Wrong Input!" forecolor="Red"
ValidationExpression="^(true)|(false)$" ValidationGroup="Submit"></asp:RegularExpressionValidator>
I want to get this done using a regular expression validator only. Suggestions using any other options like use JavaScript or use a drop-down list should be avoided.
You should change your regex to:
"^(true|false)$"
Please note that when using that, editor still accept empty string so you need to add a RequiredFieldValidator as well. Also for both of them if you added :
Display="Dynamic"
it would look better.
The simple way is just like this...
^true$|^false$
This version allows whitespace before or after, which you can remove later with Trim.
^\s*true\s*$|^\s*false\s*$
I persistently tried to use ?i: for case insensitivity, it did not work in my RegularExpressionValidator. So here are some possible alternatives that will allow the three most likely case versions "true", "True", and "TRUE".
^true$|^false$|^True$|^False$|^TRUE$|^FALSE$
and allowing whitespace...
^\s*true\s*$|^\s*false\s*$|^\s*True\s*$|^\s*False\s*$|^\s*TRUE\s*$|^\s*FALSE\s*$
Also, as another answer pointed out, you will need a RequiredFieldValidator to disallow an empty TextBox.
Try this regex:
^(?i:true)|(?i:false)$
The i makes comparison case-insensitive for the words following the colon.
Alternatively, try this:
^([T|t][R|r][U|u][E|e])|([F|f][A|a][L|l][S|s][E|e])$

Validation for TextBox For a User Form

In my User Registration Form, I want my user address text box to accpet alphanumeric as well as special character but should not contain only numeric or special character.
I am currently using regular expression validator ,what regular expression i can use.
Or Is there any different solution for that.
Regards
Here you go.. I have used it at one place, and works pretty well.. just provide your acceptable characters and you are good to go
http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/FilteredTextBox/FilteredTextBox.aspx
You mean from your question that a string of alphabets is a must right ?

Using a Regular Expression Validator for numbers in a textbox

How can I use a Regular Expression Validator to ensure that only a number like 3.00 is entered into a text box?
You could use the CompareValidator with type="Double" to allow numbers only.
Using the RegularExpressionValidator you could use this ValidationExpression: \d+\.\d{2} (One or more digits, a decimal point, two digits.)
I always use this nice little tool to compose regular expressions.
Does this answer your question or do you need help on how to use the RegularExpressionValidator control in general?
If you want to verify those cases like 123, 123., 123.12, and .23, the following may be used:
ValidationExpression="(\d+\.\d+)|(\d+\.)|(\.\d+)|(\d+)"

Regex to limit string length for strings with new line characters

Looks like a simple task - get a regex that tests a string for particular length:
^.{1,500}$
But if a string has "\r\n" than the above match always fails!
How should the correct regex look like to accept new line characters as part of the string?
I have a <asp:TextBox TextMode="Multiline"> and use a RegularExpressionValidator to check the length of what user types in.
Thank you,
Andrey
You could use the RegexOptions.Singleline option when validating input. This treats the input as a single line statement, and parses it as such.
Otherwise you could give the following expression a try:
^(.|\s){1,500}$
This should work in multiline inputs.
Can you strip the line breaks before checking the length of the string? That'd be easy to do when validating server-side. (In .net you could use a custom validator for that)
From a UX perspective, though, I'd implement a client-side 'character counter' as well. There's plenty to be found. jQuery has a few options. Then you can implement the custom validator to only run server-side, and then use the character counter as your client-side validation. Much nicer for the user to see how many characters they have left WHILE they are typing.
The inability to set the RegexOptions is screwing you up here. Since this is in a RegularExpressionValidator, you could try setting the options in the regular expression itself.
I think this should work:
(?s)^.{1,500}$
The (?s) part turns on the Singleline option which will allow the dot to match every character including line feeds. For what it's worth, the article here also lists the other RegexOptions and the notation needed to set them as an inline statement.

Resources