?
Solved

empty check javascript

Posted on 2009-05-10
16
Medium Priority
?
174 Views
Last Modified: 2012-05-07
do you know why it doesn't work?
i want to give error message if user didn't select size.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function selectProduct()
{
var val1 = document.getElementById('mylist1').options[document.getElementById('mylist1').selectedIndex].value;
var val2 = document.getElementById('mylist2').options[document.getElementById('mylist2').selectedIndex].value;
var form1 = document.getElementById('form1');
 
if (val1 == "XS/S/M")
{
if(val2 == "NATURAL/BLUSH")
{
document.getElementById('add').value = 987654321;
 
}
}
else if(val1 == "M/L/XL/XXL")
{
if(val2 == "NATURAL/BLUSH")
{
document.getElementById('add').value = 987654322;
}
}
function isValidForm(theForm) {
  if(theForm.mylist1.selectedIndex<1) {
    alert('Please select size');
    theForm.mylist1.focus();
    return false;
  }
  return true; // allow submit
}
}
</script>
</head>
<body onload="javascript:selectProduct();isValidForm();">
 
<form id="form1" method="POST" onsubmit="return isValidForm(this);" action="http://secure.ultracart.com/cgi-bin/UCEditor">
 
<input type="hidden" name="merchantId" value="SWISH" />
<input type="hidden" name="add" id="add" value="" />
<br />
<SELECT id="mylist1" NAME="mylist1" onchange="javascript:selectProduct();">
<OPTION VALUE="" >Select size:</OPTION>
<OPTION VALUE="XS/S/M" >XS/S/M</OPTION>
<OPTION VALUE="M/L/XL/XXL">M/L/XL/XXL</OPTION>
</SELECT>
<br />
<SELECT id="mylist2" NAME="mylist2" onchange="javascript:selectProduct();">
<OPTION VALUE="NATURAL/BLUSH" >NATURAL/BLUSH</OPTION>
</SELECT>
<br />
<input type="text" name="quantity" value="1" />
<br />
<input type="submit" name="submit" value="add to cart" />
</form>
</body>
</html>

Open in new window

0
Comment
Question by:Hiroyuki Tamura
[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
  • 9
  • 7
16 Comments
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24348544
Your function isValidForm is placed inside the function selectProduct. Remove it there and place it on its own.

As a friendly hint: it is good practice to indent your code, which would immediately show such errors. While indenting your code something else became apparent: the last "else" in your code is coded as a one-liner. It is better to put brackets around its body (matter of taste, but still).


function selectProduct(){
    var val1 = document.getElementById('mylist1').options[document.getElementById('mylist1').selectedIndex].value;
    var val2 = document.getElementById('mylist2').options[document.getElementById('mylist2').selectedIndex].value;
    var form1 = document.getElementById('form1');
    
    if (val1 == "XS/S/M") {
        if (val2 == "NATURAL/BLUSH") {
            document.getElementById('add').value = 987654321;
            
        }
    }
    else
        if (val1 == "M/L/XL/XXL") {
            if (val2 == "NATURAL/BLUSH") {
                document.getElementById('add').value = 987654322;
            }
        }
    
}
 
function isValidForm(theForm){
    alert('Please select size');
    if (theForm.mylist1.selectedIndex < 1) {
        alert('Please select size');
        theForm.mylist1.focus();
        return false;
    }
    return true; // allow submit
}

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24348557
that was to show how the function should be put. Here's a fix of the function itself, because the way you called it, it caused an error (you'll find the error reporting of Firefox quite instructive, it said "theForm has no properties", which says it all...)

function isValidForm(theForm){
    mylist = document.getElementById("mylist1");
    if (mylist.selectedIndex < 1) {
        alert('Please select size');
        mylist.focus();
        return false;
    }
    return true; // allow submit
}

Open in new window

0
 

Author Comment

by:Hiroyuki Tamura
ID: 24348582
now that it says "please select size:"
but it says all the time even after the page loaded.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function selectProduct(){
    var val1 = document.getElementById('mylist1').options[document.getElementById('mylist1').selectedIndex].value;
    var val2 = document.getElementById('mylist2').options[document.getElementById('mylist2').selectedIndex].value;
    var form1 = document.getElementById('form1');
    
    if (val1 == "XS/S/M") {
        if (val2 == "NATURAL/BLUSH") {
            document.getElementById('add').value = 987654321;
            
        }
    }
    else
        if (val1 == "M/L/XL/XXL") {
            if (val2 == "NATURAL/BLUSH") {
                document.getElementById('add').value = 987654322;
            }
        }
    
}
 
