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}?$
Related
I have a problem with creating regexp to allow:
exactly 2 letters at the beginning
exactly 10 numbers after letters
at most 4 "-" between numbers (should allow also without it)
so example of valid string is GB123-55-22-22-6,
my current regexp is: /^([A-Z]{2})?[0-9]{10}$/. He allow GB1235522226 but I have a problem with "-".
can somoene tell me how to allow this regexp to use at most 4 "-" chars?
thanks for any help!
You can use
^(?:[A-Z]{2})?(?=(?:-?\d){10}$)[0-9]+(?:-[0-9]+){0,4}$
See the regex demo.
Details:
^ - start of string
(?:[A-Z]{2})? - optional two letters
(?=(?:-?\d){10}$) - there must be 10 digits optionally separated with a - till end of string, the string must end with a digit
[0-9]+ - one or more digits
(?:-[0-9]+){0,4} - zero to four occurrences of a hyphen and then one or more digits
$ - end of string.
I have a couple of regex which I am planning to combine.
So the first regex is as below (allows amounts with particular thousand and decimal separators)
"^-?(\\d+|\\d{1,3}(,\\d{3})*)?(\\.(\\d+)?)?$"
I have similar other regexes (based on different locales e.g. other one would have comma as the decimal separator)
So with the above regex, following are Valid/Invalid values
123.11 (Valid)
1'23 (Invalid)
With the second regex, I want that the string can contain a max of 13 digits (including before or after the decimal)
^[^\\d]*?(\\d|\\d[^\\d]+){0,13}$
With the above regex, following are Valid/Invalid values
1234567890123 (Valid - 13 digits)
12345678901234 (Invalid - 14 digits)
1234567890.123 (Valid as 13 digits...10.3)
1234567890.1234 (Invalid as 14 digits...10.4)
Is it possible to somehow consolidate the 2 regex?
However, I do not want to touch the first regex (have different combinations based on different locales). But it would be nice to somehow dynamically append the 2nd regex into the first one ?
So, I am flexible with the 2nd regex as that is not based on any locale, but is going to be the same always and mainly validates for max of 13 digits in the string.
I'll then validate my string using the consolidated regex.
You may keep the first pattern as is, and just prepend it with
(?=^\D*(?:\d\D*){0,13}$)
The (?=^\D*(?:\d\D*){0,13}$) pattern represents a positive lookahead that matches a location that is immediately followed with
^ - start of string
\D* - 0+ non-digits
(?:\d\D*){0,13} - 0 to 13 occurrences of a digit followed with a non-digit char
$ - end of string.
Full JavaScript regex definition:
var regex1 = "^-?(\\d+|\\d{1,3}(,\\d{3})*)?(\\.(\\d+)?)?$"; // Not to be touched
var consolidated_regex = "(?=^\\D*(?:\\d\\D*){0,13}$)" + regex1;
See full regex demo.
Details
I have seperate regex validations for my requirement but struggling to combine them in one.
I am validation mobile numbers with country code or starting with 00 and also if they contain extension number(2-5 digits) seperated by #
Following is the example of valid Number :
+919986040933
00919986040933
+919986040933#12
+919986040933#123
+919986040933#1234
+919986040933#12345
I have following regex to validate the above:
var phoneRegexWithPlus = "^((\\+)|(00))[0-9]{10,14}$";
var phoneRegexWithZero = "^((\\+)|(00))[0-9]{9,12}$";
var phoneRegexExtension = "^[0-9]{2,5}$";
Currently i am checking whether number contains #,if yes then split it and match number and extension part seperately where extension is comething after hash.
My problem is now that i have to create one regex combining the above three,can anyone help me with that as m not good in regex.
Thanks in advance.
I suggest this expression:
^\+?(?:00)?\d{12}(?:#\d{2,5})?$
See the regex demo
Expression explanation:
^ - start of string
\+? - an optional plus (as ? matches the + one or zero times)
(?:00)? - an optional 00
\d{12} - Exactly 12 digit string
(?:#\d{2,5})? - an optional (again, ? matches one or zero times) sequence of:
# - a literal hash symbol
\d{2,5} - 2 to 5 digits (your phoneRegexExtension)
$ - end of string.
The phoneRegexWithPlus and phoneRegexWithZero are covered with the first obligatory part \+?(?:00)?\d{12} that matches 12 to 14 digits with an optional plus symbol at the start.
NOTE: The regex is adjusted to the sample input you provided. If it is different, please adjust the limiting quantifiers {12} (that can be replaced with, say, {9,14} to match 9 to 14 occurrences of the quantified pattern).
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.
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}.