Percentage regex - asp.net

Just a quick one here, does anyone know of a good regular expression for a percentage? Including 2 decimal places, i.e. 15.22%. I'm looking to put it inside a regularexpressionvalidator in ASP.NET.

This accepts 0.00%-100.00% including any number of leading zeros:
^0*(100\.00|[0-9]?[0-9]\.[0-9]{2})%$

(\d+(\.\d+)?%)
That should work.

\d\d\.\d\d%
To make the decimal portion optional:
\d\d(\.\d\d)?%
If single digit values are not padded with zeros:
\d{1,2}(\.\d\d)?%
Finally, to allow 100%
(100|\d{1,2}(\.\d\d)?)%

Updated to reflect #Lucero's input:
Forcing 2 decimals
^[0-9]+\.[0-9]{2}%$
Allowing either 2 decimals (with decimal) or integer. Both followed by percent.
^[0-9]+(\.[0-9]{2})?%$

Related

creating regular expression in asp.net using letters and numbers

I want to use regular expression using letters and numbers but it has to be minimum of 5 letters and at-least one number
^[a-zA-Z0-9]*$
^(?=.*[0-9])[a-zA-Z0-9]{5,}$
You can try this as well.A lookahead will make sure there is a number present.See demo.
https://regex101.com/r/uE3cC4/6
EDIT:
If you want atleast on letter too use:
^(?=.*[0-9])(?=.*[a-zA-Z])[a-zA-Z0-9]{5,}$
You can use the following regex:
(?=.*\d)[a-zA-Z0-9]{5,}
See .NET DEMO

Regular expression for x number of digits and only one hyphen?

I made the following regex:
(\d{5}|\d-\d{4}|\d{2}-\d{3}|\d{3}-\d{2}|\d{4}-\d)
And it seems to work. That is, it will match a 5 digit number or a 5 digit number with only 1 hyphen in it, but the hyphen can not be the lead or the end.
I would like a similar regex, but for a 25 digit number. If I use the same tactic as above, the regex will be very long.
Can anyone suggest a simpler regex?
Additional Notes:
I'm putting this regex into an XML file which is to be consumed by an ASP.NET application. I don't have access to the .net backend code. But I suspect they would do something liek this:
Match match = Regex.Match("Something goes here", "my regex", RegexOptions.None);
You need to use a lookahead:
^(?:\d{25}|(?=\d+-\d+$)[\d\-]{26})$
Explanation:
Either it's \d{25} from start to end, 25 digits.
Or: it is 26 characters of [\d\-] (digits or hyphen) AND it matched \d+-\d+ - meaning it has exactly one hyphen in the middle.
Working example with test cases
You could use this regex:
^[0-9](?:(?=[0-9]*-[0-9]*$)[0-9-]{24}|[0-9]{23})[0-9]$
The lookahead makes sure there's only 1 dash and the character class makes sure there are 23 numbers between the first and the last. Might be made shorter though I think.
EDIT: The a 'bit' shorter xP
^(?:[0-9]{25}|(?=[^-]+-[^-]+$)[0-9-]{26})$
A bit similar to Kobi's though, I admit.
If you aren't fussy about the length at all (i.e. you only want a string of digits with an optional hyphen) you could use:
([\d]+-[\d]+){1}|\d
(You may want to add line/word boundaries to this, depending on your circumstances)
If you need to have a specific length of match, this pattern doesn't really work. Kobi's answer is probably a better fit for you.
I think the fastest way is to do a simple match then add up the length of the capture buffers, why attempt math in a regex, makes no sence.
^(\d+)-?(\d+)$
This will match 25 digits and exactly one hyphen in the middle:
^(?=(-*\d){25})\d.{24}\d$

Regular expression in ASP.net

I wrote regular expression for phone no as ^[0]\d{9,10} (phone no should start with 0). This works fine.
But I want to omit the option repeating 0's. i.e 0000000000
How can I add this bit to it.
^0(?!0*$)\d{9,10}$
might be what you want.
The first number is a 0.
The (?!0*$) negative lookahead ensures that the rest of the string is not all zeroes.
And finally \d{9,10} matches any 9 or 10 digits.
You could specify [1-9] as the second digit instead of \d, like so:
^[0][1-9]\d{8,9}$
(I presume the rest of the digits could still be zeros)
I do note that your phone number format is fairly limited. For example, it doesn't allow for international numbers (starting with a plus sign), nor for any common formatting characters such as brackets spaces or hyphens. It also assumes that all phone numbers will be 10 or 11 digits long, which is (mostly) true in the UK and probably other countries, but may not always be the case.
Depending on the requirements of your system, you may want to adjust to take some of those points into account.

Weird behaviour on Numeric Stepper decimals

I am making an editor for a field with numbers. I tried a text field, but since it's a Number datatype coming in, it didn't go smoothly -- despite recasting strings as numbers etc.. it kept giving me NaN as the value. So I decided it would be best to go with a numeric stepper.
When I initially loaded it up it would drop all my decimals and only display my numbers as integers. I changed the stepIncrement to 0.1 and now it does show the decimals (a weird requirement imo).. but when I step up it occasionally gives me a value like '17.700000000000003' when I would expect 17.7. All of the numbers in my data have a single decimal place. I know I can write a dataformatter, but it seems like it shouldn't be necessary in this situation.
Is there another way I could deal with this?
You've stumbled upon the compromise of trying to represent decimal numbers in floating point binary formats like IEEE 754. Not all decimal numbers can be exactly represented. You can read up on this issue in great detail here:
http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding
You can use Number.toFixed(fractionDigits:uint) to display to an arbitrary number of decimal places.
You can use the valueFormatFunction which takes the numeric value and formats it to a string. You will need to set explicit widths on your numeric steppers to make they fit though.
in your MXML
<s:NumericStepper valueFormatFunction="stepperFormatter"/>
in your script
protected function stepperFormatter(newValue:Number):String
{
return Math.ceil(newValue).toString()
}

Validation to allow space for phone numbers

i have a validation in my .net textbox where it will take only numbers
but when i put the the phone format like
080 234234
it will not accept because of a space
how to resolve this ?
could anyone help in regular expression ?
Current expression is this [0-9]+
Simply add space to characters range:
[0-9][0-9 ]*
You can also add start and stop indicators:
^[0-9][0-9 ]*$
EDIT:
number must start with digit followed with digits or spaces (zero or more).
You could use
([0-9]+\s*)+
or
(\d+\s*)+
either of which would allow one or more groups of digits followed by optional whitespace
Really, the best way to deal with this is to remove all non-digit characters, then do whatever additional validation you may require, such as the number of digits or whether the number begins with a valid area code/country code, on what's left. That way it doesn't matter whether the number is entered as (assuming US numbers here) 987-654-3210, (987) 654-3210, 987 654 3210, 9876543210, 9 8 7-6.54321 0, or whatever else.
Concentrate on validating what's meaningful in the input (the digits) and not incidental details which really don't matter (how the digits are grouped or formatted).

Resources