Regular expression to validate textbox length - javascript

I want a regular expression to validate an ASP textbox field with the minimum length of 11 characters and in the middle of the string should be a "-" sign. The sample string is: "0000-011111". I want to validate the textbox to make sure user enters a minimum of 10 numbers with "-" sign after 4 digits using regular expressions. Please help me.
Thank you.

Use
\d{4}-\d{6}
\d represents a digit, - is a literal dash and the number in the curly brackets force the preceeding token to be present the given number of times.

^\d{4}-\d{6,}$
You should use also ^ at the beginning and $ at the end to ensure that there is nothing before and after your string that you don't want to have. Also important is the {6,} so it will match at least 6 digits, without , it will match exactly 6 digits. If you want set a maximum of digits you can specify after the ,, e.g. {6,20}.

Related

Reqular expression of 7 digits numbers with optional special characters in between

I need validate a 7 digit number with optional dash in between.
I was able to get if I use below.
^(\d-?\d-?\d-?\d-?\d-?\d-?\d)$
Is there a way to shorten that?
I tried ^(\d+(-?){7})$ but it's not working.
Valid 123-09-23
Valid 12-3092-3
Valid 1-230-9-23
Valid 1234567
Invalid -1237883
Invalid 12345678
InValid 123-45-678
PS: I will be implementing this in my Javascript application.
Repeat the group only (7 times, so you get 7 digits total), and don't repeat the \d as well (else you may match more digits than desired):
^(?:\d-?){7}$
https://regex101.com/r/yLQHWW/1
(Your original pattern is equivalent to: "Match one or more digits, optionally followed by up to 7 - characters".)
Start with a digit and repeat -?\d six times:
^\d(-?\d){6}$
https://regex101.com/r/oTSqri/1

How do I use regex for an integer range from 18-99?

I'm trying to create a javascript regex for a Google Form to validate an answer for an age related question. I want the user to only be able to enter whole number values between 18 and 99.
These values must be numerical only, cannot lead with zero(s), be negative, or include decimals. They must be whole positive numbers only.
I'm very new to regex this is my best attempt.
(1[89]|[2-9][0-9])
\d(1[89]|[2-9][0-9])
The first attempt will keep answers within my range (18 to 99) however it allows values with decimals, preceding zeros, and even trailing letters.
Second attempt when I use \d It only seems to accept values that lead with a zero despite if they include a decimal or trailing letter.
Then you can make a capture group like this ;
^(1[89]|[2-9][0-9])$
https://regex101.com/r/m2icLR/1
You can Try ^.{18,99}$
Explanation:
. dot stands for all characters. Except \n for which you will have to use s DOTALL flag.

Javascript regular expression validate phone number

I need to validate phone number by regular expression. The phone number should
Start with (9|09|8869|+8869)
Followed by 8 digit [0-9]
I come up with /(09|9|8869|+8869)[0-9]{8}$/g.
I test with +8869900000000 and expect it will not match but actually it passed
Could you help me to address the regex problem? And how do I fix it?
You can use this regex: /^(0?9|\+?8869)\d{8}$/
The group (0?9|+?8869) is for your starting condition where 0 is optional before 9 and + is optional before 8869.
Demo: https://regex101.com/r/1OpYl0/1/
The regex you are looking for is : ^(([0]?9)|([+]?8869))[0-9]{8}$
Note the way round brackets used to determine the conditions. We need to match within any of the 2 subsets and then precede it 8 digits.

UK bank sort code javascript regular expression

I'm trying to create a regular expression in javascript for a UK bank sort code so that the user can input 6 digits, or 6 digits with a hyphen between pairs. For example "123456" or "12-34-56". Also not all of the digits can be 0.
So far I've got /(?!0{2}(-?0{2}){2})(\d{2}(-\d{2}){2})|(\d{6})/ and this jsFiddle to test.
This is my first regular expression so I'm not sure I'm doing it right. The test for 6 0-digits should fail and I thought the -? optional hyphen in the lookahead would cause it to treat it the same as 6 0-digits with hyphens, but it isn't.
I'd appreciate some help and any criticism if I'm doing it completely incorrectly!
Just to answer your question, you can validate user input with:
/^(?!(?:0{6}|00-00-00))(?:\d{6}|\d\d-\d\d-\d\d)$/.test(inputString)
It will strictly match only input in the form XX-XX-XX or XXXXXX where X are digits, and will exclude 00-00-00, 000000 along with any other cases (e.g. XX-XXXX or XXXX-XX).
However, in my opinion, as stated in other comments, I think it is still better if you force user to either always enter the hyphen, or none at all. Being extra strict when dealing with anything related to money saves (unknown) troubles later.
Since any of the digits can be zero, but not all at once, you should treat the one case where they are all zero as a single, special case.
You are checking for two digits (\d{2}), then an optional hyphen (-?), then another two digits (\d{2}) and another optional hyphen (-?), before another two digits (\d{2}).
Putting this together gives \d{2}-?\d{2}-?\d{2}, but you can simplify this further:
(\d{2}-?){2}\d{2}
You then use the following pseudocode to match the format but not 000000 or 00-00-00:
if (string.match("/(\d{2}-?){2}\d{2}/") && !string.match("/(00-?){2}00/"))
//then it's a valid code, you could also use (0{2}-?){2}0{2} to check zeros
You may wish to add the string anchors ^ (start) and $ (end) to check the entire string.

Javascript REGEX

I need a javascript REGEX to check that the length of the string is 9 characters. Starts with 'A' or 'a' and is followed by 8 digits.
Axxxxxxxx or axxxxxxxx
/^[aA][0-9]{8}$/ or /^[aA]\d{8}$/
Also makes sure the x's are digits :)
This should do it:
/^[aA]\d{8}$/
or
/^a\d{8}$/i
This is probably what you want.
/^([aA]\d{8})$/
The carot character means the regex must start searching from the beginning of the string, and the dollar character means the regex must finish searching at the end of the string. When they are used together it means the string must be searched from start to end.
The square brackets are used to specific a character or a range of allow characters. The slash and d means to search any digit character. The brackets at the end specify a static quantity that applies to the previous test definition. A range of quantities can be used by specifing a minimum value immediately followed by a comma immediately followed by a maximum value.
did you mean this?
/^[aA]\d{8}/
or did you mean 9 chars ?
/^[aA]\d{8}/
or did you mean A + 8 equal chars ?
/[aA](.)\1{7}/

Categories