I want to validate a form number against an array of regular expressions.
For now, I have this:
static bool isValidPhoneNumber(String input) {
final RegExp regex = new RegExp(r'^\(\d\d\d\)\d\d\d\-\d\d\d\d\d$');
return regex.hasMatch(input);
}
The above works for a number in the format (734)637-78673.
But I want to match also for formats where the country code maybe 1 or 2 digits long
(1)498-5539867, (23)938-6738983
(\d{1,3})\d{3}-\d*
The regex above should match all your phone numbers with a country code of 1-3 digits. I don't know how long the last row of digits can be so I added "*". You can replace it with the amount you need.
Related
I am trying to solve 'JavaScript Algorithms and Data Structures Projects: Telephone Number Validator' #freeCodeCamp.
I need to test if string contains 10 digits and what I've come up with returns false and I don't understand why.
console.log(/\d{10}/g.test("555-555-5555"))
If you want to do this with a single regular expression, you can use:
console.log(/^(?:\D*\d){10}\D*$/g.test("555-555-5555"))
console.log(/^(?:\D*\d){10}\D*$/g.test("555-555-55555"))
requiring the input to be composed of exactly 10 digits in addition to any number of other non-digit characters - but replacing non-digits with the empty string first would be a more intuitive and readable solution.
Here \d{10} means ten consecutive digits, not "ten digits with whatever in the middle, that's cool".
If want just 10 digits, you may want to strip non-digit data first:
let number = "555-555-5555";
// Remove all non-digit values (\D) which leaves only digits
let digits = number.replace(/\D/g, '').length;
It seems that your idea is correct, but this specific challenge has so many options for dashes [-] and parentheses [()] as inputs that there is a more efficient way to pass this.
function telephoneCheck(str) {
let phoneRegex = /^(1\s?)?(\d{3}|\(\d{3}\))[\s\-]?\d{3}[\s\-]?\d{4}$/
return phoneRegex.test(str);
}
The above is a way to complete the challenge in a single line of Regex, which can save you (or anyone else reading this in the future) a lot of time and space! Cheers
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 a quick regex verification for an input string.
This is an example string format I am trying to achieve: ABC=10:1;
What I currently have: var reg = new RegExp('(([AWT]\\w\\S=)?(\\d{0,9}:)?(\\d{0,9});)');
With the above regex I am able to get the string, however all these strings are also getting accepted...
ABC=
ABC=a:a;
ABC=#:#;
What I need: Three letter string = Any number(0-9) : Any number(0-9) ;
Unacceptable criteria (like the examples above)
Anything non-numerical after the equals sign and after the colon should be considered unacceptable.
Anything after the Semi-colon.
Anything before the Three letter string.
Any help would be appreciated!
Thank you.
Updates
According to the answers below, "^[a-zA-Z]{3}=\d+:\d+;$" works perfectly fine, however when I test it with my code, it's invalid.
Here is the code:
var reg = new RegExp("^[a-zA-Z]{3}=\d+:\d+;$");
var x = $('td').find('input')[7].value;
console.log(x); // AWT=10:15;
if (!x.match(reg)) {
return [false, "Stop"];
console.log("Invalid");
} else {
return [true, 'Success'];
console.log("Valid");
}
The above code always spits out Invalid even though I tested the regex.
I don't know whats wrong here.
Looks like your regexp could be simplified to this:
"^[a-zA-Z]{3}=\d+:\d+;$"
Where:
^ and $ - the beginning and the end of the line
[a-zA-Z] - any characters from the range (latin characters in this case)
{3} - exact number of the previous symbols (3 any latin characters in this case)
\d - any digit
+ - repeat last symbol one or more times
symbols =:; don't have to be escaped in this case
UPD:
If you need only one digit before the colon and one after the colon, then you shouldn't use + signs, just like in Cary Swoveland comment. But, according the example, there could be arbitrary length numbers here
I'm trying to know how many digits there is in a string that is essentially like a password.
For now I have this regex :
^(?=.*[0-9]{3,})([a-zA-Z0-9_/+*.-]{6,})$
It works great when their is 3 digits in a row but not when they are separated in the whole string.
I need to be able to know if there is 3 digit in strings like those :
h123dasd
1hkh/23jd
1gvbn/*2fefse-
What can I do ?
You can use this regex:
/^(?=(?:\D*\d){3,})[a-zA-Z0-9_/+*.-]{6,}$/
This will enforce 3 digits in your input that may or may not be consecutive.
RegEx Demo
No need for such a complicated regex IMO - just extract digits from the string, concat the matches, and then check the length. Something like:
str.match(/\d+/g).reduce((p, c) => p + c).length > 3;
DEMO
I have to validate some vehicle number which comes in this format
XY-01-AA-1234
XY is two alphabets
01 is two digits number
AA is one or two alphabets that may or may not be present in the string
1234 is a number between 1 to 9999
If AA is not present then the followed hypen should not be present in the string
I have tried with this :
'[A-Z]{2}-[0-9]{2}-[A-Z-]{0,2}[0-9]{1,4}'
check_vehicle_no(reg_no,'[A-Z]{2}-[0-9]{2}-[A-Z-]{0,2}[0-9]{1,4}');
function check_vehicle_no(str,expp){
return str.match(expp);
}
But it accept vehicle number in this format XY-01--1234.
ie, the accepted format for vehicle numbers are XY-01-1234 or XY-01-A-1234 or XY-01-AA-1234.
You should be using this regex:
/^[A-Z]{2}-[0-9]{2}-(?:[A-Z]{1,2}-)?[0-9]{1,4}$/
RegEx Demo
(?:[A-Z]{1,2}-)? pattern makes 1 or 2 uppercase letters followed by a hyphen optional.
To use it in your function:
function check_vehicle_no(str, expp) {
return str.match(expp);
}
Then call it as:
var re = /^[A-Z]{2}-[0-9]{2}-(?:[A-Z]{1,2}-)?[0-9]{1,4}$/;
check_vehicle_no(reg_no, re);
Note use of /.../ instead of '...' for regex literal string.
PS: It might be better to call regex.test, if you just want to validate the input.
As per your conditions,
[A-Z]{2}-[0-9]{2}-([A-Z-]{0,2}.-)?[0-9]{1,4}
This could do that trick for checking vehicle numbers