Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of brgivens
brgivens

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123
dba123

ASKER

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
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123

ASKER

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;
  }
If Request.Form("CompanyID") = "Select Company Name" Then
Avatar of dba123

ASKER

How do I validate the selected index using ASP code, not JS
Avatar of dba123

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123

ASKER

>>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
Avatar of dba123

ASKER

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.
Avatar of dba123

ASKER

whoops, didn't see that extra if, thanks brgivens
Avatar of dba123

ASKER

so you must use an onclick in your submit button rather than an onsubmit if you are referencing a JS function?
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;
  }
Avatar of dba123

ASKER

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);">
actually, you're probably better off attaching the validation to onsubmit
Avatar of dba123

ASKER

thank you brgivens the above works now.

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

doesnt exist in Javascript.
Personally, I hate VB, but ASP seems to prefer it to JS for <sarcasm>some strange, unknown reason</sarcasm>, so what can you do?
Avatar of dba123

ASKER

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