Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Checking in Javascript? 1

Status
Not open for further replies.

Tr0

Programmer
Jul 12, 2007
24
US
I'm new to JavaScript. I am trying to do some error checking on a page. The goal is when a user hits the submit key it checks for a certain option value. If that value is the default value it will alert the user to select another option.

The problem is i'm doing this on an asp file that was written about 5 or 6 six years ago and horribly coded. Basically I just want to know if my JavaScript is correct or not so I can determine where its breaking.

Here's the code:

- JavaScript -

<script type="text/javascript">
function focus()
{
document.Frontpage_Form1.operation_name.focus();
}

function checkSelect()
{
if (document.Frontpage_Form1.operation_name.value = "1")
{
alert("Please Select a Location / Operation type!");
document.Frontpage_Form1.operation_name.focus();return(false);
}
}

</script>

- HTML -

<form method="POST" action="parts_build.asp?part=<%=****_partnumber%>" name="FrontPage_Form1" onsubmit="return checkSelect(this)" language="JavaScript">

<select size="1" name="operation_name">
<option value="1">Please Select</option>
<%
Do Until RSLookUp5.EOF
%>
<option value="<%=RSLookUp5("shop_num")%>"><%=RSLookUp5("machine_location")%>
. . . <%=RSLookUp5("operation")%></option>
<%
RSLookUp5.MoveNext
Loop
%>
</select>

Thanks.

 
You've just about got it.

if (document.Frontpage_Form1.operation_name.value = "1")

should be:
Code:
if (document.Frontpage_Form1.operation_name.value == "1")
the single '=' sets the value to 1 (would always report true)
the double '==' compares the values


and the onsubmit call should be:
Code:
onsubmit="return checkSelect();"
you only send in variables if the function is expecting variables.

I feel for you... old asp AND Frontpage code?? OUCH!
 
I'm sorry... that was untested. The logic makes sense, but the references to the input could be better. I would give the input an id and refer to it like this:

Code:
<script type="text/javascript">
    //function focus()
    // you do not need this function for anything.
    //{
    //    document.getElementById("operation_name")..focus();
    //}
    
    function checkSelect()
    {
	if (document.getElementById("operation_name").value == "1")
        {
            alert("Please Select a Location / Operation type!");
            document.getElementById("operation_name").focus();
	    return false;
        }
    }

</script>

that obviously assumes that you give it "operation_name" for an id. (<select size="1" id="operation_name" name="operation_name">)

I hope this helps. It should work.
 
Thanks man! It still doesn't work, but it tells me that its something on the back end. Just wanted to make sure my JS was right.

Thanks again.
 
Awesome, the second reccomendation works great!

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top