/    Sign up×
Community /Pin to ProfileBookmark

need to modify a validation script for muliple selections??

i’ve been using this form validation but now i need to update it so it only validates an html multiple list selection. i’ve tried to modify it but my attempts just don’t seem to work. can anyone help??

[CODE]<SCRIPT LANGUAGE=”JavaScript”>
<!– Begin form validation

function getCookie(name){
var cname = name + “=”;
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(“;”, begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}
function setCookie(name, value) {
var now = new Date();
var then = new Date(now.getTime() + 31536000000);
document.cookie = name + “=” + escape(value) + “; expires=” + then.toGMTString() + “; path=/”;
}
function getInfo(form) {
form.info.value = “Browser Information: ” + navigator.userAgent;
}
function getValue(element) {
var value = getCookie(element.name);
if (value != null) element.value = value;
}
function setValue(element) {
setCookie(element.name, element.value);
}
function fixElement(element, message) {
alert(message);
element.focus();
}
function isMailReady(form) {
var passed = false;
if (form.job_title_id[].selectedIndex ==0) {
fixElement(form.job_title_id[], “SELECTIONS: Please choose a ‘job title’.”);
}
else {
getInfo(form);
passed = true;
}
return passed;
}
// End –>
</SCRIPT>
[/CODE]

if it makes any difference… job_title_id[] …is what i’m trying to validate, and the reason i’m using square brackets [] is for php to capture multi selections as an array. and just to let you know …there is no select all option.

cheers!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@nitwitAug 25.2004 — The selectedIndex of an 'unselected' listbox is [b]-1[/b] - not zero. Unclear if you're testing for a default selection.

OK to use square brackets for PHP array generation - we all do it - but you can't use dot syntax in that case, as bracket characters are illegal in variable names.
<i>
</i>if (form.elements['job_title_id[]'].selectedIndex == -1) {

The .elements[] array reference is not the fix, it's the hashtable-style reference that does it. Just an added touch.?
Copy linkTweet thisAlerts:
@tarkaauthorAug 25.2004 — i feel a bit foolish here coz amended the script with your change but its not validating. did i change it correctly? here's the amended script....

oh yeah, i don't have a default selection

[CODE]<SCRIPT LANGUAGE="JavaScript">
<!-- Begin form validation

function getCookie(name){
var cname = name + "=";

var dc = document.cookie;

if (dc.length > 0) {

begin = dc.indexOf(cname);

if (begin != -1) {

begin += cname.length;

end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}
function setCookie(name, value) {
var now = new Date();
var then = new Date(now.getTime() + 31536000000);
document.cookie = name + "=" + escape(value) + "; expires=" + then.toGMTString() + "; path=/";
}
function getInfo(form) {
form.info.value = "Browser Information: " + navigator.userAgent;
}
function getValue(element) {
var value = getCookie(element.name);
if (value != null) element.value = value;
}
function setValue(element) {
setCookie(element.name, element.value);
}
function fixElement(element, message) {
alert(message);
element.focus();
}
function isMailReady(form) {
var passed = false;
if (form.elements['job_title_id[]'].selectedIndex == -1) {
fixElement(form.job_title_id[], "SELECTIONS: Please choose a 'job title'.");
}
else {
getInfo(form);
passed = true;
}
return passed;
}
// End -->
</SCRIPT>
[/CODE]
Copy linkTweet thisAlerts:
@nitwitAug 25.2004 — Oops. Read this again:...bracket characters are illegal in variable names[/QUOTE]
<i>
</i>if (form.elements['job_title_id[]'].selectedIndex == -1) {
fixElement(form.elements['job_title_id[]'], "SELECTIONS: Please choose a 'job title'.");
Copy linkTweet thisAlerts:
@tarkaauthorAug 25.2004 — THANKS Nitwit!!!
×

Success!

Help @tarka spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...