Solved

JavaScript validation

Posted on 2014-10-30
6
106 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
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 54

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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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 54

Expert Comment

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

773 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