Racking My Brain!

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
funkymeerkat
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 4:16 am

Racking My Brain!

Post by funkymeerkat »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi, could someone help me out here. Probably really simple but not working.

I am using a javascript validation script on an asp(!!!) page for work. Forgetting the Asp side of things (it works on other pages) when i submit it should ask me to fill out the required fields if not done so.

Its fins on the text fields but the drop down menus just are not "requiring" an entry to be chosen....

Script

Code: Select all

<script>
 
function checkrequired(which){
var pass=true
if (document.images){
for (i=0;i<which.length;i++){
var tempobj=which.elements[i]
if (tempobj.name.substring(0,8)=="required"){
if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj.selectedIndex==-1)){
pass=false
break
}
}
}
}
if (!pass){
alert("One or more of the required fields are not completed. Please complete them, then submit again!")
return false
}
else
return true
}
</script>
Part of page having the problem...

Code: Select all

    <select name="requiredFunctional" accesskey="7" tabindex="7">
                    <option selected>Select One</option>
        <option value="Customer Service">Customer Service</option>
                    <option value="Finance">Finance</option>
                    <option value="Human Resources">Human Resources</option>
                    <option value="Information Technology">Information Technology</option>
                    <option value="Manufacturing">Manufacturing</option>
                    <option value="Operations">Operations</option>
                    <option value="Procurement">Procurement</option>
                    <option value="R&D">R&D</option>
                    <option value="Sales">Sales</option>
                  </select>
If you leave the field as it is, selected on "select one", it submits fine with empty field. Not what i want.

Any ideas?


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
Loki
Forum Commoner
Posts: 25
Joined: Wed Jun 03, 2009 9:23 pm

Re: Racking My Brain!

Post by Loki »

Have you tried setting the value of "Select One" specifically to NULL, or value=""?
funkymeerkat
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 4:16 am

Re: Racking My Brain!

Post by funkymeerkat »

Yep tried both - no joy....a right pain in the ....
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Racking My Brain!

Post by pickle »

Have you tried just checking the value of the <select>, rather than doing all that fancy logic to determine if it's a <select> in the first place?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
NatalieM
Forum Newbie
Posts: 1
Joined: Sat Jun 20, 2009 4:44 pm

Re: Racking My Brain!

Post by NatalieM »

On line 9, shouldn't you check that the selectedIndex = 0 (ie your first option, which is the "select one" option tag), instead of = -1?
Post Reply