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>
Related
i want to use regular expression to validate SSN with dashes. This is the format i would like to see: 000-00-0000. This expression does not work for me for some reason.
<asp:RegularExpressionValidator runat="server" Display="Dynamic" ValidationExpression="^(\\d{3}-\\d{2}-\\d{4}|\\d{9})$" ControlToValidate="txtSSN" ForeColor="Red" />
this is how i resolved:
^\d{3}-\d{2}-\d{4}$
There are a few things to note about the RegularExpressionValidator:
It will never be activated by an empty string in the control it is
validating. Only the RequiredFieldValidator catches empty strings You
do not need to specify beginning of string and end of string matching
characters (^ and $)—they are assumed. If you add them, it won't hurt
(or change) anything—it's simply unnecessary.
You could just refer to an example regex on thatpage in MSDN, and see that the value is treated as a verbatim string literal.
So, to only allow SSNs with hyphens, you just need to use:
ValidationExpression="\d{3}-\d{2}-\d{4}"
It is also ECMAScript-compliant and safe to use both at server and client, but you could further limit to
ValidationExpression="[0-9]{3}-[0-9]{2}-[0-9]{4}"
Remember, \d at server-side can also match some non-standard (Arabic, etc) digits.
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])$
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.
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+)"
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.