regex for specific phone number arrangement - javascript

i'm currently using this regex
"/^([0-9\(\)\/\+ \-]*)$/",
which is fine,but the problem is i am also using a masking script,
which produces this line automatically,
(___) ___-____
and it messes up my validation, what regex code can allow me to verify only this type of input from the use
(999) 999-9999
and also not accept a "blank" input field from user when entered. any length is fine, as long as it only accepts this inputs that i mentioned above.

This should work:
^\(\d{3}\)\s{0,1}\d{3}-\d{3}$
Breaking this regexp:
\(\d{3}\) matches only three numbers between brackets.
\s{0,1} matches only 0 or 1 space.
\d{3}-\d{3} matches only three numbers followed by '-' and then three other numbers.

First, when asking about regular expressions, you should always say which language or tool you are using because that affects what features are available and which characters need to be quoted with backslash. I'll assume you're asking about JavaScript based on your question's tags.
You say any length is fine. I shall take that to mean that each sequence of consecutive digits can contain any number of digits from one to infinity. I shall assume there's exactly one space and exactly one dash. On that basis, your RE is:
/^\(\d+\) \d+-\d+$/
If, as is more likely, you want to limit the lengths of the digit sequences, you would say something like:
/^\(\d{3,4}\) \d{3}-\d{1,10}$/
(three or four digits, exactly three digits, one to ten digits).
I have omitted any capturing parentheses (...) , which are a bit redundant if you're capturing the whole string ^(....)$ .
Here's a concise summary of JavaScript regex syntax:
http://www.regextester.com/jssyntax.html

Formatting and validation are two very different things. If you try to mix them, you will fail.
That being said, before performing validation you should strip all formatting characters from your string, then validate the content.
// remove everything that isn't a digit
var strippedNumber = value.replace(/\D/g, '');
if (strippedNumber.length === 10) {
// valid phone number
}

Related

AngularJS input to accept integers and special characters

<input type="text" ng-pattern="/^[0-9]*$/" id="players" name="players" ng-model="team.players"/>
I'd like the above input to accept integer numbers along with the following special characters.
. + -
How can I filter this with minimum code?
I tried some examples found on Internet, with none of them are working correctly whatsoever.
Note: My goal is to accept integer values (along with the special characters mentioned above) but not decimals.
Let us assume that want to accept the following numbers, which are all integral: 37, -10, -09, +23, 7., 7.000.
The following regex:
^[+\-]?[0-9]+(\.0*)?$
will accept an optional leading sign, one or many digits, and an optional decimal point with only trailing zeroes. It will deliberately not accept numbers without any digits before the decimal point, e.g. .00. The regex is anchored with ^ and $ to ensure that the whole string matches.
Depending on the regex dialect you may have to quote more or less characters.
I try regex, but it didn't work.
I end up with this code below,
$scope.reg.players=parseInt($scope.reg.players)
So when user put 1.2 it will convert to 1.
If user type 1. then it print invalid input error.

Limitation of input type in regex

I trying to create a Regular Expression for a password that can accept only
digits and letters (a-zA-Z).
In the password at least one digit must to be, and at least one letter. All the text between 10-12 symbols.
I created the next REGEX :
/^(?=.*[A-Za-z])(?=.*[0-9]).{10,12}$/
The problem is that any signs like $,%me,#,space also acceptable.
How can I add a rule saying any others signs accepts letters,digits are not acatbale?
Instead of using ., which is any character, use [A-Za-z0-9]:
/^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]{10,12}$/
The lookaheads will be satisfied as long as there is a single alphabetic and a single numeric character in the 10-12 list of characters, but . allows any of the other 8-10 characters be anything.
However, I would suggest you not do this because requirements on passwords are not a good thing. You should let people enter whatever password they want.
Try with this:
/^[a-zA-Z0-9]*$/
and with limit:
/^[a-zA-Z0-9]{10,12}$/

RegEx a name with special characters in javascript

I'm relative new to RegEx and I've encountered a problem. I want to regex a name. I want it to be max 100 characters, contain at least 2 alphabetic characters and it will allow the character '-'.
I have no problem to only check for alphabetic characters or both alphabetic characters and hyphen but I dont't want a name that potantially can be '---------'.
My code without check for hyphens is
var nameRegExp = /^([a-z]){2,100}$/;
An explanation for the code is appreciated as well.
Thanks!
I guess
/^(?=.*[a-z].*[a-z])[a-z-]{1,100}$/
the lookahead part (^(?=.*[a-z].*[a-z])) checks if there are at least two letters. This pattern ("start of string, followed by...") is a common way to express additional conditions in regexes.
You can limit the number of - by adding a negative assertion, as #Mike pointed out:
/^(?=.*[a-z].*[a-z])(?!(?:.*-){11,})[a-z-]{1,100}$/ // max 10 dashes
however it might be easier to write an expression that would match "good" strings instead of trying to forbid "bad" ones. For example, this
/^[a-z]+(-[a-z]+)*$/
looks like a good approximation for a "name". It allows foo and foo-bar-baz, but not the stuff like ---- or foo----bar----.
To limit the number of - you could add a negative look-ahead, where the number 3 is one more than the maximum number you want to allow
/^(?!(?:[a-z]*-){3,})(?=-*[a-z]-*[a-z])[a-z-]{2,100}$/

