...

View Full Version : Adding all errors at once



Zangeel
07-16-2009, 06:55 AM
Hello I have this jQuery script, and when you hit submit it adds an error to the first empty input field. but it doesnt highlight the others so if all are empty itll be like

[ ] - hightlighted for error empty
[ ] - empty but no error highlight because the top one has the error first
[ ] - empty but no error highlight because the top one has the error first

the jQuery code is



var name = $('input[name=name]');
var email = $('input[name=email]');
var website = $('input[name=website]');
var comment = $('textarea[name=comment]');

//Simple validation to make sure user entered something
//If error found, add hightlight class to the text field
if (name.val()=='') {
name.addClass('hightlight');
return false;
} else name.removeClass('hightlight');

if (email.val()=='') {
email.addClass('hightlight');
return false;
} else email.removeClass('hightlight');

if (comment.val() == '') {
comment.addClass('hightlight');
return false;
} else comment.removeClass('hightlight');


i was thinking maybe a loop to add all the errors at once but im not sure how

fside
07-16-2009, 07:05 AM
Someone may have an answer. But I'd just note that there's a Javascript Framework forum here. No, some might insist jQuery is a library, not a framework. But still, that's the name the site owners chose for that forum. And people into jQuery might find you there?

Zangeel
07-16-2009, 07:26 AM
This is set up like standard javascript would be set up, so jQuery has nothing to do with the problem at hand.

Philip M
07-16-2009, 07:34 AM
if (name.val()=='') {
name.addClass('hightlight');
return false;


return false terminates the function, so if the first field is blank the others never get tested. Suggest you have a flag which returns false at the end of the function if errors found.

var flag = true;
if (name.val()=='') {
name.addClass('hightlight');
flag = false;
}
.....
return flag;


Validation such as if (name.val()=='') { is barely worthy of the name as even a single space or a ? will return false (i.e. pass the validation)).


I heard him then, for I had just
Completed my design
To keep the Menai Bridge from rust
By boiling it in wine.
- Lewis Carroll

fside
07-16-2009, 08:10 AM
jQuery has nothing to do with the problem at hand.

I read your OP - "I have this jQuery script". So I assumed the problem had to do with that library.

But it looks like Phil has an answer.

Philip M
07-16-2009, 08:39 AM
I read your OP - "I have this jQuery script". So I assumed the problem had to do with that library.

But it looks like Phil has an answer.

assume makes an a s s of u and me. :D:D


I note that this US software asteriks out the word a s s;

"There was a young maid of Madras
Who had a most beautiful a s s.
Not rounded and pink, as you fellows think,
But t'was grey, with long ears, and ate grass". :D:D

N.B. please send any infraction points to the usual email address! :rolleyes:

fside
07-17-2009, 06:23 PM
assume makes an a s s of u and me. :D:D


It's just that I assumed that because it was explicitly stated in the OP.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum