Using a Regular Expression Validator for numbers in a textbox - asp.net

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+)"

Related

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])$

how to force TextBox to only accept strings

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.

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 ?

validation on weight in asp.net?

can i get validation on weight which can take decimal values and also integer.
suppose if i enter 60 it has to accept and if i enter 60.50 it has to accept (60.1..etc)
but not characters. Thank you.
Use regular expression validator to constrain input.
Positive Decimal: (^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)
How To: Use Regular Expressions to Constrain Input in ASP.NET
you can use compare validator and use type double and operator data type check. it will work for you.
Ok try number two since I misunderstood your question :)
Use a RegularExpressionValidator and use the following expression:
^\d+\.?\d*$
Seems to match any number with as many decimal places as you allow.

Text box validataion for Decimal value

I have an application in which I want to validate a text box that it will take decimal value.
I would like to use a regular expression validator for this. What would this expression look like?
I wouldn't use a regular expression - create a custom validator that uses Decimal.TryParse under the covers.
Edit: To be fair to the question, here is a regular expression that would do the trick:
^\d*\.?\d+$
just the regex would be
`\d*`
I'd use a CompareValidator - for type decimal
And a Required field Validator - so blank is not allowed
But regular expression is accepable this link gives some examples http://msdn.microsoft.com/en-us/library/ms998267.aspx
I say to keep using the Regular Expression Validator, you'll get the added benefit of client-side validation with it.
Here is a list of regular expressions related to decimals:
http://regexlib.com/DisplayPatterns.aspx?cattabindex=2&categoryId=3
I would use a CompareValidator and use Regular Expression
^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$
This allow all decimal number, exclude all alphanumeric caracter
e.g.
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ErrorMessage="Only Decimal Value"
ValidationExpression="^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$">
</asp:RegularExpressionValidator>

Resources