Validate int value with regular expression - regex

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.

Related

Issue faced during the implementation of the regex expression in HTML form

Scenario:
I have tested the following regex expression in https://www.regextester.com/ And it performs the validation as required. That means it doesn't allow the expression containing '#' character.
Issue Faced:
But, when I implement the same regex expression for form validation in HTML then it fails to validate. That means it also allows the expressions containing '#' character, which is not the required outcome.
Here is the regex expression:
^[a-z0-9]+\.([a-z0-9])+(?!#)+(?!outlook.com)+(?!gmail.com)+$
Implementation in the HTML Form as:
<td>
<input type="text" name="exchange_usertoadd" required="required"
pattern="^[a-z0-9]+\.([a-z0-9])+(?!#)+(?!outlook.com.np)+(?!gmail.com)+$"
placeholder="Enter Exchange Username" value="" size="30">
</td>
Required Outcome:
1. The above regex expression should allow the following expressions:
a. john.mayor
b. michael.clarke
c. jitendra123.gurung
and so on.
2. But it shouldn't allow the following expressions:
a. john.mayor#gmail.com
b. jitendra.gurung#
c. sarad#
d. michael.clarke#outlook.com
and so on.
What correction do I need in the implementation of the regex expression that I have tried above?
Suggestions are highly appreciated.
Try like:
/^([a-z0-9]*([.])*([a-z0-9]))+(?!outlook.com.np)(?!gmail.com)/mg
Its in JS pattern, please change it for your input field
Demo
Perhaps I am reading your requirements too loosely, but what about using ^[^#]+$ for your regular expression? It matches all of your given test cases.
I modified the expression and it meets the requirements that I need for the validation.
This is the correct working expression which meets my requirement:
^([a-z0-9]*([.])*([a-z0-9]))+(?!#)(?!outlook.com.np)(?!gmail.com)+$

Regex in angular- first digit not zero, but allow single zero

I want to have input element which allows one of two conditions:
Single zero can be entered
Number with max of 9 digits can be entered, but first digit shouldn't be zero
I wrote this regex (solution works in online regex testers):
/(^0$)|(^[1-9]\d{0,8}$)/
But when I use it in ng-pattern in Angular, it doesn't work.
Here is my plunker example:
http://plnkr.co/edit/iDQ7ly8ypJ3UmN5A0hJw?p=preview
Not sure if alternation is doing the problems, or I messed up the regex.
UPDATE: it seems that type="number" is causing problems. Unfortunately, I need to have this in my code, so I'm searching for solution which works with type="number".
This should work for you. I did the following:
Took out the type="number".
Gave the form a name.
Gave the input a name.
Referenced the form and input via their names instead of their id and ng-model values, respectively.
It converts the value to a number under the covers, stripping the leading zeros and converting text to 0, etc.. And the name is the correct way to access it as far as I can tell.
<form name="myForm">
<input name="myNumberField" ng-model="myNumber" ng-pattern="/(^0$)|(^[1-9]\d{0,8}$)/" required/>
<span ng-show="myForm.myNumberField.$error.pattern">Invalid pattern</span>
</form>
Here is a plunker for it.

advanced regex pattern for html5 input

I can't seem to put together a working pattern to disallow all html tags except for the strong and em tag.
I don't want to parse the html but just want to give the user a warning that the input will not be accepted. I am aware that this is not supported in all browsers but I would love a pure html solution, as I already have a working JS solution, but I wan't to layer the user experience.
<input name="user_input" pattern="^(?!<[^>]*>).*$" />
So allowed tags: strong, em
the use of all other tags should make the result false
Any one able to crack this one?
KR
edit:
<input type="text" pattern="((?!<(?!\/?(strong|em))[^>]*>).)*">
is what seems to do the trick. Thank you for your help!
You can use a Negative Lookahead (?!) for this purpose.
An example regex string which matches the entire pair:
<(?!\/?strong|\/?em)[^>]*>.*(?:<\/.*?>)?
A shorter regex, which matches the first tag only
<(?!\/?(strong|em))[^>]*>
This match will pass if a HTML tag with something EXCEPT strong or em exists.
So, if match = $true, you can deny the input and give the user a warning.
Regex101 demo

Keeping Regex search to one line

I used Wget to scrape a site for migrating to new platform. I am trying to clean up the pages and remove all the viewstate code in them. I am using the following regex expression to do this:
<input type="hidden" name="__VIEWSTATE" value=.*/>
This works in programs like dreamweaver. I like to use another application called Wild Edit which is extremely fast for search and replace for large number of files. When I use that same expression it will match to the last /> on the page remove alot of good code. I have also tried <input type="hidden" name="__VIEWSTATE" value=.*/>$ with same results.
How would I constrain this to keep it to the first match of />
Try
<input type="hidden" name="__VIEWSTATE" value=.*?/>
The ?, if it's supported makes the search ungreedy so it will only match until the first /> rather than the last.
If that doesn't work, your best bet may be:
<input type="hidden" name="__VIEWSTATE" value=[^/]+/>
The regex is being too greedy. Try this:
<input type="hidden" name="__VIEWSTATE" value=.*?/>
By default, the regex engine tries to make as large of a match as possible. For example, the regular expression a.*z will match az (some other middle stuff) az as one big match, since, well, it does start with a and end with z.
The ? modifier tells the regular expression engine to, rather than be greedy, be lazy: instead of grabbing the largest possible match, grab the smallest. In the previous example, the regex a.*?z will just match the 2 az substrings, because it's being lazy: once it sees the z, it stops.

How do I deal with this regex issue?

I need to extract a value from a hidden HTML field, have somewhat figured it out but I'm currently stuck.
My regex looks like:
<input type="hidden" name="form_id" value=".*"
But this extracts the whole string from the HTML.
The string looks like:
<input type="hidden" name="form_id" value="123"/>
I need to extract the "value" from the string, it is always changing, but the "name" is always the same. Is there a way to extract it without doing another expression? I appreciate any help.
(?<=<[^<>]+?name="form_id"[^<>]+value=")(.*)(?=")
I just threw this together. Basically you want to negate any ending > in your request. So you'd likely want to do something of this nature:
<[^>]*hidden[^>]*value="(.*)"[^>]*>
And then read the first capture group (Delphi instructions). This keeps it as reasonably generic as possible although it does assume positional order on "hidden" and "value".
In order to find the value without regard for order you could use could use a slightly cleaner lookahead as was suggested:
^(?=.*name="form_id").*value="([^"]*)".*$
<[a-zA-Z"= _^>]*value="(\d*)"/>
I have tested this for your example.
If you want to extract for only input tag you can write:
<input[a-zA-Z"= _^>]*value="(\d*)"/>