Solved

JavaScript validation

Posted on 2014-10-30
6
108 Views
Last Modified: 2014-10-30
Hi Experts,
How could I have only one alert window pop up if the fields are left empty?
The way it is now the alert window pops up three times when all three fields are left empty.
<script type="text/javascript">
<!--
function validate_form ( )
{
	valid = true;

        if ( document.OT_Request.Phone1.value == "" )
        {
                alert ( "Please enter your 'Area Code'." );
                valid = false;
        }

        if ( document.OT_Request.Phone2.value == "" )
        {
                alert ( "Please enter the first three digits of your  'Phone Number'." );
                valid = false;
        }

        if ( document.OT_Request.Phone3.value == "" )
        {
                alert ( "Please enter the last four digits of your  'Phone Number'." );
                valid = false;
        }

        return valid;
}

//-->

</script>

Open in new window

Thank you for your help.
0
Comment
Question by:romsom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Assisted Solution

by:Ahmed Merghani
Ahmed Merghani earned 200 total points
ID: 40413204
Try this:
<script type="text/javascript">
<!--
function validate_form ( )
{
	valid = true;

        if ( document.OT_Request.Phone1.value == "" )
        {
                alert ( "Please enter your 'Area Code'." );
                valid = false;
        }

        if ( document.OT_Request.Phone2.value == ""  && valid == true)
        {
                alert ( "Please enter the first three digits of your  'Phone Number'." );
                valid = false;
        }

        if ( document.OT_Request.Phone3.value == ""  && valid == true )
        {
                alert ( "Please enter the last four digits of your  'Phone Number'." );
                valid = false;
        }

        return valid;
}

//-->

</script>
                                  

Open in new window

0
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 200 total points
ID: 40413205
Just add else to your 2nd and 3rd if like so
<script type="text/javascript">
<!--
function validate_form ( )
{
	valid = true;

        if ( document.OT_Request.Phone1.value == "" )
        {
                alert ( "Please enter your 'Area Code'." );
                valid = false;
        }

        else if ( document.OT_Request.Phone2.value == "" )
        {
                alert ( "Please enter the first three digits of your  'Phone Number'." );
                valid = false;
        }

        else if ( document.OT_Request.Phone3.value == "" )
        {
                alert ( "Please enter the last four digits of your  'Phone Number'." );
                valid = false;
        }

        return valid;
}

//-->

</script>
                                  

Open in new window

0
 
LVL 19

Assisted Solution

by:Albert Van Halen
Albert Van Halen earned 100 total points
ID: 40413214
Or display all messages in one alert
function validate_form ( )
{
	var msgs = [];

        if ( document.OT_Request.Phone1.value == "" )
                msgs.push ( "Please enter your 'Area Code'." );

        if ( document.OT_Request.Phone2.value == "" )
                msgs.push ( "Please enter the first three digits of your  'Phone Number'." );

        if ( document.OT_Request.Phone3.value == "" )
                msgs.push ( "Please enter the last four digits of your  'Phone Number'." );

	if ( msgs.length > 0 ) {
		alert ( msgs.join ( "\n" ) );
		return false;
	}

        return true;
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:romsom
ID: 40413228
Thank you very much all of you!
0
 

Author Comment

by:romsom
ID: 40413236
Albert, I really like your solution, thank you very much
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 40413254
You are welcome - good luck with your project.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question