ASP Question: stopping a forms action.

Discussion in 'OT Technology' started by Scream_Phoenix, Jan 31, 2003.

  1. Scream_Phoenix

    Scream_Phoenix Handsome Boy Model

    Joined:
    May 31, 2001
    Messages:
    45,119
    Likes Received:
    158
    Location:
    Capital City
    as in i have to validate a text box, so if the field is empty i display a message box saying please complete the field. that much works but the the form submits anyway after i click ok on the msgbox.

    here is the code that matters:

    Code:
    <script language=VBScript>
        sub frmUser_onSubmit
            if document.frmUser.FirstName.value= "" then
                call msgbox("please fill in all fields", 0)
                 [COLOR=red]i need something like frmUser.action = cancel here but i dont know the syntax or method :([/COLOR] 
            end if
        end sub
    </script>
    
    <form name="frmUser" action="UpdateMember.asp" method="post">
    First Name:<INPUT TYPE="Text" NAME="FirstName" VALUE="" SIZE="40"><br>
    <INPUT TYPE="Submit" NAME="cmdSave" VALUE="Save">
    
    
    
    i thought about having another abstract asp page that would do the validating and then redirect to updatemember.asp if the field is good or returning to the previous screen if the field is empty, but it seems like there should be a way of doing this with out an extra page. thanks in advance :)
     
  2. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    hmmm, if you use JavaScript you can say "return false;"

    try just putting "exit function" and see what happends. :dunno:
     
  3. Scream_Phoenix

    Scream_Phoenix Handsome Boy Model

    Joined:
    May 31, 2001
    Messages:
    45,119
    Likes Received:
    158
    Location:
    Capital City
    my teacher gave me an example of how to do it with js, but i dont want to just copy it in. i added exit sub to where the red is, then i removed "action="updatemember.asp"" and put a redirect to updatemember.asp under the function. but now it just stays on the same page all the time.
     
  4. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Public Service Announcement: Clientside VBScript is only good in IE. JavaScript you at least catch IE, Mozilla, Safari, Netscape, Lynx, Opera, and many more browsers. With that in mind, the rest of this post is in JavaScript. Either convert it to VBScript or learn JavaScript (shouldn't be difficult either way).

    PHP:

    <script language="javascript">

    function 
    frmUser_onSubmit()
    {
        if(
    document.frmUser.FirstName.value == "")
            {
            
    alert("Please fill in all fields");
            return 
    false;                // important!
            
    }  // end if(document.frmUser.FirstName.value ==)
        
    return true;                    // also very important!
    }  // end function frmUser_onSubmit()
    </script>

    <
    form name="frmUser" action="UpdateMember.asp" method="post" onSubmit="return frmUser_onSubmit();">


    Several notes here:

    1. Because you are in a function, use the JavaScript return to exit out. But exit out with either true/false.

    2. Set the form attribute onSubmit to the function you have setup to do your validation. But you need to include the "return" so the true/false from your function makes it back to the form object (no return and the browser just keeps on going).

    3. VERY VERY VERY important is if you are writing server side ASP is to validate all your data before doing anything with it (along with client side validation). Granted, it depends on the situation. If you are strictly collecting data for email, then its no big deal. But if its data to be used on the command prompt or in the database, you will want to ensure the web users can't botch up your server or get ugly SQL errors.

    4. Why are you using clientside VBScript? Where in the W3C does it say its a web standard?
     
  5. Scream_Phoenix

    Scream_Phoenix Handsome Boy Model

    Joined:
    May 31, 2001
    Messages:
    45,119
    Likes Received:
    158
    Location:
    Capital City
    thanks for the help. im still very new to web programming, having the script run at client was an over sight. i meant to have it run at server. :sad2:
     

Share This Page