Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-10-2013, 11:45 AM   PM User | #1
Spudster
New Coder

 
Join Date: Jul 2012
Posts: 56
Thanks: 5
Thanked 0 Times in 0 Posts
Spudster is an unknown quantity at this point
Classes & Global Vars??

Hey I have two questions.

One ive been trying to make my vars global but nothing working? How can I do this without affecting the functionality of the site? And Im trying to see if A user passwords & email match ive done this using two classes the same is there a better way in doing this?


Heres the HTML

PHP Code:


        
        
<form name='form'>
        
        
Username:<br>
        
        <
input type='text' name='user_reg' id='user_reg_input' placeholder='John,Nick,Terry,Mathew etc..'>    <text id='user_check'>  </text>
        
        
        <
br>
            
Password:<br>
            
        <
input type='password' name='pass_reg' id='pass_reg' class='reg' placeholder='Choose a strong Password'> <text id='result'>    </text>    
        
        
        <
br>
        
        
Comfirm Password:<br>
        <
input type='password' name='com_pass' id='com_pass' class='reg' placeholder='Comfirm Your Password'>    <text id='com_pass_check'Text> </text><br>
        
        
Email:<br>
        
        <
input type='email' name='email_reg' id='email_reg' class='email'  placeholder='Enter A Vaild Email'>    <text id='email_content'Text </text><br>
        
        
Comfirm Email:<br>
        <
input type='email' name='com_email' id='email_com' class='email'  placeholder='Comfirm Your Email'> <text id='com_content'Text </text>    <br>
        
        <
input type='submit' name='reg_procces' value='Sign Up'>
        
        </
form




Heres the JQuery/Java Script
PHP Code:



    

                            
var email_reg = $('#email_reg').val();
                            var 
email_con = $('#email_com').val();
                            

$(
document).ready(function() {






 
 
 

$(
'#title').keyup(function() {


var 
title = $('#title').val();


$.
post('pages/get.php', {  titletitle}, function(data)  {

$(
'#typing').html(data);
        
        
        });



    });


        
$(
'#user_check').load('check.php').show()



$(
'#user_reg_input').keyup(function() {


        
    
var 
user_reg = $('#user_reg_input').val();
 
$.
post('check.php', {user_regform.user_reg.value  },    

    function(
result) {
        
    $(
'#user_check').html(result).show();
    
    });




            });


                
                
    $(
'#pass_reg').keyup(function(){
        $(
'#result').html(checkStrength($('#pass_reg').val()))
        
        
        
    })    
    
    function 
checkStrength(password){
    
    
//initial strength
    
var strength 0
    
    
//if the password length is less than 6, return message.
    
if (password.length 6) { 
        $(
'#result').removeClass()
        $(
'#result').addClass('short')
        return 
'Too short' 
    
}
    
    
//length is ok, lets continue.
    
    //if length is 8 characters or more, increase strength value
    
if (password.length 7strength += 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 += 
    
    
//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 ) {
        $(
'#result').removeClass()
        $(
'#result').addClass('weak')
        return 
'Weak'            
    
} else if (strength == ) {
        $(
'#result').removeClass()
        $(
'#result').addClass('good')
        return 
'Good'        
    
} else {
        $(
'#result').removeClass()
        $(
'#result').addClass('strong')
        return 
'Strong'
    
}
}


        $(
'.reg').keyup(function() 
        
        {
    
                    
        
        
        
     
     var 
password = $('#pass_reg').val();
     
        
     
     var 
com_pass = $('#com_pass').val();
     
        
        
         

        
        if(
password == com_pass) {
         $(
'#com_pass_check').html('Success').val();
         
         
         } 
         
         
         else {
         
         if(
password != com_pass)
         $(
'#com_pass_check').html('Passwords Must Match').val();
         
                }
                
                
                
            {
            
            
        
    if((
jQuery.trimpassword )).length==0)

    $(
'#result').html('').val();
         
        
        
        
    if((
jQuery.trimcom_pass )).length==0)


        $(
'#com_pass_check').html('').val();
                }
                
                });
                                

                            $(
'#email_reg').keyup(function() {
                            
                            
                
    

                            var 
email_reg = $('#email_reg').val();
                            var 
email_con = $('#email_com').val();
                            
            var 
enteredEmailAddress=this.value;
            var 
pattern = new RegExp(/^(("[\w-\s]+")|([w-]+(?:.[w-]+)*)|("[\w-\s]+")([w-]+(?:.[w-]+)*))(@((?:[w-]+.)*w[w-]{0,66}).([a-z]{2,6}(?:.[a-z]{2})?)$)|(@[?((25[0-5].|2[0-4][0-9].|1[0-9]{2}.|[0-9]{1,2}.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2}).){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})]?$)/i);
        
        

                            
                            
                            
//white space here
                            
                        
        
        
if((jQuery.trimemail_reg )).length==0) {
                        
                        $(
'#email_content').html('').val();
                        
                        }                            
                        
                        
////vaild email
                        
                            
else if(email_reg) {
                            
                            
                            
                            
        if(!
pattern.test(enteredEmailAddress))    {
            
            


        $(
'#email_content').html('Email Must Be Vaild').val();
                            
                }

                            
                            
                            
        if(
pattern.test(enteredEmailAddress))    {
                


        $(
'#email_content').html('Success').val();
        
        
                            
                }
                            
                            
                            
//here
                            
}
                            
                            
                            
                            
                            
//post to php page here 
                            
                            
                            
                            
                            
});
                
                            
                            $(
'.email').keyup(function() {
                            
                            
                            var 
email_reg = $('#email_reg').val();
                            var 
email_com = $('#email_com').val();
                            
                            if(
email_reg==email_com)
                            
alert('Equal');
                            });
                            

    }); 
Since these codes are client side I can make it avliable to view since it is by the browser anyway.. I carn,t add Server side since this is A site project made to be public soon.

Thanks for the advice,

Spudster
Spudster is offline   Reply With Quote
Old 02-10-2013, 12:03 PM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,612
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
You can’t have values in the variables before the elements even exist. You need to put them inside the document ready function, too. Is there a reason why they should be global?
__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Old 02-10-2013, 09:31 PM   PM User | #3
Spudster
New Coder

 
Join Date: Jul 2012
Posts: 56
Thanks: 5
Thanked 0 Times in 0 Posts
Spudster is an unknown quantity at this point
Ok ive looked it up and vars dont work within any function, They dont have to be global but it would help so i dont have to re create the var within every function to use them.

Not I big deal but the code would be alot neater.
Spudster is offline   Reply With Quote
Old 02-10-2013, 10:50 PM   PM User | #4
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,612
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
You don’t have to recreate the variables in every function if you just move them inside the document ready function:
PHP Code:
$(document).ready(function() {
  var 
email_reg = $('#email_reg').val();
  var 
email_con = $('#email_com').val();
  $(
'#title').keyup(function() {
  
… 
Also, you can declare variables without any value initially and assign any value to them later on, like:
PHP Code:
var xyz;
function 
example() {
  

  xyz 
123;

__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:10 PM.


Advertisement
Log in to turn off these ads.