Solved

JavaScript validation

Posted on 2014-10-30
6
103 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 51

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 51

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now