regular expression not to allow zero
it should allow 0.0000001 as value but should not allow to enter 0.
I need validator not javascript
I think all you need is this ^(?=.*[1-9])\d*\.?\d*$
But, you could get fancy and allow only a single leading zero if its before a decimal point.
^(?=.*[1-9])(?:[1-9]\d*\.?|0?\.)\d*$
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Input is not valid."
ValidationExpression="^(?=.*[1-9])(?:[1-9]\d*\.?|0?\.)\d*$"></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
a regexp like that?
([1-9](\.[0-9]+)?)|(0\.[0-9]*[1-9])
looks like working ;-)
if you remove the braces it looks more understandable:
[1-9](\.[0-9]+)? | 0\.[0-9]*[1-9]
If you want to allow both Positive or Negative numbers but not 0:
^(-?.*[1-9])\d*\.?\d*$
Related
I need a regex for fetching the value in the </span> tag
<span class="booking-id-value">U166097</span>
value required: U166097
can please someone suggest me. I have tried using
<span class="booking-id-value">(.+?)
but it is not deriving the desired result it display on "U"
I think you need to be more specific about your expected value - below I'll just accept alphabetic and numeric characters as value - and more flexible about your tag, then I can suggest you to use a regex like this:
/<\s*span.+?class\s*=\s*"\s*booking-id-value\s*".*?>/s*([A-Za-z0-9]+)\s*<\//
Regex Demo
? after the .+ makes it ungreedy, tells it to match as little as possible - and that’s just the first U in this case.
Remove the ?, and instead look for the closing </span> after (.*) to terminate what is matched correctly:
<span class="booking-id-value">(.+)<\/span>
https://regex101.com/r/vt4pgN/1/
Regex:
<span.*>(.*)<\/span>
Substitute with:
$1
Result
I am using following lines of code
<asp:TemplateField HeaderText="Phone" SortExpression="Phone">
<ItemTemplate>
<asp:Label ID="lblPhone" runat="server">
<%# Regex.Replace(Eval("Phone").ToString(), #"(\d{3})(\d{3})(\d{4})", "($1)-$2-$3") %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
It is formatting numbers like 9419002345 but not numbers like 1408-464-1680 or 463237062... Please help me in writing accurate regular expression.
That is because your regular expression just matches when there are ten digits. Your 463237062 has only 9.
You should make the algorithm allow matching on 9 digits too, for example like this:
#"(\d{1,3})(\d{3})(\d{4})"
You can try this:
(\d{10}) will compare 10 numbers
OR
4 digits-3 digits-4 digits.
(\d{10})|(\d{4}-\d{3}-\d{4})
Adjust number of digits as per your need.
I want to validate that input only allow maximum value: 2,147,483,647. But it's not validate at all. What is wrong in there regular expression.
<input
pattern="^(([1-9][0-9]{1,9})|([2][0-1][0-4][0-7][0-4][0-8][0-3][0-6][0-4][0-8]{10}))$"
required type="number" class="form-control" />
I think a regex may be a poor solution choice for what you are seeking. That said, below is what I believe to be a correct regex for what you are seeking to do:
^(0*(
[1-9][0-9]{1,9})|
([1-2][0-1][0-4][0-7][0-4][0-8][0-3][0-6]4[0-8])|
([1-2][0-1][0-4][0-7][0-4][0-8][0-3]6[0-4][0-8])|
([1-2][0-1][0-4][0-7][0-4][0-8]3[0-6][0-4][0-8])|
([1-2][0-1][0-4][0-7][0-4]8[0-3][0-6][0-4][0-8])|
([1-2][0-1][0-4][0-7]4[0-8][0-3][0-6][0-4][0-8])|
([1-2][0-1][0-4]7[0-4][0-8][0-3][0-6][0-4][0-8])|
([1-2][0-1]4[0-7][0-4][0-8][0-3][0-6][0-4][0-8])|
([1-2]1[0-4][0-7][0-4][0-8][0-3][0-6][0-4][0-8])|
(2[0-1][0-4][0-7][0-4][0-8][0-3][0-6][0-4][0-8])|
([1-2][0-1][0-4][0-7][0-4][0-8][0-3][0-6][0-3][0-9])|
([1-2][0-1][0-4][0-7][0-4][0-8][0-3][0-5][0-9]{2})|
([1-2][0-1][0-4][0-7][0-4][0-8][0-2][0-9]{3})|
([1-2][0-1][0-4][0-7][0-4][0-7][0-9]{4})|
([1-2][0-1][0-4][0-7][0-3][0-9]{5})|
([1-2][0-1][0-4][0-6][0-9]{6})|
([1-2][0-1][0-3][0-9]{7})|
([1-2]0[0-9]{8})|
(1[0-9]{9})
)$
The whitespace and newlines are only for clarity and are not part of the regex. I do not guarantee its correctness though I think it is. You can try this out at a place like Regex 101.
As you can see this is complex and should probably be generated by a program instead of a human.
The ugliness of the answer hints that it is a poor choice of solution.
I need to limit an input box to either accept a number or a certain string pattern
<input type="text" ng-model="value" name="value" ng-pattern="/^([0-9])|([R])$/" required>
This does not seem to fully work, i can add 555f and this will be a valid value for my ng-model
How can i limit this to either match a digit or my string pattern?
http://jsfiddle.net/DaleS/dv7vuecz/
You have a problem with your reg exp in ng-pattern, this: /^([0-9])|([R])$/ will mach any string that starts from a number or ends with 'R'. And for what you want, you need smth like this:
/^[0-9]+$|^[R]+$/
It will match any string that is number, or starts and ends with 'R'.
^([0-9]|[R])$
Try this.See demo.
https://regex101.com/r/eZ0yP4/10
I have a input of type number, and I want to make sure it only accepts a number. I can do this fine on the Server side, but with AngularJS I can not get it to work.
Here's the code:
<input type="number" ng-pattern="/[0-9]+/" name="numOfFruits" ng-model="basket.numOfFruits" />
I suspect this has something to do with the pattern I am supplying [0-9]+ basically I only want numbers in this text box, anything that is not made up of the numbers 0 to 9, I want the input invalid.
At the moment, my input field sees this aa23423 as valid input!
You need to use anchors:
/^[0-9]+$/
^: Start-of-line anchor.
[0-9]+ One or more numbers between 0 and 9.
$: End-of-line anchor.
So this matches the start of the string, then it matches the one or more digits, after that it looks for the end-of-string, so it matches a string containing only numbers and nothing else.
Otherwise, /[0-9]+/ will match only a part of aa23423, more accurately the number 23423 and thus will give you valid.
Here is regexp to validate floating point numbers, both positive and negative:
/^-?[0-9]\d*(\.\d+)?$/
Use this regexp in 'text' input, example:
<input type="text" ng-model="score" ng-pattern="/^-?[0-9]\d*(\.\d+)?$/" required/>
Pattern don't work for input with type="number".
You can use type="text" and than convert value to number
Try defining your regex as a scope variable
In the controller, it worked for me.