Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts

    Adding all errors at once

    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

    Code:
    		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

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    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?

  • #3
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    This is set up like standard javascript would be set up, so jQuery has nothing to do with the problem at hand.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,892
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    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

  • #5
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Quote Originally Posted by Zangeel View Post
    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.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,892
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Quote Originally Posted by fside View Post
    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.


    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".

    N.B. please send any infraction points to the usual email address!
    Last edited by Philip M; 07-16-2009 at 08:45 AM.

  • #7
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Quote Originally Posted by Philip M View Post
    assume makes an a s s of u and me.
    It's just that I assumed that because it was explicitly stated in the OP.


  •  

    Posting Permissions

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