Am trying to validate a mobile number 254777123456 against a regex /^((254|255)[0-9]+){9,15}$/, the mobile number should be prefixed with the country codes specified but the total length of the mobile number should not be more than 15 characters, doing this via javascript am getting null, can anyone point out what am doing wrong.
PS. Am using way more country codes than the ones I specified, I just put those two as a test before I add the others because they will all be separated by the pipe.
Your regex ^((254|255)[0-9]+){9,15}$ means, that pick at least 4 digits (of which first 3 should be either 254 or 255) and whole of them must occur at least 9 times to max 15 times, which will mean the minimum length of string that will match should be of 36 characters. Which obviously you don't want. Your regex needs little correction where you need to take [0-9] part out and have {9,12} quantifier separately. Correct regex to be used should be this,
^(?:(?:254|255)[0-9]{9,12})$
This regex will match 254 or 255 separately and will restrict remaining number to match from 9 to 12 (as you want max number to be matched of length 15 where 3 numbers we have already separated out)
Demo
var nums = ['254777123456','255777123456','255777123456123','2557771234561231']
for (n of nums) {
console.log(n + " --> " + /^(?:(?:254|255)[0-9]{9,12})$/g.test(n));
}
Related
I'm evaluating a phone number in js with this regex:
/[a-z]/i.test(this.state.phone)
Now I need to limit its length to 30 chars.
I tried so many times but basically I know I need to specify max lenght in curly braces like this:
/[a-z]{30}/i.test(this.state.phone)
But this way the check about letters doesn't work anymore.
I've spended too much time on it I need some help!
EDIT: to clarify I need to avoid any letter (upper or lowercase) or special char but round brackets, space, dot, minus and plus sign.
So this is ok:
+001.333 123456
this not
+001 333 123456v
Now I need to limit its length to 30 chars
/[a-z]{30}/i will check for exact 30 characters, you need to specify minimum length as well.
/\d{1,30}/i
{1,30} will check for min 1 and max 30 characters.
Also, if there no other characters allowed in this.state.phone, then asset start of string ^ and end of string $ as well.
/^(\+)?[0-9\s]{1,30}$/
I want regex which finds out continues max 12 digits long number by ignoring space, plus (+), parenthesis & dash, e.g:
Primary contact number +91 98333332343 call me on this
My number is +91-983 333 32343
2nd number +1 (983) 333 32343, call me
Another one 983-333-32343
One more +91(983)-333-32343 that's all
121 street pin code 421 728 & number is 9833636363
Currently, I have a regex, which does the job of fetching contact numbers from string:
/* This only work for the first case not for any other
and for last one it outputs "121" */
\\+?\\(?\\d*\\)? ?\\(?\\d+\\)?\\d*([\\s./-]?\\d{2,})+
So what can be done here to support all the above cases, in short ignoring special characters and length should range from 10-12.
I see that there are numbers ranging from 10 to 13 digits.
You may use
/(?:[-+() ]*\d){10,13}/g
See the regex demo.
Details:
(?:[-+() ]*\d){10,13} - match 10 to 13 sequences of:
[-+() ]* - zero or more characters that are either -, +, (, ), or a space
\d - a digit
var re = /(?:[-+() ]*\d){10,13}/gm;
var str = 'Primary contact number +91 98333332343 call me on this\nMy number is +91-983 333 32343\n2nd number +1 (983) 333 32343, call me\nAnother one 983-333-32343\nOne more +91(983)-333-32343 that\'s all\n121 street pin code 421 728 & number is 9833636363';
var res = str.match(re).map(function(s){return s.trim();});
console.log(res);
The accepted answer will match your criteria but I'd like to propose a more restrictive approach. It is quite specific to the number formats you provided :
test specifically if a string IS a number /^(\+(\d{1,2})[- ]?)?(\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4,5}$/
test whether a string contains at least one number : /(\+(\d{1,2})[- ]?)?(\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4,5}/
I made you a small fiddle where you can try out different regexes on any number of... well numbers : https://jsfiddle.net/u51xrcox/5/.
have fun.
I have a requirement to validate some inputs which should be in format ###.##
Invalid inputs are:
-11.10 ==> no negative
000.00 or 0 ==> 0 not allowed should be positive
Valid inputs are:
1
11
111
1.1
11.11
111.11
I have tried with the following regex ^([^-]\d{0,2}(.\d{1,2})?)$ which fulfills my requirements except it's accepting 0 which I don't want. How I can modify my regex so only 0's do not get matched?
Thanks For Help
Try
^(?=.*[1-9])\d{1,3}(?:\.\d\d?)?$
It should do it for you.
It starts with a positive look-ahead to make sure there's a digit other than 0 present.
Then it matches 1-3 digits, optionally followed by a . and 1-2 digits.
Your regex101 updated.
([0-9]){1,3}(\.[0-9]{1,2})? is the expression you are searching for.
([0-9]){1,3} = any number sequence with a length from 1 up to 3
(\.[0-9]{1,2})? = "?" maybe there is a float tail
(\.[0-9]{1,2}) = float tail must start with a dot and decimal numbers have to be up to 2
There is a way to except all zero sequences but it will waste your time for no reason, as you can simply check it with if myNum > 0.
It will work for negative values also, but this regex excludes them too.
^[1-9][0-9]*(\.[0-9]+)?$|^0\.[0-9]+$
This will work for you. It accepts all valid positive decimal numbers excluding 0.
I'm looking for a regular expression in javascript that takes care of this:
Accept only numbers between 6 and 15 digits, 6 is the minimum.
Numbers cannot contain groups of repeated digits, such as 408408 or 123123
Numbers cannot contain only two different digits, such as 121212
I started with this, then I was loss
^[0-9]{6,15}$
Instead of just a Regex, use a combination of if-statements and a RegEx.
function validateNumber() {
var numbers = document.getElementById('numbers1').value;
if (numbers && !isNaN(numbers)) {
// make sure a that something was entered and that it is a number
if (numbers.length < 6 || numbers.length > 15) {
// make sure number is between 6 and 15 digits
alert('Number must be between 6 and 15 digits long.');
} else if (numbers.match(/(.+)\1+/)) {
// make sure that the numbers contain no repeated digits
alert('Number cannot be repeated.');
} else {
alert('Number validated!');
// otherwise, the number is validated
}
} else {
// if no number was entered
alert('Please enter a number.');
}
}
<input type="text" placeholder="enter numbers" id="numbers1" />
<input type="button" value="Validate!" onclick="validateNumber()" />
You have the first rule correct:
^\d{6,15}$
That covers both the 6-15 length requirement and the fact that it has to be numeric.
With the next rule, it's easier to test for repeated substrings than to test for their absence:
(.+)\1
The last one is a lot more complicated. Here's how you test for at least 3 distinct characters:
(.+)(?!\1)(.+)(?!\1|\2).
Put it all together and what do you get:
^(?=\d{6,15}$)(?!.*(.+)\1)(.+)(?!\2)(.+)(?!\2|\3).+$
That answers your question as written, but as I said in the comments, you should consider very carefully whether you're starting from the right assumptions. You don't have to use regex for this, nor do you have to do it all in one regex. Will the pattern above be easy for you to work with when you come back in 6 months and have to change the rules?
More importantly, if you're trying to make sure users pick a strong password, the rules you're using will weaken your security by reducing the number of possible choices. And the maximum length of 15 characters suggests you're storing passwords in plain text. You should be hashing them.
Here's my attempt, just for the challenge:
^(?!\d*?(\d+?)\1)\d{6,15}$
Demo
The (?!\d*?(\d+?)\1) part will make sure there are no groups of repeated digits by matching a group of digits and trying to match the same digits immediately after. If it finds one, the match fails.
If you want to allow two same consecutive digits, replace (?!\d*?(\d+?)\1) with (?!\d*?(\d{2,}?)\1)(?!(\d)\2*(\d)(?:\2|\3)*$). This will then make sure there are more than 2 different digits by matching a series of one digit, then a different digit followed by a series of a combination of both digits. If it reaches the end of the string it forces the match to fail.
But it'll be probably more maintainable to just do it the conventional way, without regex.
I have a list of phone numbers which are formatted in multiple ways such as: (212)-555-1234 or 212-555-1234 or 2125551234.
Using JavaScript, what would be the best way to extract only the area code out of these strings?
First, remove everything that is not a digit to get the plain number. Then, get the first three digits via slicing:
return myString.replace(/\D/g,'').substr(0, 3);
Get the first 3 consecutive digits...
/[0-9]{3}/.exec("(212)-555-1234")[0]
Sample (fiddle):
console.log(/[0-9]{3}/.exec("(212)-555-1234")[0]); // 212
console.log(/[0-9]{3}/.exec("212-555-1234")[0]); // 212
console.log(/[0-9]{3}/.exec("2125551234")[0]); // 212
Take the first 3 digits of a 10 digit number, or the first 3 digits after the 1 of an 11 digit number starting with 1. This assumes your domain is U.S. phone numbers.
You can also use my library.
https://github.com/Gilshallem/phoneparser
Example
parsePhone("12025550104");
result: { countryCode:1, areaCode:202, number:5550104, countryISOCode:"US" }
Regex as '^\(*(\d{3})' should do it. Get the first group from the match.
Here ^ will start the match from beginning, \d{3} will match 3 digits. \(* will match the optional starting parenthesis. You don't need to care about next digit or symbols after the area code.