javascript not working - password strength

Posted on 2014-08-12
Last Modified: 2014-08-12
Im trying to check a strength of a password but it does not seem to do anything on the page

      <input type="text" name="username" id="username" />
       <div class="username_avail_result" id="username_avail_result"></div>
     <input type="text" name="password" id="password" />
     <div class="password_strength" id="password_strength"></div>

	$('#username').keyup(function(){ // Keyup function for check the user action in input
		var Username = $(this).val(); // Get the username textbox using $(this) or you can use directly $('#username')
		var UsernameAvailResult = $('#username_avail_result'); // Get the ID of the result where we gonna display the results
		if(Username.length > 2) { // check if greater than 2 (minimum 3)
			UsernameAvailResult.html('Loading..'); // Preloader, use can use loading animation here
			var UrlToPass = 'action=username_availability&username='+Username;
			$.ajax({ // Send the username val to another checker.php using Ajax in POST menthod
			type : 'POST',
			data : UrlToPass,
			url  : 'Username_Checker/checker.php',
			success: function(responseText){ // Get the result and asign to each cases
				if(responseText == 0){
					UsernameAvailResult.html('<span class="success">Username name available</span>');
				else if(responseText > 0){
					UsernameAvailResult.html('<span class="error">Username already taken</span>');
					alert('Problem with sql query');
			UsernameAvailResult.html('Enter atleast 3 characters');
		if(Username.length == 0) {
	$('#password, #username').keydown(function(e) { // Dont allow users to enter spaces for their username and passwords
		if (e.which == 32) {
			return false;
		checkStrength is function which will do the 
		main password strength checking for us
	function checkStrength(password)
		//initial strength
		var strength = 0
		//if the password length is less than 6, return message.
		if (password.length < 6) { 
			return 'Too short' 
		//length is ok, lets continue.
		//if length is 8 characters or more, increase strength value
		if (password.length > 7) strength += 1
		//if password contains both lower and uppercase characters, increase strength value
		if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  strength += 1
		//if it has numbers and characters, increase strength value
		if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  strength += 1 
		//if it has one special character, increase strength value
		if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/))  strength += 1
		//if it has two special characters, increase strength value
		if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1
		//now we have calculated strength value, we can return messages
		//if value is less than 2
		if (strength < 2 )
			return 'Weak'			
		else if (strength == 2 )
			return 'Good'		
			return 'Strong'

Open in new window

Question by:runnerjp2005
    LVL 107

    Accepted Solution

    Have you tried looking at this with Chrome Dev Tools?  It usually will tell you exactly what statement is wrong.

    Author Closing Comment

    Missed a ' out on line53.

    Thanks for teaching me how to fish ;)
    LVL 107

    Expert Comment

    by:Ray Paseur
    Thanks for the points.  The Dev Tools are just excellent for dealing with anything in JS and CSS.  I'm amazed we didn't have them all along!

    All the best, ~Ray

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
    Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now