Validating using HTML5 pattern - html

I need to validate two possible patterns for the input using HTML5 pattern.
123456789a (first 9 digits should be exactly numbers and then an alphabetical character) nothing more nothing less
OR
123456789012 (exactly 12 digits nothing more nothing less)
I tried ^([0-9]{12,12})|([0-9]{9,9}[A-Za-z]{1,1}), ^([0-9]{12})|([0-9]{9,9}[A-Za-z])$, and many more but the problem is if user enters an alphabet character when the total length is between 9 and 12, then it takes as a valid input. But it should be not.
Valid input is either 12 digits, or 9 digits with one char.
What have I done wrong?

You could check for 9 digits at the start of the string: (see ^, beginning of input assertion, \d, digit character class and the x{n} quantifier)
^\d{9}
followed by either an alphabetical character or 3 more digits, and the end of the string: (see the non capturing group (?: ... ), [ ... ], the character set, x|y and $, end of input assertion)
(?:[a-zA-Z]|\d{3})$
So the expression would be:
^\d{9}(?:[a-zA-Z]|\d{3})$

Related

Finding numbers with a specific length from csv file

I'm working with a csv file from a customer, which holds a large amount of data. The data is extracted from an SQL database and the commas therefore signify the different columns. In one of these columns there are 10 digit numbers. For some reason all 10 digit numbers starting with 0 have been converted to 9 digit numbers with the 0 removed. I need to find all these instances and insert a 0 at the beginning of the 9 digit number.
A complication in the data is that another column also contains 9 digit numbers, and these do not need to be modified. I can assume, however that all those numbers start with 0 and all the numbers i need to find do not start with 0.
I'm currently using notepad++ trying to fix the problem and found the regular expression \d{9} which finds all numbers with 9 digits, but that is not what I'm looking for
Below i have an example of how the data could look. The column that needs all 9 digit numbers converted is on the left, and the other column with 9 digit numbers is on the right.
An example of the data that is causing the trouble could be:
Column 1
Column 2
2323232323
002132413
231985313
004542435
In this example I need to find the second line of column 1 and insert a 0 in front of the number.
Ctrl+H
Find what: \b(?!0)\d{9}\b
Replace with: 0$0
TICK Wrap around
SELECT Regular expression
Replace all
Explanation:
\b # word boundary, make sure ae haven't digit before
(?!0) # negative lookahead, make sure the next character is not 0
\d{9} # 9 digits
\b # word boundary, make sure ae haven't digit after
Replacement:
0 # 0 to be inserted
$0 # the whole match (i.e. 9 digts)
Screenshot (before):
Screenshot (after):
Using Notepad++ do CTRL + H (search and replace utility).
Tick Regular Expression
Find what ? ([^0-9])(\d{9})([^0-9])
Replace with ? \10\2\3
Explanation :
([^0-9])(\d{9})([^0-9]) matches a 9 digit number surrounded by a non-digit on each side (including line return / comma, etc) :
Each (....) "captures" a group for later use (in "replace").
[^0-9] is a non-number character
\d{9} is a 9 digits number
\10\2\3 is a 0 right after the first captured group \1 (it was just one character here) followed by the 9 digit number (2nd captured group : \2) and the character that was after that number (3rd captured group : \3).
Limit :
It won't match a number at the very beginning of the file (before any other character) or at the very end (after every character). Adding a newline at the end of the file is one workaround, or fixing the last number manually if there is no newline before EOF.

Allow only 1 letter and unlimited numbers in input

So, banging my head on the wall with a regex test site and can't seem to nail this one down.
Trying to make it so an HTML is validated to only allow 1 letter maximum, but unlimited numbers, no other characters.
W123 = valid
124X = valid
1234 = valid
WW12 = invalid
Nothing incredible here, only two possible cases:
the string starts with a digit
the string doesn't start with a digit
In the two scenari only one letter is allowed.
To express that you need a group and an alternation:
^(?:\d+[A-Z]?|[A-Z]\d)\d*$
In the two branches stored inside the group, you can see that there's at least one digit (in the first because I used the + quantifier, in the second because of \d). The first branch matches any string that starts with a digit with and without a letter (because of the optional letter [A-Z]?), the second one matches strings that start with a letter. \d* at the end of the pattern matches remaining digits.
Obviously the pattern is enclosed between anchors for the start ^ and the end of the string $.
You look for zero or more digits followed by a letter followed by zero or more digits or alternatively at least one digit.
^\d*[a-zA-Z]\d*$|^\d+$

Regex of max 5 decimal separate by +

i'm trying to make a regex to add to a input pattern (HTML) to check if is valid,
it need to be valid only if the input contain a string composed by decimal(with 1 or 2 number after comma) or integer number separated by a +
and maximum of 5 number
and it can not start or end with a + or it can not be possible to have a number with comma without number after (i use comma instead of dot for decimal)
for example
10+5,1+6,20 OK
10 OK
6+4+8,9+3+9+3 NO
10,2+4+6+ NO
10,+5 NO
i've tried with something like this but id doesn't work very well
((\d{1,3}|(\d*,\d{1,2})*)+(\+)?){1,5}
also i've tried with this:
^((\s*)|([0-9]\d{0,9}(\,\d{1,2})?%?))*(\+((\s*)|([0-9]\d{0,9}(\,\d{1,2})?%?))+){0,4}$
but it doesn't work very well with the 2 digit max for the decimal and ending +
any suggestions ??
i've made some test here:
https://regexr.com/5jsfv
it should pass the first 3 and faile on the last 4
thanks
You can use
^\d+(?:,\d{1,2})?(?:\+\d+(?:,\d{1,2})?){0,4}$
In the HTML pattern attribute use it as
pattern="\d+(?:,\d{1,2})?(?:\+\d+(?:,\d{1,2})?){0,4}"
See the regex demo.
NOTE: If you want to limit the number of digits in the integer part to be max 3, replace the \d+ with \d{1,3}:
^\d{1,3}(?:,\d{1,2})?(?:\+\d{1,3}(?:,\d{1,2})?){0,4}$
Details:
^ - start of string (implicit in pattern regex)
\d+(?:,\d{1,2})? - one or more digits and then an optional sequence of a , and one or two digits
(?:\+\d+(?:,\d{1,2})?){0,4} - zero to four occurrences of a + char followed with one or more digits and then an optional sequence of a , and one or two digits
$ - end of string (implicit in pattern regex)

I want to prevent a textbox to accept only alphabets

I am designing a membership form and want only alphabets is textbox as username.
ng-pattern="/^\d{1,10}$/"
I tried it but it not works.
The pattern you're using will only get digits
/^\d{1,10}$/"
\d{1,10} matches a digit (equal to [0-9])
{1,10} Quantifier — Matches
between 1 and 10 times, as many times as possible, giving back as
needed
$ asserts position at the end of a line
If you want only alphanumeric characters try this:
/^[a-zA-Z]*$
It will accept any character a through z -- capital or lower case.

I need a regular expression that matches the following

I need a regular expression that matches the following to be used on a web page:
-The first number must be a digit
-The length must be a minimum of 10 digits
-No spaces, alpha, special characters allowed
As my understanding, you need to check if it is a number (containing only the digits 0-9) and this number should be minimum of 10 digits.
Then the following code should work:
/^\d{10,}$/