www.webdeveloper.com
Results 1 to 2 of 2

Thread: Validate script

  1. #1
    Join Date
    Sep 2011
    Posts
    1

    Validate script

    Hi all,

    I'm new to javascript and I wanted some exercise.
    My script does not work anymore. And i can't get it working on myself.

    Does anyone see the bug in this code?

    Thanks!

    Code:
    $(document).ready(function() {
    
    var username = $("#username");
    var usernameInfo = $("#usernameInfo");
    var emailadres = $("#emailadres");
    var emailadresInfo = $("#emailadresInfo");
    var password1 = $("#password1");
    var password1Info = $("#password1Info");
    var password2 = $("#password2");
    var password2Info = $("#password2Info");
    
    username.blur(validateUserName);
    username.keyup(validateUserName);
    emailadres.blur(validateEmail);
    emailadres.keyup(validateEmail);
    password1.blur(validatePassword1);
    password1.keyup(validatePassword1);
    password2.blur(validatePassword2);
    password2.keyup(validatePassword2);
    
    function validateUserName() {
    usernameInfo.html('<img src="loader.gif" align="absmiddle">&nbsp;');
    if(username.val().length < 3) {
    username.removeClass("okay");
    username.addClass("error");
    usernameInfo.html('<img src="/images/icons/exclamation.png">');
    return false;
    }
    else {
    username.removeClass("error");
    username.addClass("okay");
    usernameInfo.html('<img src="/images/icons/accept.png">');
    return true;
    }
    }
    
    function validateEmail() {
    emailadresInfo.html('<img src="loader.gif" align="absmiddle">&nbsp;');
    var a = $("#emailadres").val();
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if(filter.test(a)) {
    emailadres.removeClass("error");
    emailadres.addClass("okay");
    emailadresInfo.html('<img src="/images/icons/accept.png">');
    return true;
    }
    else {
    emailadres.removeClass("okay");
    emailadres.addClass("error");
    emailadresInfo.html('<img src="/images/icons/exclamation.png">');
    return false;
    }
    }
    
    function validatePassword1(){
    		if(password1.val().length < 4){
    			password1.removeClass("okay");
                password1.addClass("error");
                password1Info.html('<img src="/images/icons/exclamation.png">');
                return false;
    		}
    		else {			
    			password1.removeClass("error");
                password1.addClass("okay");
                password1Info.html('<img src="/images/icons/accept.png">');
                return true;
    		}
    	}
    
    function validatePassword2(){
    		if( password1.val() != password2.val() ){
    			password2.removeClass("okay");
                password2.addClass("error");
                password2Info.html('<img src="/images/icons/exclamation.png">');
                return false;
    		}
    		else{
    			password2.removeClass("error");
                password2.addClass("okay");
                password2Info.html('<img src="/images/icons/accept.png">');
                return true;
    		}
    	}
    
    
    
    });

    Bram131

  2. #2
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    ok im not sure why but it doesnt seem to like having some of your variables in the dom.ready, frankly i have no idea why. I ran this through jshint.com, there still seems to be an issue with unescaped - 's in the var filter, but maybe one of the more advanced guys could explain why.

    This is what I have so far, perhaps it will get you in the right direction.

    Code:
    var username = $("#username");
    var usernameInfo = $("#usernameInfo");
    var emailadres = $("#emailadres");
    var emailadresInfo = $("#emailadresInfo");
    var password1 = $("#password1");
    var password1Info = $("#password1Info");
    var password2 = $("#password2");
    var password2Info = $("#password2Info");
    $(document).ready(function() {
    
    
    
    
    function validateUserName() {
    usernameInfo.html('<img src="loader.gif" align="absmiddle">&nbsp;');
    if(username.val().length < 3) {
    username.removeClass("okay");
    username.addClass("error");
    usernameInfo.html('<img src="/images/icons/exclamation.png">');
    return false;
    }
    else {
    username.removeClass("error");
    username.addClass("okay");
    usernameInfo.html('<img src="/images/icons/accept.png">');
    return true;
    }
    }
    
    function validateEmail() {
    emailadresInfo.html('<img src="loader.gif" align="absmiddle">&nbsp;');
    var a = $("#emailadres").val();
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    if(filter.test(a)) {
    emailadres.removeClass("error");
    emailadres.addClass("okay");
    emailadresInfo.html('<img src="/images/icons/accept.png">');
    return true;
    }
    else {
    emailadres.removeClass("okay");
    emailadres.addClass("error");
    emailadresInfo.html('<img src="/images/icons/exclamation.png">');
    return false;
    }
    }
    
    function validatePassword1(){
    		if(password1.val().length < 4){
    			password1.removeClass("okay");
                password1.addClass("error");
                password1Info.html('<img src="/images/icons/exclamation.png">');
                return false;
    		}
    		else {			
    			password1.removeClass("error");
                password1.addClass("okay");
                password1Info.html('<img src="/images/icons/accept.png">');
                return true;
    		}
    	}
    
    function validatePassword2(){
    		if( password1.val() != password2.val() ){
    			password2.removeClass("okay");
                password2.addClass("error");
                password2Info.html('<img src="/images/icons/exclamation.png">');
                return false;
    		}
    		else{
    			password2.removeClass("error");
                password2.addClass("okay");
                password2Info.html('<img src="/images/icons/accept.png">');
                return true;
    		}
    	}
    username.blur(validateUserName);
    username.keyup(validateUserName);
    emailadres.blur(validateEmail);
    emailadres.keyup(validateEmail);
    password1.blur(validatePassword1);
    password1.keyup(validatePassword1);
    password2.blur(validatePassword2);
    password2.keyup(validatePassword2);
    
    
    
    });

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles