Solved

Validate Selection box index in ASP and JavaScript

Posted on 2004-04-14
23
365 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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 …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now