Results 1 to 4 of 4

Thread: Javascript IF Statements

  1. #1

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    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.

  2. #2

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    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>

  3. #3
    Member
    Join Date
    Jan 2003
    Posts
    44
    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)">

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    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
  •  



Click Here to Expand Forum to Full Width