function isValidForm(theForm){
    alert('Please select size');
    if (theForm.mylist1.selectedIndex < 1) {
        alert('Please select size');
        theForm.mylist1.focus();
        return false;
    }
    return true; // allow submit
}
</script>
</head>
<body onload="javascript:selectProduct();javascript:isValidForm();">
 
<form id="form1" method="POST" onsubmit="return isValidForm(this);" action="http://secure.ultracart.com/cgi-bin/UCEditor">
 
<input type="hidden" name="merchantId" value="SWISH" />
<input type="hidden" name="add" id="add" value="" />
<br />
<SELECT id="mylist1" NAME="mylist1" onchange="javascript:selectProduct();" >
<OPTION VALUE="" >Select size:</OPTION>
<OPTION VALUE="XS/S/M" >XS/S/M</OPTION>
<OPTION VALUE="M/L/XL/XXL">M/L/XL/XXL</OPTION>
</SELECT>
<br />
<SELECT id="mylist2" NAME="mylist2" onchange="javascript:selectProduct();">
<OPTION VALUE="NATURAL/BLUSH" >NATURAL/BLUSH</OPTION>
</SELECT>
<br />
<input type="text" name="quantity" value="1" />
<br />
<input type="submit" name="submit" value="add to cart" />
</form>
</body>
</html>

Open in new window

0
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

 
LVL 39

Expert Comment

by:abel
ID: 24348628
> but it says all the time even after the page loaded.

I don't know why, and I didn't ask, but you have put the method in the page load event. Remove it and it will only say it when you try to submit it. Change this

<body onload="javascript:selectProduct();javascript:isValidForm();">
into this:

<body onload="javascript:selectProduct();">
and btw: you can remove the javascript: prefix. It is not a link.


0
 

Author Comment

by:Hiroyuki Tamura
ID: 24348783
thank you.
now that even i select size, still getting error message.
0
 

Author Comment

by:Hiroyuki Tamura
ID: 24348785
even i can jump to shopping website.
0
 

Author Comment

by:Hiroyuki Tamura
ID: 24348792
> and btw: you can remove the javascript: prefix. It is not a link.
where can i remove?
0
 

Author Comment

by:Hiroyuki Tamura
ID: 24348794
javascript:selectProduct();" to selectProduct();" ?
0
 

Author Comment

by:Hiroyuki Tamura
ID: 24348802
and i need to click twice to close the window
0
 
LVL 39

Expert Comment

by:abel
ID: 24348876
I'm sorry, I don't follow. You had a question, and I'd love to help you, but considering all your new questions, it seems that you actually want me to implement it for you. I love to explain you how something works, but I do not like to do the job for you.

You came up with a piece of code. I assume you wrote it. If you have problems with a specific line, I can help you. If you have questions on how something works, I can help you. So, please rephrase: what is your question really (you said "I want to give error message if empty", that is what the code does, after my changes)?
0
 
LVL 39

Expert Comment

by:abel
ID: 24348879
If you have questions about my last comment, I understand. You can read the FAQ about this subject here: http://www.experts-exchange.com/help.jsp?hi=23
0
 

Author Comment

by:Hiroyuki Tamura
ID: 24348903
sorry, i'm trying to learn how that works.
i'm focusing to solve why i need to click twice to close the window.
0
 
LVL 39

Expert Comment

by:abel
ID: 24348971
> sorry, i'm trying to learn how that works.
that's good :)

> i'm focusing to solve why i need to click twice to close the window.

can you explain me that? I don't need to click twice, I can just close the window by clicking the X button in the tab of my firefox / ie / opera.
0
 

Author Comment

by:Hiroyuki Tamura
ID: 24389159
thank you.
i figured out.
function isValidForm(theForm){
    alert('Please select size');
    if (theForm.mylist1.selectedIndex < 1) {
        alert('Please select size');
        theForm.mylist1.focus();
        return false;
}
 
function isValidForm(theForm){
//delete here
    if (theForm.mylist1.selectedIndex < 1) {
        alert('Please select size');
        theForm.mylist1.focus();
        return false;
}

Open in new window

0
 

Author Closing Comment

by:Hiroyuki Tamura
ID: 31581676
thank you.
0
 
LVL 39

Expert Comment

by:abel
ID: 24389268
Strange you missed that, that was actually in my first followup to the solution I gave, because I saw that double line there http:#24348557

glad you found it too now.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
Suggested Courses

770 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