I want users to allow only phone numbers in following format
xxx-xxx-xxxx or xxxxxxxxxx all digits only . Can some one suggest a regular expression to do this ?
Something like this:
\d{3}-\d{3}-\d{4}|\d{10}
While general phone number validation is a larger problem than what you're trying to solve, I'd do the following:
var targ=phone_number_to_validate.replace(/[^\d]/g,''); // remove all non-digits
if(targ && targ.length===10) {
// targ is a valid phone number
}
Doing it this way will validate all of the following forms:
xxxxxxxxxx
xxx-xxx-xxxx
(xxx) xxx-xxxx
etc.
Also, to trivially check for a valid U.S. area code, you can use:
if(targ.matches(/^[2-9]\d{2}/)) // targ is a valid area code
Again, this is a trivial check. For something a little more rigorous, see this List of Legal US Area Codes.
See also A comprehensive regex for phone number validation
Use this :
/^(1-?)?(([2-9]\d{2})|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/
var rx=/^\d{3}\-?\d{3}\-?\d{4}$/;
if(rx.test(string)){
//10 diget number with possible area and exhange hyphens
}
else //not
The following regular expression can be used to validate defined and operational (as of 6/2011) telephone area codes within the U.S.:
var area_code_RE=
'(2(?:0[1-35-9]|1[02-9]|2[4589]|3[1469]|4[08]|5[1-46]|6[0279]|7[068]|8[13])'+
'|3(?:0[1-57-9]|1[02-9]|2[0139]|3[014679]|47|5[12]|6[019]|8[056])'+
'|4(?:0[126-9]|1[02-579]|2[3-5]|3[0245]|4[023]|6[49]|7[0589]|8[04])'+
'|5(?:0[1-57-9]|1[0235-8]|[23]0|4[01]|5[179]|6[1-47]|7[01234]|8[056])'+
'|6(?:0[1-35-9]|1[024-9]|2[0368]|3[016]|4[16]|5[01]|6[012]|7[89]|8[29])'+
'|7(?:0[1-4678]|1[2-9]|2[047]|3[1247]|4[07]|5[47]|6[02359]|7[02-59]|8[156])'+
'|8(?:0[1-8]|1[02-8]|28|3[0-25]|4[3578]|5[06-9]|6[02-5]|7[028])'+
'|9(?:0[1346-9]|1[02-9]|2[0578]|3[15679]|4[0179]|5[124679]|7[1-35689]|8[0459])'+
')';
Related
I'm writing an application that requires color manipulation, and I want to know when the user has entered a valid hex value. This includes both '#ffffff' and '#fff', but not the ones in between, like 4 or 5 Fs. My question is, can I write a regex that determines if a character is present a set amount of times or another exact amount of times?
What I tried was mutating the:
/#(\d|\w){3}{6}/
Regular expression to this:
/#(\d|\w){3|6}/
Obviously this didn't work. I realize I could write:
/(#(\d|\w){3})|(#(\d|\w){6})/
However I'm hoping for something that looks better.
The shortest I could come up with:
/#([\da-f]{3}){1,2}/i
I.e. # followed by one or two groups of three hexadecimal digits.
You can use this regex:
/#[a-f\d]{3}(?:[a-f\d]{3})?\b/i
This will allow #<3 hex-digits> or #<6 hex-digits> inputs. \b in the end is for word boundary.
RegEx Demo
I had to find a pattern for this myself today but I also needed to include the extra flag for transparency (i.e. #FFF5 / #FFFFFF55). Which made things a little more complicated as the valid combinations goes up a little.
In case it's of any use, here's what I came up with:
var inputs = [
"#12", // Invalid
"#123", // Valid
"#1234", // Valid
"#12345", // Invalid
"#123456", // Valid
"#1234567", // Invalid
"#12345678", // Valid
"#123456789" // Invalid
];
var regex = /(^\#(([\da-f]){3}){1,2}$)|(^\#(([\da-f]){4}){1,2}$)/i;
inputs.forEach((itm, ind, arr) => console.log(itm, (regex.test(itm) ? "valid" : "-")));
Which should return:
#123 valid
#1234 valid
#12345 -
#123456 valid
#1234567 -
#12345678 valid
#123456789 -
How would I format a number like: (99) 9999-9999 into: 9999999999 using angularjs? someone told me to use phoneformat.js but I don't know how to implement it in my project
I'm not sure that you need anything special from Angular or any special libraries . . . just use the basic JS .replace() method and a little regex:
var sPhoneNum = "(99) 9999-9999";
var sFormattedPhoneNum = sPhoneNum.replace(/\D/g, "");
// sFormattedPhoneNum equals "9999999999"
The regular expression /\D/g matches all non-numeric characters, so it will strip out everything but the numbers.
so like talemyn said... the solution is simply to remove the unwanted char... the angular way to do it is via filter I guess... this is a jsfillde
with an example...
myApp.filter('phoneToNum', function() {
return function(input, scope) {
return input.replace(/\D/g, "");
}
});
now if you also want to revert it... use
phone filter
Try this:
formatLocal('US', phoneNumber)
I had an input field where I needed to get phone number from users but only the digits so that it's easy to index using phone number in the database. I used .replace on ng-keyup, so the non-digit characters gets removed as the user types.
in html
<input ng-keyup="formatNum()" ng-model='data.phone' placeholder="Phone Number" />
in controller
$scope.formatNum = function() {
if ($scope.data.phone)
$scope.data.phone = $scope.data.phone.replace(/\D/g, "");
};
I'm trying to auto format an input on HTML with javascript, and it is almost done, i need the format to be xxx-xxx-xxxx but and I have this code
this.value = this.value.replace(/(\d{3})\-?/g,'$1-');
as youy can see it will just auto format xxx-xxx-xxx but I need to be 4 digits at the end
any sugestions?
Try this regexp:
'1234567890'.replace(/(\d{3}(?!\d?$))\-?/g, '$1-'); // 123-456-7890
The part (?!\d?$) is a negative lookahead. It allows regular expression to match three digits only if they are not followed by another number (4th) (but not necessarily ?) at the end of the string $.
So it will not match 789 group because it's followed by 0$.
Or simply : .replace(/(\d{3})(\d{3})(\d{4})\-?/g,'$1-$2-$3');
Sample code to help you out:
var phone = '(555) 666-7777';
// First clean your input
phone = phone.replace(/[^\d]/g, '');
// Check the length of the phone
if(phone.length == 10){
// Now we can format the phone
phone = phone.substring(0,3)+'-'+phone.substring(3,6)+'-'+phone.substring(6, 10);
// Optionally
//phone = phone.replace(/(\d{3})(\d{3})/, '$1-$2-');
}
else {
// whatever you want to tell the user
}
there seems to be a bug in the "replace" regex's above. enter a 10 digit number and you see it formatted correctly (e.g. "(123) 456-7890"). Then, select the contents of the field (double or triple click it) and overtype the value. You'll see the first two digits get transposed. So if you enter 1 2 3, you'll see 2 1 3...
I am using a regular expression to validate input from a text field to only allow a whole number or a number with up to two decimal places (eg: 10, 10.4, 10.45, 100.45) ,however when I enter a number with 3 or more decimal places it will still validate it. Code shown below.
var loanAmount = document.getElementById("loan_amount");
var loanRE = /\d+(\.\d{1,2})?/;
if (!(loanRE.test(loanAmount.value))){
alert("Not a valid input for the loan amount");
return false;
}
Everything looks good to me. What am I missing?
Anchor your regex. loadRE = /^\d+(\.\d{1,2})?$/
Otherwise it will just say "okay, there's some digits. It passes!"
Can anybody advise me how to make a validation rule for Dutch bank accounts?
So far i could only found this on web:
regex = /[0-9]{2}[\\s]{1}[0-9]{2}[\\s]{1}[0-9]{2}[\\s]{1}[0-9]{3}/;
This is my JavaScript:
function dutchBankAccount(input) {
var regex = /[0-9]{2}[\\s]{1}[0-9]{2}[\\s]{1}[0-9]{2}[\\s]{1}[0-9]{3}/;
if(input.value.toString().match(regex) && !(input.value == "")) {
return true;
} else {
input.click();
input.style.border = '2px solid #F20056';
return false;
}
}
And here is my HTML code:
<li><input type="text" id="anum" placeholder="Account Number" autocomplete="off" onkeypress="return isNumberKey(event)" onBlur="isValidAnum()" onFocus="emptyAnum('anum')"/></li>
Later on when I enter a dutch bank account I get error which I'm not supposed to get. So if you know how to solve this please help me.
The regex syntax is incorrect. Try something more like this:
var regex = /[0-9]{2}\s[0-9]{2}\s[0-9]{2}\s[0-9]{3}/;
That matches strings like
32 01 28 192
Two digits followed by a space three times, then three digits. Whether that's what all Dutch bank accounts look like I don't know, though that seems like a small namespace for something like that.
(It occurs to me that /(?:\d{2}\s){3}\d{3}/ should match the same strings and it's a little shorter.)
edit — To elaborate, the regex in the original code has some problems:
The backslashe before each of the "\s" (space) characters is doubled, but it should not be. (That's assuming that Dutch bank account numbers don't actually look like "92\s31\28s\120")
Putting a single character class shortcut ("\s") in square brackets is needlessly redundant
Suffixing a regex element with "{1}" is needlessly redundant too
The real problem was the extra backslash. Also, speaking of needlessly redundant, there's no need to call ".toString()" on the value of an input element "value" attribute, and there's no need to make sure the value isn't the empty string if it has matched the pattern. In this case, an empty string cannot match the pattern, so that test is not necessary. Finally (promise), if you're just testing a regex against a string, the ".test()" method on the RegExp prototype is a little more efficient:
if (regex.test(input.value)) { // matched
// ...
}