|
-
Apr 24th, 2003, 07:07 AM
#1
Thread Starter
Fanatic Member
Javascript IF Statements
Im new to Javascript and have written the follwowing and put it in my head tags.
<SCRIPT LANGUAGE="JavaScript">
function Remove(){
if (document.F_AMOUNT.value = "")
{
var response = alert("testing!!!");
return false
}
}
</script>
Later on in this page my function is called when submitting a form
<form method="post" action="TRAVEL_S005.asp" onSubmit="return Remove();">
the javascript should cature if one of my textboxes named F_AMOUNT is empty and if so not submit the form. It doesnt do anything at the moment and I think I have got the if staement wrong, can anybody help please.
-
Apr 24th, 2003, 08:13 AM
#2
Thread Starter
Fanatic Member
Javascript if statement <*Resolved*>
I used
<SCRIPT LANGUAGE="JavaScript">
function Remove(F_AMOUNT){
if ((F_AMOUNT.value.length==0) ||
(F_AMOUNT.value==null)) {
var response = alert("Sorry you need to\nenter a claim amount");
return false
}
}
</script>
-
Apr 24th, 2003, 10:42 AM
#3
Member
Just so you're aware, the real reason is because you also need to reference the form's name. What you have now may work in some browsers if there is only one form on the page. You also have a parameter for the function but are not using it. You also capture the response of the alert box but don't do anything with it.
Code:
<script type="text/javascript">
function chkfield(field)
{
if (!field) {
alert("You need to enter a claim amount");
return false;
}
else
return true;
}
</script>
.
.
.
<form action="TRAVEL_S005.asp" method="post" onsubmit="return chkfield(this.F_AMOUNT.value)">
-
Apr 26th, 2003, 08:39 AM
#4
Frenzied Member
You made a fairly common (and easily overlooked) error in your if statement: if (document.F_AMOUNT.value = ""). Tests for equality need "==", not "=".
While your statement is valid - it assigns a null string to document.F_AMOUNT.value and then evaluates document.F_AMOUNT.value for TRUE/FALSE - I don't think this is what you intended.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|