This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A comprehensive regex for phone number validation
Validate phone number with JavaScript
I'm trying to write a regular expression to validate US phone number of format
(123)123-1234 -- true
123-123-1234 -- true
every thing else in not valid.
I came up something like
^\(?([0-9]{3}\)?[-]([0-9]{3})[-]([0-9]{4})$
But this validates,
123)-123-1234
(123-123-1234
which is NOT RIGHT.
The easiest way to match both
^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$
and
^[0-9]{3}-[0-9]{3}-[0-9]{4}$
is to use alternation ((...|...)): specify them as two mostly-separate options:
^(\([0-9]{3}\)|[0-9]{3}-)[0-9]{3}-[0-9]{4}$
By the way, when Americans put the area code in parentheses, we actually put a space after that; for example, I'd write (123) 123-1234, not (123)123-1234. So you might want to write:
^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$
(Though it's probably best to explicitly demonstrate the format that you expect phone numbers to be in.)
Related
This question already has answers here:
Match exact string
(3 answers)
What is the MM/DD/YYYY regular expression and how do I use it in php?
(9 answers)
Closed 4 years ago.
I am trying to learn how to use regular expressions. Currently, I am creating my own regular expression using JavaScript to test for a date in the format of MM-DD-YYYY.
Here is my code:
// regex for testing valid date
var regex = new RegExp("[0-9]{2}\-[0-9]{2}\-[0-9]{4}");
regex.test("113-12-1995");
Unfortunately, this is outputing to true and I cannot figure out why. I am under the impression that {2} means it must be two digits and no more or less. It seems like it is behaving as if I had put a {2,} which would correlate to at least two digits, but that isn't what I want.
Additionally, how would I test to see if the value of the first two digits are greater than 12?
This question already has answers here:
Javascript date regex DD/MM/YYYY
(13 answers)
Closed 7 years ago.
I want to validate the format of the date value entered by a user using regex with javascript.
My regex doesn't allow the '/' character , /[^0-9\.]/g,''
But I want to let '/' pass the regex test too. What modification do I need to make here?
Modified from this answer you can be pretty exact with this. This works for the years 1000-9999, is Proleptic Gregorian and assumes that we won't change how leap-years work until the year 9999 ;)
^(?:(?:(?:0[1-9]|1\d|2[0-8])/(?:0[1-9]|1[0-2])|(?:29|30)/(?:0[13-9]|1[0-2])|31/(?:0[13578]|1[02]))/[1-9]\d{3}|29/02/(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00))$
Debuggex Demo
"20/11/1992".match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)
The above snippet should do, but there are too many validations to be performed on dates, so I wouldn't recommend regex.
Instead, I'd say do it like most websites do and place 3 combo boxes (dd/mm/yyyy), and allow the user to select a date, then you validate that date using the Date() constructor (if the values haven't changed, the date is correct).
note: the answer is based upon the assumption that you don't want to use any of the existing libraries (or the native validation provided by browser when using input[type="date"])
You can use this regex:
/(^(((0[1-9]|[12][0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)/
This validates the date with format dd/mm/yyyy and also checks for leap years.
It depends on how strict you need to be? I thing that simple:
/[0-3]\d\W[01]\d\W(?>19|20)\d{2}/g
should be sufficient.
day: [0-3]\d 2 digits, first 0-3, second any number \d
month: [01]\d 2 digits, first 0 or 1, second any number
year: (?>19|20)\d{2} 4 digits, starts with 19 or 20 (for 19th and 20th century) and next any digit two times {2}
Also note, I used \W to match single non-word character as workaround to match /. Are you sure that you cannot use escaped slash \/ instead?
This question already has answers here:
How to validate a date?
(11 answers)
Closed 8 years ago.
My goal is to validate a user-entered date in javascript.
I am basing my code on https://stackoverflow.com/a/6178341/3255963, but that code was designed to only validate mm/dd/yyyy.
if(!/^\d{2}\/\d{2}\/\d{4}$/.test(dateString))
return false;
I want to also allow users to enter m/d/yyyy (in other words no leading zeros).
It appears the following works but I was wondering if there is a way to do this with regex in one line.
if(
!/^\d{2}\/\d{2}\/\d{4}$/.test(dateString) &&
!/^\d{1}\/\d{1}\/\d{4}$/.test(dateString) &&
!/^\d{1}\/\d{2}\/\d{4}$/.test(dateString) &&
!/^\d{2}\/\d{1}\/\d{4}$/.test(dateString)
)
return false;
PS Another portion of the linked script verifies other aspects of the input, but I'm not modifying that part.
You can specify minimum and the maximum number of characters to be matched, like this
if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
Note: As #Matt suggested in the comments, using RegEx to validate date strings, is not a foolproof way.
This question already has answers here:
Evaluating a string as a mathematical expression in JavaScript
(26 answers)
Closed 8 years ago.
I am trying to create a simple graphing calculator where a user enters a function of f (like f(x) = x^2+2x+6). Basically the javascript replaces the x in the function with some number and then evaluates the function using eval(). The problem is, I want users to be able to type x^2 instead of default javascript which would be Math.pow(x,2). I'm guessing it's going to be some regular expression but I have little experience with them and find them really confusing, personally. Is it possible to convert a statement like x^3-x^2 to Math.pow(x,3)-Math.pow(x,2) ??
Help greatly appreciated.
You want to use a Regular Expression that looks something along the lines of
(.+)\^(.+)
This will match both selections, you then replace the instances of that string, using those matches like this.
Math.pow($1, $2)
Javascript has support for this kind of operation with the function option in String.prototype.replace
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter
1) Yes, it is possible. You can easily program it yourself if you parse what the user entered. Wherever you see x^n just turn it into Math.pow(x,n). And only then eval.
This will work for say polynomials of one variable.
2) If you want to solve this more generally (for a broader class of math functions),
you need to come up with some grammar and build an AST from the user input.
http://en.wikipedia.org/wiki/Abstract_syntax_tree
Everybody.
Several days ago an Interviewer asked me a question.
And I couldn't answer it. May be at this site exists some guru JS. =)
We have just one string: VARNAME[byte][byte][byte][byte] where [byte] is place for one char.
Question: How write JS correct, if pair of [byte][byte] in HEX MUST BE NOT MORE than 1000 in decimal?
I tried following :
1) VARNAME[20][3D][09][30] it is equal
2) VARNAME<space>=1<space> and it is correct JS CODE BUT!
3) 0x203D = 8253 in decimal not correct must be <=1000
0x0120 = 2352 not correct must be <=1000!
I tried replacing 20 on 09, then:
0x093d = 2365 it is more good, but more than 1000 =(
How i can make it? Interviewer says that it is possible because char can be any( i mean
varname;<space><space><space> and etc), but he can not say me an answer.
Who can make it guys?
The question as described has no answer.
The lowest code point that can appear in an expression context after a variable references is \u0009 which, as you point out, will result in a value greater than 1000 (>= 2304). The ECMAScript 5 specification requires JavaScript environment to generate an early error when an invalid character is encountered. The only characters legal here are a identifier continuation character or a InputElementDiv which is either Whitespace, LineTerminator, Comment, Token, and DivPunctuator, none of which allow code points in the range \u0000-\u0003 which would be required for the question to have an answer.
There are some environments that terminate parsing when a \u0000 is encountered (the C end-of-string character) but those do not conform ES5 in this respect.
The statement that JavaScript allows any character in this position is simply wrong.
This all changes if VARNAME is in a string or a regular expression, however, which can both take character in the range \u0000-\u0003. If this is the trick the interviewer is looking for I can only say that was an unfair question.
Remember, in an interview, you are interviewing the company as much, or more, than the company is interviewing you. I would have serious reservations about joining a company that considers such a question a valid question to use in an interview.