Greetings overflowers,
I'm trying to write a regular expression to validate phone numbers of the form ########## (10 digits)
i.e. this is these are cases that would be valid: 1231231234 or 1111111111. Invalid cases would be strings of digits that are less than 10 digits or more than 10 digits.
The expression that I have so far is this:
"\d{10}"
Unfortunately, it does not properly validate if the string is 11+ digits long.
Does anyone know of an expression to achieve this task?
You need to use ancors, i.e.
/^\d{10}$/
You need to anchor the start and the end too
/^\d{10}$/
This matches 10 digits and nothing else.
This expression work for google form 10 digit phone number like below:
(123) 123 1234 or 123-123-1234 or 123123124
(\W|^)[(]{0,1}\d{3}[)]{0,1}[\s-]{0,1}\d{3}[\s-]{0,1}\d{4}(\W|$)
I included the option to use dashes (xxx-xxx-xxxx) for a better user experience (assuming this is your site):
var regex = /^\d{3}-?\d{3}-?\d{4}$/g
window.alert(regex.test('1234567890'));
http://jsfiddle.net/bh4ux/279/
I usually use
phone_number.match(/^[\(\)\s\-\+\d]{10,17}$/)
To be able to accept phone numbers in formats 12345678, 1234-5678, +12 345-678-93 or (61) 8383-3939 there's no real convention for people entering phone numbers around the world. Hence if you don't have to validate phone numbers per country, this should mostly work. The limit of 17 is there to stop people from entering two many useless hyphens and characters.
In addition to that, you could remove all white-space, hyphens and plus and count the characters to make sure it's 10 or more.
var pureNumber = phone_number.replace(/\D/g, "");
A complete solution is a combination of the two
var pureNumber = phone_number.replace(/\D/g, "");
var isValid = pureNumber.length >= 10 && phone_number.match(/^[\(\)\s\-\+\d]{10,17}$/) ;
Or this (which will remove non-digit characters from the string)
var phoneNumber = "(07) 1234-5678";
phoneNumber = phoneNumber.replace(/\D/g,'');
if (phoneNumber.length == 10) {
alert(phoneNumber + ' contains 10 digits');
}
else {
alert(phoneNumber + ' does not contain 10 digits');
}
Related
Not able to match the regular expression in my JavaScript code which I have written for form validation.
I wanted to validate my form field which is password using RegExp [[0-9]{0,8}[a-z]{0,8}[A-Z]{1,8}#]
My Validations on password is
- Should contain 10 characters including digit
- At least one uppercase letter should be there
- Only # should be used as special character
But the same is working with [0-9a-zA-Z#]{10} but not with [[0-9]{0,8}[a-z]{0,8}[A-Z]{1,8}#]
var regexpassword=/[[0-9]{0,8}[a-z]{0,8}[A-Z]{1,8}#]/
if(!regexpassword.test(password.value)){
alert("Enter valid password")
password.focus();
return false
}
NOTE: The password that I have entered is Welcome#67
It should not give the alert as "Enter valid password"
Best I can tell, the regex you provided, is matching exactly 1 character. the [] operator indicates "any of what is inside". But the only place you are indicating "multiple times" is the [A-Z]{1,8}. Also, as #Pointy mentioned, I don't think you can nest square brackets. Even if you can, it is somewhat redundant.
Your regex is being interpreted as follows:
1. Look for [ or the numbers 0 through 9 between 0 and 8 times in a row
2. Followed precisely by the lowercase letters a through z between 0 and 8 times in a row
3. Followed precisely by the uppercase letters A through Z between 1 and 8 times in a row
4. Followed precisely by a single #
5. Followed precisely by a single ]
This leads to matching strings like (but not limited to):
[A#]
0A#]
9aaaaaaaZ#]
[0123456abcdefghABCDEFGH#]
[[[[[[[[Q#]
[[[[[[[[azazazazAZAZAZAZ#]
but it will not match Welcome#67.
Is there a way to write a regex that will validate a password with your requirements?
Possibly.
Should you use a single regex to validate your password?
Probably not as the necessary complexity of that regex would make it impractical to maintain when your password requirements change.
Is there a practical, maintainable way to validate passwords?
Certainly! Use multiple regexes to validate the required parts of the password.
Then determine if the needed parts are present and make sure the length is acceptable.
Example:
var hasOnlyValidCharacters = /^[0-9a-zA-Z#]+$/gm.test(password.value);
var hasDigits = /[0-9]+/gm.test(password.value);
var hasUpper = /[A-Z]+/gm.test(password.value);
var hasLower = /[a-z]+/gm.test(password.value);
var hasAtSign = /[#]+/gm.test(password.value); // Technically could be /#+/gm.test(...), but I tend to use character classes every time I'm looking for specific characters.
var isValidPassword = (
password.value.length === 10 // Should contain 10 characters
&& hasOnlyValidCharacters
&& hasDigits // including digit
&& hasUpper // At least one uppercase letter should be there
// && hasLower // Uncomment to require at least one lowercase letter
// && hasAtSign // Uncomment to require at least one #
);
if (!isValidPassword) {
alert("Enter valid password")
password.focus();
return false
}
The [untested code] above should do the trick, and following the patterns established in it, you should be able to easily change your password requirements on a whim.
I am attempting to setup a regex verification for a user to enter a Brazilian telephone number. The formats I would need it to accept (or as close as possible) would be:
22-22222-2222 22222222222 (22) 22222 2222 (22)-22222-2222 22 22222 2222 (22)222222222
So it needs to accept numbers, spaces, dashes, parentheses only. Any advice on this would be greatly appreciated!
I just made a currently valid regex for Brazilian numbers:
^\s*(\d{2}|\d{0})[-. ]?(\d{5}|\d{4})[-. ]?(\d{4})[-. ]?\s*$
Check out the validation at https://regex101.com/r/safMl7/2
Will not get the country code, since we know it is from Brazil: +55.
Will work with the following examples, with or without spaces and dashes:
12 1234 1234
12 12345 1234
12345 1234
1234 1234
Each group of the regex will be divided by the correct brazilian grouping.
See this example: 62 98345 1234
If you always have the area code (DDD), try a simplified version of Frederiko's regular expression.
^(\d{2})\D*(\d{5}|\d{4})\D*(\d{4})$
Check out at https://regex101.com/r/M1DrBo.
What about this: \(?\d{2,}\)?[ -]?\d{4,}[\-\s]?\d{4}
Mask for brasil Phone using javascript
regex for format 11 digits: /(\d{2})(\d{1})(\d{4})(\d{4})/, "($1) $2 $3-$4
before no regex : 63991017575
After aply regex: (63) 9 9101-7575
EXAMPLE 1 USING THE JAVASCRIPT:
var number = '63992017738';
number = number.toString().replace(/(\d{2})(\d{1})(\d{4})(\d{4})/, "($1) $2 $3-$4")
Result: (63) 9 9201-7738
EXAMPLE 2 USING THE JAVASCRIPT:
var number = '63992017738';
number = number.toString().replace(/(\d{2})(\d{1})(\d{4})(\d{4})/, "$1 $2 $3-$4")
Result: 63 9 9101-7575
EXAMPLE 3 USING THE JAVASCRIPT:
now + country code
//if you need this: +55 (63) 9 9201-7131
var number = '5563992017131';
number = number.toString().replace(/(\d{2})(\d{2})(\d{1})(\d{4})(\d{4})/, "+$1 ($2) $3 $4-$5")
Result: +55 (63) 9 9201-7131
My answer came in the form of using the Jquery mask plugin. Was exactly what I needed and has much more functionality.
https://igorescobar.github.io/jQuery-Mask-Plugin/
$(document).ready(function () {
$('.input-telephone').mask('(99) 9999-9999?9');
});
The following use of Regex seems to catch around 20 of the most common ways to write a Brazilian phone number. Fix and mobile numbers included.
(\b\(\d{2}\)\s?[9]?\s?\d{4}(\-|\s)?\d\d{4})|(\b\d{2}\s?[9]?\s?\d{4}(\-|\s)?\d{4})|(\b([9]|[9]\s)?\d{4}(\-|\s)?\d{4})|(\b\d{4}(\-|\s)?\d{4})
There are four "or" options to match a number. It goes from the more complex one (with parenthesis and DDD code) to the most simple one (i. e. eight digit phone number).
I added the \b in front and not in the end of each case because sometimes you can catch cases where people write a number followed by some description: "99000-1100word". You can remove the word boundaries as you want, though:
(\(\d{2}\)\s?[9]?\s?\d{4}(\-|\s)?\d\d{4})|(\d{2}\s?[9]?\s?\d{4}(\-|\s)?\d{4})|(([9]|[9]\s)?\d{4}(\-|\s)?\d{4})|(\d{4}(\-|\s)?\d{4})
This is the simplier
regex for 10 digits format: (\d{2})(\d{4})(\d{4})
regex for 11 digits format: (\d{2})(\d{5})(\d{4})
Therefore, you could write a function to regex and replace:
export function phoneFormatter10(phone) {
phone = phone.replace(/[^\d]/g, ""); //remove all non digits
return phone.replace(/(\d{2})(\d{4})(\d{4})/, "($1)$2-$3");
}
export function phoneFormatter11(phone) {
phone = phone.replace(/[^\d]/g, ""); //remove all non digits
return phone.replace(/(\d{2})(\d{5})(\d{4})/, "($1)$2-$3");
}
Examples of use:
phoneFormatter10('3499883424')
result: '(34)9988-3424'
phoneFormatter11('3499^883*4-244') //even with badly formatted string
result: '(34)99883-4244'
I'm extracting the phone numbers that begin with 9 followed by other 9 digits from tweets using JavaScript.
Here's the regex pattern I am using:
var numberPattern = /^9[0-9]{9}/;
Here's the pattern matching phase:
var numberstring = JSON.stringify(data[i].text);
if(numberPattern.test(data[i].text.toString()) == true){
var obj={
tweet : {
status : data[i].text
},
phone : numberstring.match(numberPattern)
}
//console.log(numberstring.match(numberPattern));
stringarray.push(obj);
The problem is it is working for few numbers and not all. Also, I want to modify the regex to accept +91 prefix to numbers as well and(or) reject a starting 0 in numbers. I'm a beginner in regex, so help is needed. Thanks.
Example:
#Chennai O-ve blood for #arun_scribbles 's friend's father surgery in few days. Pl call 9445866298. 15May. via #arun_scribbles
Your regex pattern seems to be designed to allow a 9 or 8 at the beginning, but it would be better to enclose that choice in parentheses: /^(9|8)[0-9]{9}/.
To allow an optional "+" at the beginning, follow it with a question mark to make it optional: /^\+?(9|8)[0-9]{9}/.
To allow any character except "0", replace the (9|8) with a construct to accept only 1-9: /^\+?[1-9][0-9]{9}/.
And in your example, the phone number doesn't come at the beginning of the line, so the caret will not find it. If you're looking for content in the middle of the line, you'll need to drop the caret: /\+?[1-9][0-9]{9}/.
var numberPattern = /([+]91)?9[0-9]{9}\b/;
Try this regex pattern: [\+]?[0-9]{1,4}[\s]?[0-9]{10}
It accepts any country code with +, a space, and then 10 digit number.
While i type the phone number in a field after 5 digits put a hypen and continue the digits
after hypen. It allows only 10 digits and hypen.
Output : 90000-00000
Can any one help me how to fix the issue.
Thanks
The ReGex for this is really simple, you should have been able to Google this and work it out for yourself. The RegEx you want is as follows:
^[\d]{5}-[\d]{5}$
Using that with javascript:
var input = "90000-00000";
var valid = /^[\d]{5}-[\d]{5}$/.test(input);
//valid is a boolean: true or false
Here is a working example
I need a Javascript RegEx through which I can validate phone number. RegEx should handle following criteria
It should only consist of numbers ( ) + and -
Count of + should not exceed 1
Count of - should not exceed 4
There must be only one pair of ()
If '(' is present in phone number then ')' must be present.
Thanks for the help!
Hussain.
Try this:
function valid_phone_number(ph) {
var regex = /^(?!([^-]*-){5})(\+\d+)?\s*(\(\d+\))?[- \d]+$/gi;
return regex.test(ph);
}
I'm new to regular expressions, so please be nice. :-)