...

View Full Version : Javascript If statement



kerrymorris
03-20-2012, 04:05 PM
I am looking to validate a drop down list box (Motor_Frame), so when "Num" is selected from it, a value must be entered into the Motor_Frame_num text box.

I am trying to use the following code but its not working:

<script type="text/javascript">
function validate_form (){
Motor_Frame = document.getElementById('Motor_Frame');
if(Motor_Frame.value.toString() != "Num")
else if (Motor_Frame_num.value!="" )
{ alert("Please fill in Motor Frame Number with the correct value, a value between 1 and 500")
document.forms[0].Motor_Frame_num.focus();
return false;
}
return true;
}


</script>

Lerura
03-20-2012, 04:27 PM
You are missing a statement block before the else
Even if you want nothing to happen if the conditionis true, it have to be there.

change
if(Motor_Frame.value.toString() != "Num") to
if(Motor_Frame.value.toString() != "Num"){}

And Motor_Frame.value is already a string
so this line could be shortened to
if(Motor_Frame.value != "Num"){}

You do also have a space between the function name and the parenthesis, that can cause troubles.

try using this
function validate_form(){
Motor_Frame = document.getElementById('Motor_Frame');
if(Motor_Frame.value != "Num"){}
else if (Motor_Frame_num.value!="" )
{ alert("Please fill in Motor Frame Number with the correct value, a value between 1 and 500")
document.forms[0].Motor_Frame_num.focus();
return false;
}
return true;
}

Philip M
03-20-2012, 05:21 PM
Motor_Frame = document.getElementById('Motor_Frame');

Do not use the same name for a Javascript variable and an HTML element.

var MFrame = document.getElementById('Motor_Frame');

kerrymorris
03-20-2012, 09:05 PM
I have tried that code but i keep getting "query was empty" when saving my record to database. I am using php code....any ideas???

Old Pedant
03-20-2012, 09:14 PM
<script type="text/javascript">
function validate_form ()
{
var form = document.forms[0];
if ( form.Motor_Frame.value == "Num" && form.Motor_Frame_num.value = "" )
{
alert("Please fill in Motor Frame Number with the correct value, a value between 1 and 500")
form.Motor_Frame_num.focus();
return false;
}
return true;
}


BUT... But that doesn't check to see of Motor_Frame_num is actually between 1 and 500!

So:


<script type="text/javascript">
function validate_form ()
{
var form = document.forms[0];
if ( form.Motor_Frame.value != "Num" ) return true;
var MFn = parseInt(form.Motor_Frame_num.value);
if ( isNaN(MFn) || MFn < 1 || MFn > 500 )
{
alert("Please fill in Motor Frame Number with the correct value, a value between 1 and 500")
form.Motor_Frame_num.focus();
return false;
}
return true;
}

kerrymorris
03-20-2012, 09:41 PM
that code worked an the error appeared when i didnt enter anything into the motor_frame_num field but when i clicked save i still get Query was empty.....????

Old Pedant
03-20-2012, 09:57 PM
So now you have a PHP problem. Likely nothing to do with JavaScript.

Time to learn how to debug PHP. And you probably need to post in the PHP forum.

webdev1958
03-21-2012, 02:34 AM
that code worked an the error appeared when i didnt enter anything into the motor_frame_num field but when i clicked save i still get Query was empty.....????

All user inputs must be validated in your server side script before using it further down.

Client side validation (javascript) is a "nice to have" but not a "must have" since it can easily be turned off or bypassed alltogether by the user.

Near the top of your php script, first make sure all sent data has been corectly received and contains only valid data. Only after all the expected data has been received and validated do you proceed with your php script.

If any expected data is not received or invalid, display an appropriate error message, redirect back to the <form> or whatever and then terminate the php script.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum