Women in Technology

Hear us Roar



Article:
  Form Validation
Subject:   Article great... But error in checkPassword() search() code in IE
Date:   2002-01-10 14:28:07
From:   aedell
This is an excellent article! (http://www.oreillynet.com/pub/a/javascript/2001/08/10/form_valid.html?page=1)


Found an error though... the checkPassword() function has an error in how it uses the search() feature of JavaScript. (I can only speak about Microsoft JScript for IE4+ - because I couldn't get this code to work in IE6 - I didn't try Netscape or others). After some investigation, here is what I found


This following code does not work properly in IE 6 (and most likely IE 4+):


else if (!((strng.search(/(a-z)+/))
&& (strng.search(/(A-Z)+/))
&& (strng.search(/(0-9)+/)))) {
error = "The password must contain at least one
uppercase letter, one lowercase letter,
and one numeral.\n";
}


Why? According to JScript documentation: "If a match is found, the search method returns an integer value that indicates the offset from the beginning of the string where the match occurred. If no match is found, it returns -1. " In other words, the logical AND (&&) operator in the above code is trying to discern whether the combination of three numbers is TRUE or not - the result is less than accurate! -1 according to JScript is "true" and a "0" is false - so this code is simply all wrong.


The code SHOULD look something like this (I believe).


else if (!((strng.search(/(a-z)+/) != -1)
&& (strng.search(/(A-Z)+/) != -1)
&& (strng.search(/(0-9)+/) != -1))) {
error = "The password must contain at least one
uppercase letter, one lowercase letter,
and one numeral.\n";
}


The above code works for me. Is this behavior of search() only like this in JScript for IE4+????


Again, thanks for the article!


Adam Edell
Web Developer

Full Threads Oldest First

Showing messages 1 through 2 of 2.

  • Article great... But error in checkPassword() search() code in IE
    2003-11-26 09:24:51  anonymous2 [View]

    Even this wont work the main problem is (A-Z)
    we have to use [A-Z] same for all
    Thanks
    Ram Gogineni
  • Article great... But error in checkPassword() search() code in IE
    2003-11-26 09:18:41  anonymous2 [View]

    Even this wont work the main problem is (A-Z)
    we have to use [A-Z] same for all
    Thanks
    Ram