Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,516 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,905 people online right now. Registration is fast and FREE... Join Now!




Need help with detecting a symbol in textbox!

 
Reply to this topicStart new topic

Need help with detecting a symbol in textbox!, I have a poassword strenght bar that runs in real time.

EvolutionMedia
post 5 Oct, 2008 - 08:42 PM
Post #1


D.I.C Head

**
Joined: 11 Aug, 2008
Posts: 83



Thanked 1 times
My Contributions


Hey guys!

So, in this registration form that I made! I have a real time password strength script.

Now the problem is that say if I type in 7 characters and a _ or - or a / it will put it as strong. But however, that's not what I want. I want it to do like this: monkey1/11 and then it it was strong.

Here's the script:
CODE

<script language="javascript" type="text/javascript">
    
    var minpwlength = 4;
    var fairpwlength = 7;
    
    var STRENGTH_SHORT = 0;  // less than minpwlength
    var STRENGTH_WEAK = 1;  // less than fairpwlength
    var STRENGTH_FAIR = 2;  // fairpwlength or over, no numbers
    var STRENGTH_GOOD = 3; // fairpwlength or over with at least one number
    var STRENGTH_STRONG = 4;
    
    img0 = new Image();
    img1 = new Image();
    img2 = new Image();
    img3 = new Image();
    img4 = new Image();
    
    img0.src = 'images/strenghtbar.png';
    img1.src = 'images/strenght_weak.png';
    img2.src = 'images/strenght_fair.png';
    img3.src = 'images/strenght_good.png';
    img4.src = 'images/strenght_strong.png';
    var strengthlevel = 0;
    
    var strengthimages = Array( img0.src,
                                img1.src,
                                img2.src,
                                img3.src,
                                img4.src );
    var strengthstat = Array(' ','Weak','Fair','Medium','Strong');
    
    
    function updatestrength( pw ) {
    
        if( istoosmall( pw ) ) {
    
            strengthlevel = STRENGTH_SHORT;
    
        }
        else if( !isfair( pw ) ) {
    
            strengthlevel = STRENGTH_WEAK;
    
        }    
        else if( hasnum( pw ) ) {
    
            strengthlevel = STRENGTH_GOOD;
    
        }
        else if(( hassymb(pw)) || (hasnum(pw))){
    
            strengthlevel = STRENGTH_STRONG;
    
        }
        else {
        strengthlevel = STRENGTH_FAIR;
        }
        
        document.getElementById( 'strength' ).src = strengthimages[strengthlevel];
        document.getElementById('strength_status').innerHTML = strengthstat[strengthlevel];
        
    
  }  
    function isfair( pw ) {
    
        if( pw.length < fairpwlength ) {
    
            return false;
    
        }
        else {
    
            return true;
    
        }
    
    }
    
    function istoosmall( pw ) {
    
        if( pw.length < minpwlength ) {
    
            return true;
    
        }
        else {
    
            return false;

        }
    
    }
    
    function hasnum( pw ) {
    
        var hasnum = false;
    
        for( var counter = 0; counter < pw.length; counter ++ ) {
    
            if( !isNaN( pw.charAt( counter ) ) ) {
    
                hasnum = true;
    
            }
    
        }
    
    
        return hasnum;
    
    }
function hassymb(pw) {
var hassymb = false;
for( var count = 0; count < pw.length; count++) {
          if( pw.charAt(count) == '_') {
          
           hassymb = true;
           }
         else if( pw.charAt(count) == '-') {
         hassymb = true;
         }
         else if( pw.charAt(count) == '/') {
         hassymb = true;
         }
        
    
   }
return hassymb;
}


Thanks for the helps guys - really appreciate it! smile.gif

-Paul
User is offlineProfile CardPM

Go to the top of the page


BetaWar
post 6 Oct, 2008 - 07:15 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,976



Thanked 77 times

Dream Kudos: 1175
My Contributions


I believe the problem in your code is here:
CODE
else if(( hassymb(pw)) || (hasnum(pw))){
    
            strengthlevel = STRENGTH_STRONG;
    
        }


You are using the or statement || instead of the and && which means that only 1 of the things has to be true (hasnum() or hassym())

Try changing it to this:
CODE
else if(( hassymb(pw)) && (hasnum(pw))){
    
            strengthlevel = STRENGTH_STRONG;
    
        }


HTH
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 12:58AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month