Validate Selection box index in ASP and JavaScript

How would I validate a selectionbox using 2 different ways (ASP and JavaScript) in my ASP page

Just need the basics, forgot how to do so.
LVL 1
dba123Asked:
Who is Participating?
 
brgivensConnect With a Mentor Commented:
Here's a JS skeleton...

<script>
  function validate(o) {
    if (o.myselect.selectedIndex == 2) {alert("This weird form doesn't allow you to select 2."); o.myselect.focus(); return false;}
    return true;
  }
</script>
<form action="myasp.asp" method="post">
  <select name="myselect">
    <option>1</option>
    <option>2</option>
  </select>
  <input type="submit" onclick="return validate(this.form);">
</form>
0
 
dba123Author Commented:
here is my asp version which is not working
Function Validate

      If Request("CompanyID") = "Select Company Name" Then
               err1 = "Please enter a company"
               Validate = 0
                     response.write "Test0"
      Else
            response.write "Test1"
            Validate = 1
      End If

      End Function
0
 
sciber_dudeCommented:
It is probably not working coz you have not set the value of the CompanyId to "Select Company Name"

either change the form to this ...

    <select name="CompanyID">
        <option VALUE="Select Company Name">Select Company Name</option>
    </select>

OR

Function Validate

     If Request("CompanyID") = "" Then                               ' <<<<<<<<<<<<<<<<
             err1 = "Please enter a company"
             Validate = 0
                  response.write "Test0"
     Else
          response.write "Test1"
          Validate = 1
     End If

     End Function
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
sciber_dudeConnect With a Mentor Commented:
And btw, use Javascript to validate before the form gets submitted (the validating code is on the form)

And ASP to validate after the form gets submitted (The validating code is present along with the database-writing script).

:) SD
0
 
dba123Author Commented:
why does this not work

 function ValidateR(o) {
    if (o.CompanyID.selectedIndex == 0) {alert("Please select a Company"); o.CompanyID.focus(); return false;}

    else if
    if (o.ResourceID.selectedIndex == 0) {alert("Please select a Resource"); o.ResourceID.focus(); return false;}

    Else
    return true;
    End If

  }

but this does

 function ValidateR(o) {
    if (o.CompanyID.selectedIndex == 0) {alert("Please select a Company"); o.CompanyID.focus(); return false;}
    return true;
  }
0
 
brgivensCommented:
If Request.Form("CompanyID") = "Select Company Name" Then
0
 
dba123Author Commented:
How do I validate the selected index using ASP code, not JS
0
 
dba123Author Commented:
I'd rather checkt the selected index in both situations anyway, it is more efficient and prevents required maintenance if the select default value ever changes
0
 
brgivensConnect With a Mentor Commented:
confusing JS & vbS and you had an extra if in there

function ValidateR(o) {
    if (o.CompanyID.selectedIndex == 0) {alert("Please select a Company"); o.CompanyID.focus(); return false;}

    else if (o.ResourceID.selectedIndex == 0) {alert("Please select a Resource"); o.ResourceID.focus(); return false;}

    return true;
 
  }
0
 
sciber_dudeConnect With a Mentor Commented:
You are mixing up Javascript and VBscript while writing the ValidateR() function.

Try this...

function ValidateR(o)
{
    if (o.CompanyID.selectedIndex == 0)
           {alert("Please select a Company"); o.CompanyID.focus(); return false;}
    else if {
           if (o.ResourceID.selectedIndex == 0) {alert("Please select a Resource"); o.ResourceID.focus(); return false;}
           Else { return true;}
   }
}
0
 
brgivensConnect With a Mentor Commented:
While it's possible to pass the selected index to asp, it's a really bad idea... you should only pass the value you intend to store into the database.
0
 
dba123Author Commented:
>>It is probably not working coz you have not set the value of the CompanyId to "Select Company Name"

>>either change the form to this ...

I am using a selected value so there would be no way for there to be a null
0
 
dba123Author Commented:
Like I said, it would be better for me to check the selectedindex = 0 in ASP or JS since that is the most efficient than trying to check against a string value.
0
 
dba123Author Commented:
whoops, didn't see that extra if, thanks brgivens
0
 
dba123Author Commented:
so you must use an onclick in your submit button rather than an onsubmit if you are referencing a JS function?
0
 
brgivensCommented:
btw... the else isn't really needed... the only way the logic can get as far as the else is if the first validation check is ok... you'll find it a lot easier to read if you omit the else's when you can...

function ValidateR(o) {
    if (o.CompanyID.selectedIndex == 0) {alert("Please select a Company"); o.CompanyID.focus(); return false;}
    if (o.ResourceID.selectedIndex == 0) {alert("Please select a Resource"); o.ResourceID.focus(); return false;}
    return true;
  }
0
 
dba123Author Commented:
this still doesn't work

 function ValidateR(o) {
    if (o.CompanyID.selectedIndex == 0) {alert("Please select a Company"); o.CompanyID.focus(); return false;}
    return true;
    else if(o.ResourceID.selectedIndex == 0) {alert("Please select a Resource"); o.ResourceID.focus(); return false;}
      return true;
    End If

  }

<INPUT TYPE="Submit" Name="submit_button" VALUE="Update" onclick="return ValidateR(this.form);">
0
 
brgivensCommented:
actually, you're probably better off attaching the validation to onsubmit
0
 
dba123Author Commented:
thank you brgivens the above works now.

I hate going between JS and VB
0
 
brgivensCommented:
There is NO "End If" in JavaScript...
0
 
sciber_dudeCommented:
>> End If

doesnt exist in Javascript.
0
 
brgivensCommented:
Personally, I hate VB, but ASP seems to prefer it to JS for <sarcasm>some strange, unknown reason</sarcasm>, so what can you do?
0
 
dba123Author Commented:
I need an example in VB on how to check for the selectedindex and then I can close this post....that is what i am looking for.  I assume you can do the same in VB as you can in JS somehow where you check if the value selected is 0
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.