[Javascript] Regarding Password Validation + RegExp matching

Paul Novitski paul at dandemutande.org
Mon Mar 1 12:15:40 CST 2004


Ram,

An alternative to Dave's method (of deleting all the non-matching 
characters and counting the results) and probably nanoseconds faster (if 
you're in a hurry) is simply to count the number of matches:

         var reDigits = /[0-9]/g;                // find all digits
         var reSpecials = /[^a-zA-Z0-9]/g;       // find all non-(letters & 
digits)

         var aDigits = myString.match(reDigits);
         var aSpecials = myString.match(reSpecials);

         alert("number of digits: " + aDigits.length);
         alert("number of special characters: " + aSpecials.length);

One advantage of this technique is that it works for counting substrings of 
varying lengths.  RegExp allows you to search for substrings containing 
optional characters, for example, /abc?d/ means "ab" + optional "c" + "d", 
finding "abd" and "abcd".

Using the String.match() method gives you an array of the matching 
substrings, which is handy when you want to examine, display, or otherwise 
process them as a series:

         for (var i=0; i < aDigits.length; i++){
                 DoSomething(aDigits[i]);
         }

As far as I can tell there's no penalty for consolidating these statements:

         var aDigits = myString.match(/[0-9]/g);
         var aSpecials = myString.match(/[^a-zA-Z0-9]/g);

...except that once you derive the ideal RegExp for a particular purpose, 
you may wish to declare or include it globally so that you use the same 
technique throughout your software.  For example, once you decide how you 
want to validate an email address, you'll likely want to do it consistently 
everywhere.

Paul


At 06:59 AM 3/1/2004, you wrote:
>The easiest way I can think of is to use two regular expression filters to
>strip (respectively) everything out EXCEPT numbers, and everything out
>EXCEPT special characters.  The output of these two filters are put into two
>strings, such as
>
>var digitString;
>var specialString;
>
>... and then simply check the length of the two strings to make sure that
>they are greater than or equal to 2 and 1, again respectively.
>
>If you're not up on regular expression filters, you're missing about 80% of
>the power of JavaScript (at least IMO).
>
>Try
>
>   var re0 = /[^0-9]/g;
>   var re1 = /[a-zA-Z0-9]/g;
>
>   var digitString = myString.replace(re0, "");
>   var specialString = myString.replace(re1, "");
>
>   alert("number of digits: " + digitString.length);
>   alert("number of special characters: " + specialString.length);
>
>Or some variation thereof.  Incidently, I'd also check myString (the target
>string coming in) for total length as well, as short strings are easy to
>crack.
>
>-- Dave Lovering
>----- Original Message -----
>From: "Ramachandran" <ramachandran at summitworks.com>
>To: "[JavaScript List]" <javascript at LaTech.edu>
>Sent: Monday, March 01, 2004 6:42 AM
>Subject: [Javascript] Regarding Password Validation
>
>
> > Hi all,
> >
> > I want to perform a validation in password field. The validation includes
> > the following conditions.
> >
> > 1. It should contain atleast two no.s and once special characters.
> >
> > How can i do this one..
> >
> > Regards,
> > Ram
> >
> > _______________________________________________
> > Javascript mailing list
> > Javascript at LaTech.edu
> > https://lists.LaTech.edu/mailman/listinfo/javascript
> >
> >
>
>
>_______________________________________________
>Javascript mailing list
>Javascript at LaTech.edu
>https://lists.LaTech.edu/mailman/listinfo/javascript





More information about the Javascript mailing list