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!

stop menu from proceeding if errors. 2

Status
Not open for further replies.

jfdabiri

MIS
Feb 27, 2007
282
0
0
US
hi,
i have an asp/vbs page the users enter information to proceed to the next page after pressing submit button. is there any way to pop a message if they entered invalid information? and keep them from proceeding to the next page?
thanks.
 
You can do client-side validation with JavaScript. To do this, start by writing a function that checks the form values and returns true or false if the input is good or bad. Then call this function from the onSubmit event of your form.

You should also do server-side validation on the form that processes the submitted form values. If only do one or the other, then server-side is the one to do... if the server-side validation fails then change the HTML response so it shows the error message and does not send the next submit button... or better yet send them back to the form with the failed fields hightlighted in some manner like with red text.
 
here's my code:
Code:
<html>       
   </head>
   <body>
<form name="frm1" method="POST" action="stats_listing.asp" target="_self">     
    <SELECT NAME="facility" SIZE="1">    
    <OPTION>Select a facility</option>  
    <OPTION>CRJ</option>       
    <OPTION>ERJ</option>
    <OPTION>NCRJ</option> 
    <OPTION>-------------------------------</option>              
    </SELECT> 
    
    <SELECT NAME="quarter" SIZE="1">
    <OPTION>Select a Quarter</option>  
    <OPTION>Q1</option>     
    <OPTION>Q2</option> 
    <OPTION>Q3</option>     
    <OPTION>Q4</option>    
    </SELECT>
        
    <input type="submit" value="Submit" name="Submit">  
    </form> 
</body>
 
</html>   

((((((( edits: to make sure the they have selected a facility and a quarter ))))))))
i'm familiar with vbs but where do i place it and how do i keep this form from proceeding to stats_listing.asp?
thanks.
 
The client side script might be structured something like this: (changes in [blue]blue[/blue])
Code:
<html> 
  <head>
[b][blue]    <script>
     function SubmitOK()
     {
       var bOK = true;

       // Here you insert some JavaScript to set
       // bOK to false if the form elements are
       // in an invalid state.  For the same of 
       // demonstratin lets just hardcode it false!
       bOK = false;

       if (bOK == true) 
       { return true; 
       }
       else
       { alert('bad input');
         return false;
       }
     }
    </script>  [/blue][/b]
  </head>
  <body>
    <form name="frm1" 
          method="POST" 
          action="stats_listing.asp" 
          target="_self"
          [b][blue]onSubmit="return SubmitOK();"[/blue][/b]>  
      <SELECT NAME="facility" SIZE="1">    
        <OPTION>Select a facility</option> 

        [...]

So obvioulsy this SubmitOK is hard coded to always return false ... you'll want to customize this functin so that it validtes your form elements. If you have questions about syntax check the many online resources, including the Tek-tips JavaScript forum: forum216
 


jf- here is some client side code I use for form validation:

Code:
function validate_form( )
{
	valid = true;
        if (document.registration_form.fname.value == "" )
        {
                alert ( "Please fill in the 'First Name' box." );
                valid = false;
        }

        if ( document.registration_form.lname.value == "" )
        {
                alert ( "Please fill in the 'Last Name' box." );
                valid = false;
        }

        return valid;
}

Put as many if conditions as you want in there.

Sheco- could you say a little more about doing server side validation. I would like to do something in that regard. (I also need to do something about SQL injection but that is a whole 'nuther topic.)

Thanks.
 
sheco and big red,
thanks so much. i think i'm on the right track now.
cheers.
 
Since i'm not familiar with javascript, i ran into problem. the menu kept going to the next screen.
here's the code i put in for above:
Code:
if (document.frm1.facility.value == "Select a Facility" )
        {
            alert ( "Please fill in the 'Facility' box." );
            bOK = false;
        }
are my variables right?
 

Code:
if (document.frm1.facility.value == "Select a Facility" )
        {
            alert ( "Please fill in the 'Facility' box." );
            bOK = false;
        }

frm1 is the name of your form.
facility is the name of the form question.

You did remember the
onSubmit="return function_name_here();
that sheco included and I left out, right?

I used the variable 'valid' and set that to false. You seem to be using sheco's bOK. It was set to true and/or changed to false? If you mashed two scripts togetehr you may need to give it an extra proof.

You might try it with value == "" .

I'm not a Javascript expert. I nicked that bit of code from somewhere ages ago and just recycle it cause it does what I want.

 
thanks bigred,
document.frm1.facility is not a text box, it's an option/select pulldown menu as i have listed above. somehow, it's not checking the value. i even tried to alert the value. the value is blank.
my question is, what should i code for document.frm1.facility value?
thanks.
 
Again, no way a Javascript expert but I think you might need:

document.frm1.facility.options[document.frm1.facility.selectedIndex].value == "Select a Facility"

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top