I'm using form validator in order to validate some fields on my signup form. One of which is a telephone field for which I have prepared two regexes that will provide validation.
regexp: {
regexp:
>/(^01|^02|^071|^073|^074|^075|^076|^077|^078|^079)/,
/^((?!(012345|123456|234567|345678|456789|0{6,}|1{6,}|2{6,}|3{6,}|4{6,}|5{6,}|6{6,}|7{6,}|8{6,}|9{6,})).)*$/
message: 'The phone number is not valid'
}
My issue is that only one of those two will work, not both. Any idea how I could modify this such that they both work?
This regex will work, but I'd still suggest maybe finding a different way or set of criteria to validate. It's up to you though.
/^(0(?:[12]|7[145789]))(?!012345|123456|234567|345678|456789|0{6,}|1{6,}|2{6,}|3{6,}|4{6,}5{6,}|6{6,}|7{6,}|8{6,}|9{6,})/
This will ensure the number begins with the proper prefix and does not contain the sequences you've indicated.
You could combine the two like this:
/^(?=(01|02|071|073|074|075|076|077|078|079))((?!(012345|123456|234567|345678|456789|0{6,}|1{6,}|2{6,}|3{6,}|4{6,}|5{6,}|6{6,}|7{6,}|8{6,}|9{6,})).)*$/
Does seem like a kind of obfuscated way to perform some fairly simple checks to me, but to each their own.
Related
Is there an easy way to get the opposite of a regex or do I need to build a new regex that produces the opposite of what I have?
For example, I use this regex to make sure I'm getting proper currency values -- without $ sign.
/^[0-9]+(\.[0-9][0-9]?)?$/
I want to now remove everything that falls outside this pattern. For example, if user enters letters or tries to enter two periods e.g. 7.50.6, I want to remove undesired characters. How do I do that?
I think you're going at this in the wrong way. First of all, trying to hide input error in such a way is a bad idea. If a user has to type a number and they put an extra dot, what tells you which is the good part and which is the bad? You're better off telling the user there's something wrong with their input.
But typically, you use a regex by specifying what it has to look like AND what are the significant portions you want to keep using capture groups.
This is a capture group: ([a-z0-9])#example.com; this is a non-capture group: (?:hello|hi).
In case of a phone number, all that matters are the digits, so you can capture them and accept multiple forms of in-between characters. Here's a simple one for a postal code:
([A-Z][0-9][A-Z]) ?([0-9][A-Z][0-9])
Then all you have to do is combine the captured groups. If present, the space won't be captured.
Find more examples on MDN.
I wanted to make a really easy JavaScript validation for the IBAN.
It is for a project on school what means the goal of the validation isn't to get a 100% good IBAN validation but something easy to get along with.
I tried to create my own:/^[A-Z]{2}+[0-9A-Z]*$/
But apparently it seems to disactivate all the Javascript in the same file.
What is the reason that this disactivates all my JavaScript, and what is a good validation?
The conditions of the validation (might it not be clear already):
The first two characters must be alphabetic and upper-case.
The other characters can be numeric and/or alphabetic.
The length doesn't have to be included because that is checked with another if-statement in my function.
That's because you're using two-quantifiers side-by-side
/^[A-Z]{2}+[0-9A-Z]*$/
// ^ Remove this. It means match the previous token one or more times
So, it would be /^[A-Z]{2}[0-9A-Z]*$/
I have a field called price, now how to validate this field by java script? It will take only integer value and dot. It may take decimal point or not.
e.g 200,200.00 both are valid.
How shall I do that?
There are many ways to achieve this.
Without the use of any 3rd party libraries i would recommend using regular expressions.
A possible pattern used to validate price would be
\d[\d\,\.]+
here is a tutorial on using regular expressions to validate fields in java-script
Tutorial
Hope this helps
I am trying to get this Regex statement to work
^([_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})+(\s?[,]\s?|$))+$
for a string of comma separated emails in a textbox using jQuery('#textbox').val(); which passes the values into the Regex statement to find errors for a string like:
"test#test.com, test1#test.com,test2#test.com"
But for some reason it is returning an error. I tried running it through http://regexpal.com/ but i'm unsure ?
NB: This is just a basic client-side test. I validate emails via the MailClass on the server-side using .NET4.0 - so don't jump down my throat re-this. The aim here is to eliminate simple errors.
Escaped Version:
^([_a-z0-9-]+(\\.[_a-z0-9-]+)*#[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})+(\\s?[,]\\s?|$))+$
You can greatly simplify things by first splitting on commas, as Pablo said, then repeatedly applying the regex to validate each individual email. You can also then point out the one that's bad -- but there's a big caveat to that.
Take a look at the regex in the article Comparing E-mail Address Validating Regular Expressions. There's another even better regex that I couldn't find just now, but the point is a correct regex for checking email is incredibly complicated, because the rules for a valid email address as specified in the RFC are incredibly complicated.
In yours, this part (\.[a-z]{2,3})+ jumped out at me; the two-or-three-letters group {2,3} I often see as an attempt to validate the top-level domain, but (1) your regex allows one or more of these groups and (2) you will exclude valid email addresses from domains such as .info or .museum (Many sites reject my .us address because they thought only 3 letter domains were legal.)
My advice to reject seriously invalid addresses, while leaving the final validation to the server, is to allow basically (anything)#(anything).(anything) -- check only for an "at" and a "dot", and of course allow multiple dots.
EDIT: Example for "simple" regex
[^#]+#[^.]+(\.[^.]+)+
This matches
test#test.com
test1#test.com
test2#test.com
foo#bar.baz.co.uk
myname#modern.museum
And doesn't match foo#this....that
Note: Even this will reject some valid email addresses, because anything is allowed on the left of the # - even another # - if it's all escaped properly. But I've never seen that in 25 years of using email in Real Life.
I am using the below regex in JavaScript for password policy check:
^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[##$_])(?=.*[\d\W]).*$
I tried the above regex using online regex checker
http://www.nvcc.edu/home/drodgers/ceu/resources/test_regexp.asp
Test cases passed as expected, negative test cases failed. But same regex when deployed in application does not validate properly.
For eg:
Tracker#123 does not work, where tRacker#123 works
Asd56544#12 also works fine.
Can you please point out what's wrong in regex above?
My advice is to separate this regex into several simple regex'es.
You may assign rules for your password, and for every rule you can assign a regex.
For example
Rule №1. Minimal length of password = 8 characters (can be done without regex)
Rule №2. At least one digit is required. ( /[0-9]/ )
Rule №3. At least one letter is required ( /[a-z]/i)
Rule №4. Illegal characters for password ( regex for some characters you don't want users to use in passwords)
Rule №n - some little regex
(and so on)
With this approach, it will be more easier to manage your validation in sooner time. For example after a year, you'll have to change your password policy. You'll forget what your big regex is meaning (and will spend a lot of time changing that big regex, or doing a new one). But with little separates regexes (meaning rules) you easily configure your password policy
Are you sure you syntax is correct?
Have a look at this JSfiddle, in it all the test cases pass
http://jsfiddle.net/pCLpX/