Excluding $ in a regex in CF

I am working through some different form validation types and I am having trouble getting all the items on my wishlist to work.
My code for my cfinput is this (works the same as a regular form input and has some canned javascript validation)
<cfinput type="Text" name="negdays"
range="0,23"
pattern="^(([^0]{1})([0-9])*|(0{1}))?$"
message="Negative Days must be a number between 0 and 23"
required="No" width="2" >
This one should, and does, exclude everything I need except the $. I am having difficulty stopping the form from accepting the $.
Another example that is similar is this one where I want a range and to keep it numeric, so I mixed the validation types
<cfinput type="text" name="achamount"
validate = "range,numeric"
range = "0,99999"
message="ACH Amount must be a range from 0 - 99999 and numeric only" >
... and it works perfect - except for one problem: a $ is allowed.
So I thought maybe I could add to it with a regex like this:
<cfinput type="text" name="achamount"
validate = "range,numeric,regex"
range = "0,99999"
pattern="^\d"
message="ACH Amount must be a range from 0 - 99999 and numeric only" >
But my pattern is of course only to limiting it to numeric, which I am already doing. I need my pattern to exclude the dollar signs. But as a special character its not behaving like the other stuff I want to get rid of.
Any ideas or suggestions? Everything I have tried either does not work or breaks all the other validation on the page.
Solution: Matching Only Numbers
You don't need to specifically exclude $ - to only allow numeric digits, you simply need to ensure every character matches \d.
To do this, you need to anchor the start and end of the regex to the start and the end of the input, which is done with the regex metacharacters ^ and $ respectively. (If you ever need to use either of these characters as literals, prefix them with a backslash.)
So for an integer between 0 and 99999 you want:
^\d{1,5}$
Matching an integer between 0 and 23 works the same way thing, but the central part of the pattern needs to be complex, to ensure you don't get 24 or above:
^(?:[03-9]|1\d?|2[0-3]?)$
The three alternatives here are:
* [03-9] matches any single digit except 1 or 2.
* 1\d? matches 1, or 1 followed by any digit.
* 2[0-3]? matches 2, or 2 followed by any digit upto 3.
The (?:..) is to ensure the ^ and $ still apply to the entire string.
(Of course, you could also just use ^\d{1,2}$ then later check if it's less than 24.)
Bonus Info: Excluding Characters
As above, you don't need to do this in this case, but if you encounter a situation where you did need to exclude $, you could do it either using a negative character class:
^[^$]{1,5}$
Or using a negative lookahead:
^(?:(?!\$).){1,5}$
This latter one is a bit more complicated, but it allows more flexibility so is useful to be aware of.
A lookahead is another form of anchor (it matches at a position, but doesn't consume the characters it matches). When used against a item that has a quantifier (the {1,5} bit) attached, you need to group both items together for it to apply correctly. (i.e. If you only did (?!\$).{1,5} the negative lookahead would only be checked for the first character, not all five.)
Note that outside of a character class $ must be escaped as \$ to prevent it's special meaning of "end of string anchor". Inside a character class it is just a regular character.
(Hopefully this explanation is clear - let me know if further information or clarification would be useful.)
Your regex ^(([^0]{1})([0-9])*|(0{1}))?$ can be simplified quite a bit. It seems that you want either a single digit preceeded by a 0 or maximum 2 digits.
Try this: ^\d{2}$
What about adding the $ to a range of characters you don't allow?
pattern="[^$]"

Regular Expression with optional elements in input string in javascript

Can anyone give me the regular expression for currency which have the following formats :
1000 - valid
1,000 - valid
1,000.00 or 1000.00 - valid.
This means, the number May or May Not contain a comma(,) separator every 3 digits.
The number May Or May Not contain a dot (.), and if it carries a dot(.) it should show atleast 1 number after the decimal place.
And lastly it should be numerical characters only. If I need to make my question clear kindly suggest.
/^\d{1,3}(?:(?:,\d{3})*|(?:\d{3})*)(?:\.\d{1,2})?$/
"Between one and three digits, then any number of groups of three digits prefixed by a comma or any number of groups of three digits not prefixed by said comma (disallowing a mix of the two kinds of groups), then an optional group of one or two digits prefixed by a dot."
Note: This regex assumes that you want to validate an entire string against the criteria outlined in your question. If you want to use it to find such numbers in a longer string, you will need to remove the ^ and $ from the beginning and end of the expression.
Something like so should work: (,?\d{3})+(\.\d{2})?. The regex will attempt to match a sequence of 3 digits precedeed by an optional comma, which is then, finally followed by an optional decimal point and 2 digits.
Please refer to this tutorial for more information.
EDIT: As per the comment below, the above regex can fail. I would recommend first using this regular expression: ^[\d][\d.,]+$ to make sure that you only have digits, thousand and decimal seperators. This regular expression will also make sure that the number starts with a digit, not with anything else. You could most likely have one regular expression which does everything, but it will most likely be quite complex.

Categories

Resources