7 Replies - 840 Views - Last Post: 28 February 2012 - 01:24 PM

Topic Sponsor:

#1 laytonsdad  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 30-April 10

Best usage of regular expressions

Posted 21 January 2012 - 01:00 PM

I am working on a form that I need to validate the password meets specs:

  • At least one capital letter
  • At least one lowercase letter
  • At least one number


I have already tested if the string is at least 8 characters now I need to test the above specs.

My question is:

Should I use three different regex tests or can I make one that will fit this? I have thought of using ajax and php is this a better Idea?

This post has been edited by laytonsdad: 21 January 2012 - 01:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Best usage of regular expressions

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3239
  • View blog
  • Posts: 10,644
  • Joined: 18-April 07

Re: Best usage of regular expressions

Posted 21 January 2012 - 01:57 PM

*
POPULAR

You should be able to do this with one test. Javascript supports the ability to "lookahead". The best way to explain this is to show an example...

I have the word "quick" I want to match. However, I want to see if an "i" is somewhere in the string following the "q". It could be right next to it or not. In this case it is two characters down the string. I would do this using the expression q(?=.*i). The thing about this is that it doesn't consume the match. It just tests if it is true. Is there a "q" followed by an "i" somewhere along the string. Yes.

We are "looking ahead" in the string to find if there is that "i". Now you can use a combination of these things to then run each of your tests. We will look ahead to the next look ahead.

The following expression should test all three on the same string...

(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])



This adding of lookaheads essentially creates an "and"ing effect. Look for a capital letter, then look down the line for a lowercase and look down the line for a number. We put in the .* pattern to account for the characters that might be between these matches. Each of these conditions start from the original match point so it is not based on order.

Try it out. Should work for you.

This post has been edited by Martyr2: 21 January 2012 - 01:58 PM

Was This Post Helpful? 5
  • +
  • -

#3 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 124
  • View blog
  • Posts: 552
  • Joined: 30-October 10

Re: Best usage of regular expressions

Posted 21 January 2012 - 02:47 PM

  • Only one regular expression is necessary for this, as Martyr2 has already demonstrated
  • You definitely shouldn't use your server for this, there simply isn't a need for it. You will, however, have to validate all of the submitted data when it is submitted to your server; there is nothing stopping your users from moddifying your Javascript, or turning it off completely!

Was This Post Helpful? 1
  • +
  • -

#4 laytonsdad  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 30-April 10

Re: Best usage of regular expressions

Posted 21 January 2012 - 07:29 PM

View PostRetardedGenius, on 21 January 2012 - 02:47 PM, said:

  • Only one regular expression is necessary for this, as Martyr2 has already demonstrated
  • You definitely shouldn't use your server for this, there simply isn't a need for it. You will, however, have to validate all of the submitted data when it is submitted to your server; there is nothing stopping your users from moddifying your Javascript, or turning it off completely!


Thank you to Martyr2 and RetardedGenius this has been helpful!

My only other question is does anyone have a good place to find a step by step tutorial of how to use regular expressions for javascript? I have found many but they are brief and I am having trouble getting it.
Was This Post Helpful? 0
  • +
  • -

#5 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 124
  • View blog
  • Posts: 552
  • Joined: 30-October 10

Re: Best usage of regular expressions

Posted 22 January 2012 - 02:51 AM

laytonsdad, I learned the basics of regular expressions from David Flanagan's Javascript: The Definitive Guide, which I highly recommend to anyone who is doing any client-side web development. But, at over 1000 pages, this book is definitely overkill if you only want to learn about regular expressions; in which case I'd recommend reading the Wikipedia article on regular expressions and checking out the tutorials on regular-expressions.info. :)

This post has been edited by Atli: 22 January 2012 - 04:20 AM
Reason for edit:: Fixed the second URL. The forum software seems to have a hard time with the word "expression" in URLs.

Was This Post Helpful? 1
  • +
  • -

#6 laytonsdad  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 30-April 10

Re: Best usage of regular expressions

Posted 22 January 2012 - 03:15 PM

View PostRetardedGenius, on 22 January 2012 - 02:51 AM, said:

laytonsdad, I learned the basics of regular expressions from David Flanagan's Javascript: The Definitive Guide, which I highly recommend to anyone who is doing any client-side web development. But, at over 1000 pages, this book is definitely overkill if you only want to learn about regular expressions; in which case I'd recommend reading the Wikipedia article on regular expressions and checking out the tutorials on regular-expressions.info. :)


Thank you for the input I had not even thought of using Wikipedia. I saw the site regular-expressions.info but I had a hard time getting it from there. Ill go back again and read it over and over. :stupid:
Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 323
  • View blog
  • Posts: 1,947
  • Joined: 19-May 09

Re: Best usage of regular expressions

Posted 20 February 2012 - 11:45 AM

This page I found most useful for both a clear synopsis of javascript regular expressions (there's a link to the RegExp object) and for a place to tinker with examples.

This post has been edited by BobRodes: 20 February 2012 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#8 laytonsdad  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 30-April 10

Re: Best usage of regular expressions

Posted 28 February 2012 - 01:24 PM

View PostBobRodes, on 20 February 2012 - 11:45 AM, said:

This page I found most useful for both a clear synopsis of javascript regular expressions (there's a link to the RegExp object) and for a place to tinker with examples.

Thank you for this I will take a look.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1