/    Sign up×
Community /Pin to ProfileBookmark

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;
}
}

});
[/CODE]

Bram131

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@DanInMASep 08.2011 — 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);



});[/CODE]
×

Success!

Help @Bram131 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...