• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I need a help with a regular expression please

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My requirement is that a password string be validated in JavaScript:

(It should work for both firefox 3.0 and internet explorer 6 and IE7)

1. It must have ONE UPPERCASE.

2. It must have ONE LOWERCASE.

3. It must have ONE DIGIT.

4. SPECIAL CHARACTER is not mandatory.


I am using the regular expression:

var re = /(?=\w*[0-9])(?=\w*[a-z])(?=\w*[A-Z])\w*/;


Here the problem is that Abcd1@123 works.

However Abcd@123 does not work. I think the '@123' is being matched to \w*. Thus the condition of (?=\w*[0-9]) , that a digit is a mandatory is not being satisfied and my password match is failing. Anything after a special character is not being checked.


Can anyone help me please? Thanks in advance.

Kiran
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

However Abcd@123 does not work. I think the '@123' is being matched to \w*. Thus the condition of (?=\w*[0-9]) , that a digit is a mandatory is not being satisfied and my password match is failing. Anything after a special character is not being checked.



The \w* portion of the regex is supposed to gobble up the word characters up to the digit. In this case, the @ sign is not a word character, so it can't gobble it -- and hence, digits after the @ sign can't be used as a match for that part of the regex.

Henry
 
Kiran Shirali
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

However Abcd@123 does not work. I think the '@123' is being matched to \w*. Thus the condition of (?=\w*[0-9]) , that a digit is a mandatory is not being satisfied and my password match is failing. Anything after a special character is not being checked.



The \w* portion of the regex is supposed to gobble up the word characters up to the digit. In this case, the @ sign is not a word character, so it can't gobble it -- and hence, digits after the @ sign can't be used as a match for that part of the regex.

Henry




hey thanks...But then what should i do to make a symbol being gobbled up. I tried replacing \w* with \W*. But it was no success. what changes to the regular expression should i make?

Kiran
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don;t you do it in multiple checks instead of one. Than you can tell the user why it fails.

Eric
 
Kiran Shirali
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Pascarello wrote:Why don;t you do it in multiple checks instead of one. Than you can tell the user why it fails.

Eric




But how can i do that?

I mean would i have to have different regular expressions for each case? If so how will i check for non mandatory symbol?


Can you please help me...

Kiran
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I tried replacing \w* with \W*. But it was no success.



No offense, but you just provided us with a regex that used multiple zero length look-aheads. That is pretty advanced stuff there.... Adding the ability to check for the "@", in addition to a word character, is simple in comparson.

Why don't you use a character set with the characters that you want? (ie... the square brackets)

Henry
 
Kiran Shirali
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


No offense, but you just provided us with a regex that used multiple zero length look-aheads. That is pretty advanced stuff there.... Adding the ability to check for the "@", in addition to a word character, is simple in comparson.

Why don't you use a character set with the characters that you want? (ie... the square brackets)

Henry




Hi Henry,

I did try that. I tried the following:

var re = /(?=\w*[0-9])(?=\w*[a-z])(?=\w*[A-Z])[@]*\w*/;

It is not working. I have read very little of regular expressions. I had searched in online forums for a regular expression that would satisfy my requirement. This expression was the best match that i could find. I am sorry if i am asking a dumb question, but i am very new to the subject.

Kiran
 
Kiran Shirali
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Pascarello wrote:Why don;t you do it in multiple checks instead of one. Than you can tell the user why it fails.

Eric




Hi Eric,

I did what you suggested and it works fine.

This is the piece of code i used:




That was one major relief . Thanks a lot everyone.

Kiran
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It is not working. I have read very little of regular expressions. I had searched in online forums for a regular expression that would satisfy my requirement. This expression was the best match that i could find. I am sorry if i am asking a dumb question, but i am very new to the subject.



I am so glad that you got it working -- and you understood how it works too !!

Although changing the regex would have been easier (probably a 1 minute change), I really don't ever recommend using any code that you don't understand. So, your new code is more than just "one major relief" -- if you encounter the need to have another check, in the future, you won't have to worry about unknown code again.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic