Solved

JavaScript validation

Posted on 2014-10-30
6
109 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

728 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