Solved

Validate Selection box index in ASP and JavaScript

Posted on 2004-04-14
23
367 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:dba123
  • 11
  • 8
  • 4
23 Comments
 
LVL 7

Accepted Solution

by:
brgivens earned 300 total points
ID: 10828017
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
 
LVL 1

Author Comment

by:dba123
ID: 10828137
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
 
LVL 11

Expert Comment

by:sciber_dude
ID: 10828193
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 11

Assisted Solution

by:sciber_dude
sciber_dude earned 200 total points
ID: 10828200
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
 
LVL 1

Author Comment

by:dba123
ID: 10828213
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
 
LVL 7

Expert Comment

by:brgivens
ID: 10828223
If Request.Form("CompanyID") = "Select Company Name" Then
0
 
LVL 1

Author Comment

by:dba123
ID: 10828227
How do I validate the selected index using ASP code, not JS
0
 
LVL 1

Author Comment

by:dba123
ID: 10828235
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
 
LVL 7

Assisted Solution

by:brgivens
brgivens earned 300 total points
ID: 10828239
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
 
LVL 11

Assisted Solution

by:sciber_dude
sciber_dude earned 200 total points
ID: 10828261
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
 
LVL 7

Assisted Solution

by:brgivens
brgivens earned 300 total points
ID: 10828262
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
 
LVL 1

Author Comment

by:dba123
ID: 10828264
>>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
 
LVL 1

Author Comment

by:dba123
ID: 10828268
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
 
LVL 1

Author Comment

by:dba123
ID: 10828279
whoops, didn't see that extra if, thanks brgivens
0
 
LVL 1

Author Comment

by:dba123
ID: 10828292
so you must use an onclick in your submit button rather than an onsubmit if you are referencing a JS function?
0
 
LVL 7

Expert Comment

by:brgivens
ID: 10828304
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
 
LVL 1

Author Comment

by:dba123
ID: 10828318
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
 
LVL 7

Expert Comment

by:brgivens
ID: 10828325
actually, you're probably better off attaching the validation to onsubmit
0
 
LVL 1

Author Comment

by:dba123
ID: 10828329
thank you brgivens the above works now.

I hate going between JS and VB
0
 
LVL 7

Expert Comment

by:brgivens
ID: 10828331
There is NO "End If" in JavaScript...
0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 10828338
>> End If

doesnt exist in Javascript.
0
 
LVL 7

Expert Comment

by:brgivens
ID: 10828343
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
 
LVL 1

Author Comment

by:dba123
ID: 10829786
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question