Closed Thread
Results 1 to 6 of 6

Thread: Multiple error messages in one alert box in Javascript?

  1. #1
    system32 is offline Newbie
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Question Multiple error messages in one alert box in Javascript?

    I am new to JavaScript and am currently learning. I want to validate data on a form. But I want all errors to show up on one alert prompt box. For example, if a user does not type in their name and city, I do not want two alerts telling them to fill in the name field and then another after that saying that they need to fill in the city as well. I want to combine these into one, to create one alert that displays all validation errors. How would I do this? Here is some code I started with:

    Code:
    <script language="javascript" type="text/javascript">
    function checkName()
    {
    var myForm = document.pizzaForm;
    if (myForm.firstName.value == "" || myForm.lastName.value == "")
    {
    alert("Please type in your name");
    return false;
    }
    else if (myForm.address.value == "")
    {
    alert("Please type in your address");
    return false;
    }
    else
    {
    alert("Please type in your city");
    return false;
    }
    
    }
    </script>

    Thanks

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    cdg10620's Avatar
    cdg10620 is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Texas
    Posts
    387
    Blog Entries
    3
    Rep Power
    12

    Re: Multiple error messages in one alert box in Javascript?

    One really great way to debug javascript is using Firefox's extension "Firebug." If you want to try it out do this:

    Make sure you have Firefox with Firebug installed. In your code, if there is something you want to check you can write console.log( "message" ) or console.log( variable ). You can use this to give yourself messages or check variables. This is a great way to be able to search your available DOM objects or get a better handle on scope. It's also not as pesky as alert boxes. Let me know if you like it.

    If you do want to use the alert box then you can just string concatenate your variables and strings using "+".
    -CDG10620
    Software Developer

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Multiple error messages in one alert box in Javascript?

    You can't use an if-elseif-elseif-else structure for what you want. You need to concatenate the error message into a single string, with a separate if statement for each error message.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    cdg10620's Avatar
    cdg10620 is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Texas
    Posts
    387
    Blog Entries
    3
    Rep Power
    12

    Re: Multiple error messages in one alert box in Javascript?

    I would also suggest using a method name that better fits its purpose. checkName() implies that you are only checking the name. checkValues() or validateForm() seems like a better fit.
    -CDG10620
    Software Developer

  6. #5
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: Multiple error messages in one alert box in Javascript?

    Wrong subforum, someone should move the thread to Javascript.

  7. #6
    system32 is offline Newbie
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Re: Multiple error messages in one alert box in Javascript?

    thanks everyone

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 06-18-2010, 05:26 AM
  2. Replies: 4
    Last Post: 02-07-2010, 12:53 PM
  3. Error messages
    By svaidya in forum Java Help
    Replies: 15
    Last Post: 02-20-2009, 07:54 AM
  4. Java HW help with error messages
    By nickstoner in forum Java Help
    Replies: 7
    Last Post: 11-06-2008, 08:52 AM
  5. How to produce custom error messages
    By reachpradeep in forum ASP, ASP.NET and Coldfusion
    Replies: 0
    Last Post: 03-03-2007, 09:51 AM

Tags for this Thread

Bookmarks

Posting Permissions

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