I need a regex for validating a string / decimal in JavaScript.
Which can be max 9 elements long with 2 decimals
Simply
123 - valid
123456789 - valid
1234567896 - invalid ( max 10 chars )
123. - invalid
123.2 - valid
123.32 valid
123.324 invalid ( 3 decimal points )
So I wrote a regexp like this
/^([0-9]{1,9})+[.]+([0-9]{0,2})$/
Can any one plz fine tune this regex
You can use regex ^(?=.{0,10}$)\d{0,9}(\.\d{1,2})?$
$('input').on('input', function() {
$(this).css('color', this.value.match(/^(?=.{0,10}$)\d{0,9}(\.\d{1,2})?$/) ? 'green' : 'red');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type=text/>
Regex explanation here
Give the following a try:
^\d{1,9}(\.\d{1,2})?$
Something like this?
/^[0-9]{1,9}(\.[0-9]{0,2})?$/
You may use a negative lookahead at the beginning to apply a length restriction to the whole match:
^(?!\S{10})\d+(?:\.\d{1,2})?$
See regex demo
^ - start of string
(?!\S{10}) - no more than 9 non-whitespace characters from the beginning to end condition
\d+ - 1 or more digits
(?:\.\d{1,2})? - 1 or zero groups of . + 1 or w2 digits
$ - end of string
However, you might as well just match the float/integer numbers with ^\d+(?:\.\d{1,2})?$ and then check the length of the matched text to decide whether it is valid or not.
Note that in case you have to omit leading zeros, you need to get rid of them first:
s = s.replace(/^0+/, '');
And then use the regex above.
Related
i am trying to validate a number exactly 15 digits with 5 digits group separated by "-" , ":" , "/", that should start with either 5 or 4 or 37, it also should not contain any characters.
It means
37344-37747-27744 - valid
40344-37747-27744 - valid
59344-37747-27744 - valid
37344:37747:27744 - valid
37344/37747/27744 - valid
87887-98734-83422 - Not valid
548aa:aa764:90887 - not valid
37759\29938\92093 - not valid
I was only able to go this far
\d{5}[-:\/]\d{5}[-:\/]\d{5}
Please help.
Try this one:
(((5|4)\d{4})|((37)\d{3}))[-:\/]\d{5}[-:\/]\d{5}
https://regex101.com/r/VvTq5P/1
Edit:
I would also add: \b at the beggining and at the end:
\b(((5|4)\d{4})|((37)\d{3}))[-:\/]\d{5}[-:\/]\d{5}\b
so nothing like:
12337344-37747-27744
can pass the test.
You could use look ahead (?=.) to first check if your string starts ^ with numbers 5|4|37 that is the requirement, here's full pattern:
^(?=5|4|37)\d{5}[-:\/]\d{5}[-:\/]\d{5}$
Demo here
If I understand correctly the regex is this:
((5|4)\d{4}|37\d{3})[-:\/]\d{5}[-:\/]\d{5}
Needs to be
4XXXX, 5XXXX or 37XXX
So I split it up to accept the 3 forms
((5|4)\d{4}|37\d{3})[-:\/]\d{5}[-:\/]\d{5}
(5|4)\d{4} - looks for a number that starts with either 5 or 4 and four digits afterward.
Then the or 37\d{3} looks for 37 and three digits afterward.
If the separators have to be the same:
^(?=37|[54])\d{5}([-:\/])\d{5}\1\d{5}$
Explanation
^ Start of string
(?=37|[54]) Positive lookahead, assert either 37 or 5 or 4 to the right
\d{5} Match 5 digits
([-:\/]) Capture group 1, match one of - : /
\d{5} Match 5 digits
\1 Backreference to match the same separator as in group 1
\d{5} Match 5 digits
$ End of string
See a regex101 demo.
const regex = /^(?=37|[54])\d{5}([-:\/])\d{5}\1\d{5}$/;
[
"37344-37747-27744",
"40344-37747-27744",
"59344-37747-27744",
"37344:37747:27744",
"37344/37747/27744",
"87887-98734-83422",
"548aa:aa764:90887",
"37759\\29938\\92093",
"37344-37747/27744",
"37344:37747-27744"
].forEach(s =>
console.log(`${s} --> ${regex.test(s)}`)
)
The pattern without a lookaround using an alternation:
^(?:37\d{3}|[54]\d{4})([-:\/])\d{5}\1\d{5}$
See a regex101 demo
const regex = /^(?:37\d{3}|[54]\d{4})([-:\/])\d{5}\1\d{5}$/;
[
"37344-37747-27744",
"40344-37747-27744",
"59344-37747-27744",
"37344:37747:27744",
"37344/37747/27744",
"87887-98734-83422",
"548aa:aa764:90887",
"37759\\29938\\92093",
"37344-37747/27744",
"37344:37747-27744"
].forEach(s =>
console.log(`${s} --> ${regex.test(s)}`)
)
I am using regEx /^[0-9]+(x|X){0,1}[0-9]*?$/g this is working fine for nth number of digits + x + nth number of digits.
But I want to - 10 digits + x + 5 digits.
Stackblitz Code
Example - 1234567890x12345
So please provide me regEx for above example to prevent user to enter nth number of digits.
Valid inputs:
1234567890
1234567890x1
1234567890X1
1234567890x12345
Invalid inputs:
12345678901
1234567890x123456
1234567890X123456
To make sure a user can type only a valid string in the input field you need a regex for live validation like
^[0-9]{1,10}(?:[xX][0-9]{0,5})?$
See the regex demo
Pattern details
^ - start of string
[0-9]{1,10} - one to ten digits (change to {0,10} to also allow an empty string)
(?:[xX][0-9]{0,5})? - an optional sequence of
[xX] - an x or X
[0-9]{0,5} - zero to five digits
$ - end of string.
Here you go: ^[0-9]{10}(x|X){0,1}[0-9]{5}?$
I need a regex which satisfies the following conditions.
1. Total length of string 300 characters.
2. Should start with &,-,/,# only followed by 3 or 4 alphanumeric characters
3. This above pattern can be in continuous string upto 300 characters
String example - &ACK2-ASD3#RERT...
I have tried repeating the group but unsuccessful.
(^[&//-#][A-Za-z0-9]{3,4})+
That is not working ..just matches the first set
You may validate the string first using /^(?:[&\/#-][A-Za-z0-9]{3,4})+$/ regex and checking the string length (using s.length <= 300) and then return all matches with a part of the validation regex:
var s = "&ACK2-ASD3#RERT";
var val_rx = /^(?:[&\/#-][A-Za-z0-9]{3,4})+$/;
if (val_rx.test(s) && s.length <= 300) {
console.log(s.match(/[&\/#-][A-Za-z0-9]{3,4}/g));
}
Regex details
^ - start of string
(?:[&\/#-][A-Za-z0-9]{3,4})+ - 1 or more occurrences of:
[&\/#-] - &, /, # or -
[A-Za-z0-9]{3,4} - three or four alphanumeric chars
$ - end of string.
See the regex demo.
Note the absence of g modifier with the validation regex used with RegExp#test and it must be present in the extraction regex (as we need to check the string only once, but extract multiple occurrences).
You're close. Add the lookahead: (?=.{0,300}$) to the start to make it satisfy the length requirement and do it with pure RegExp:
/(?=.{0,300}$)^([&\-#][A-Za-z0-9]{3,4})+$/.test("&ACK2-ASD3#RERT")
You can try the following regex.
const regex = /^([&\/\-#][A-Za-z0-9]{3,4}){0,300}$/g;
const str = `&ACK2-ASD3#RERT`;
if (regex.test(str)) {
console.log("Match");
}
I am using regex ^(\+91|[0]?)\d{10}$ for phone number validation. I want below output.
+911234567891 - valid
01234567891 - valid
1234567891 - valid
0123456789 - should be invalid as I want 10 digits after 0.
Please suggest changes in regex pattern
Thanks in advance
Your ^(\+91|[0]?)\d{10}$ pattern matches +91 or an optional 0 and then any 10 digits. That means any 10 digit string will pass the test. You need to make sure 10 digits are allowed after +91 or 0, or make sure the first digit is 1 to 9 and the rest is just 9 digits.
You may use
^(?:(?:\+91|0)\d{10}|[1-9]\d{9})$
See the regex demo.
Details
^ - start of string
(?:(?:\+91|0)\d{10}|[1-9]\d{9}) - 2 alternatives:
(?:\+91|0)\d{10} - +91 or 0 and then any 10 digits
| - or
[1-9]\d{9} - a digit from 1 to 9 and then any 9 digits
$ - end of string.
Say for instance, in php - and javascript flavor you can use a possessive quantifier. Demo here. Code below:
^(\+91|0?+)\d{10}$
The change is replacing [0]? with 0?+. I removed the [...] for the sake of convenience. Then, the ?+ matches one 0 and won't let it go.
Another alternative is to list all the opportunities:
^(\+91\d{10}|0\d{10}|[1-9]\d{9})$
Demo here.
Only mistake in your regex is a misplaced question mark.
^(\+91|0)?\d{10}$
You can remove the square bracket around '0' as it is a single character.
/^([+]{0,1})([0]|[9][1]){0,1}([\d]{10})$/
I want to create a RegExp validation in JavaScript for string which can have 3 parts:
P1-P2-P3
With following rules:
P1, P2 and P3 can only be digits
each part can be 1-13 digits long
entire string can not be longer then 20 characters
P2 and P3 parts are optional (meaning 34 is already a valid string, or 34-6565 or, 566-233455-23232).
Currently I have this, but I am missing the entire string length and I don't know how to define optional parts:
/^.\d{1,13}-\d{1,13}-\d{1,13}$/
Here are few valid entries: 5656, 33434-2323-45, 12-4345-12, 1234567890123-123456, 1234567890123-12-56
Invalid entries: 34453454351234566787, 1234567890123-1234567890123, 23455-233-123-3434, 34sd1322-23, 31234as, ...
You can use:
^(?=\d{1,13}(-\d{1,13}){0,2}$)[\d-]{1,20}$
Online Demo: http://regex101.com/r/sM6wQ7
Explanation:
Try this:
/(^\d{1,13}(-\d{1,13})?(-\d{1,13})?$){1,20}/
? means 0 or 1 of this, so this is useful for optional parts.
{1,20} at the end of the whole string to check it wont be longer than 20.
/^(?!.{21,})\d{1,13}(-\d{1,13}){0,2}$/
(?!.{21,}) at the beginning - negative lookahead meaning: not more than 20 characters.
The total string length check must be separated; the rest can be done with regular expressions:
var re = /^\d{1,13}(?:\-\d{1,13}){0,2}$/;
if (str.length <= 20 && str.match(re)) {
// valid
}
Try this one :
/^(?=.{0,20}$)\d{1,13}(-\d{1,13}){0,2}$/
Description :
^ beginning of the input
(?=.{0,20}$) followed by any char, 0 or 20 times, + end of the input
\d{1,13} a digit, 1 or 13 times
(-\d{1,13}){0,2} a dash + same as above, 0 or 2 times
$ end of the input
More on regular expressions : http://www.javascriptkit.com/javatutors/redev.shtml.
(\d{1,13}-){0,2}(\d{1,13})
Matches two sets of numbers of up to 13 digits of length followed by a dash, then a set of numbers of up to 13 digits of length. This approach is extensible, because you just need to change the number after the first capturing group to change the amount of number parts you want. I would recommend you checking via code if the string's length is appropriate before validating trough Regex. It will be faster and more readable.
Example: http://regex101.com/r/wI7uX1
You could also do this via Javascript, and it may be quite faster: http://jsfiddle.net/sauYY/