?
Solved

JavaScript validation

Posted on 2014-10-30
6
Medium Priority
?
110 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 800 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 58

Accepted Solution

by:
Julian Hansen earned 800 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 58

Expert Comment

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

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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

800 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