newbieBells
01-20-2006, 06:57 AM
This is my problem: State is mandatory in my form and the javascript alerts members if they have not selected a state. Now I changed my state select field to a multiple selection which made an error in javascript already. The state has become an array already and my simple script does not work anymore.
My previous code:
if (document.PostForm.state.value == "")
{
window.alert("State is mandatory. Please select a state.");
document.Postform.state.focus();
return false;
}
I have tried create a new function that would get the values from my multiple select field and if ever there are no values selected, the script should prompt the user that state is mandatory and they should select at least one. I've tried but none are correct.. Hehe.. Could anyone please help me.. Just a newbie in javascript...
This is the code that my friend and I did.
function checkStates() {
var NumState = 0;
document.PostForm.hid_state.value = "";
for(var i=0; i< document.PostForm.state.options.length; i++) {
if(document.PostForm.state.options[i].selected ) {
NumState = NumState + 1;
document.PostForm.hid_state.value = document.PostForm.hid_state.value + "|" + document.PostForm.state.options[i].value
}
}
if ( NumState == 0 ) {
window.alert('Enter your state, please!');
document.PostForm.state.focus();
return false;
}
else {
return true;
}
Please help! Thanks!!!
My previous code:
if (document.PostForm.state.value == "")
{
window.alert("State is mandatory. Please select a state.");
document.Postform.state.focus();
return false;
}
I have tried create a new function that would get the values from my multiple select field and if ever there are no values selected, the script should prompt the user that state is mandatory and they should select at least one. I've tried but none are correct.. Hehe.. Could anyone please help me.. Just a newbie in javascript...
This is the code that my friend and I did.
function checkStates() {
var NumState = 0;
document.PostForm.hid_state.value = "";
for(var i=0; i< document.PostForm.state.options.length; i++) {
if(document.PostForm.state.options[i].selected ) {
NumState = NumState + 1;
document.PostForm.hid_state.value = document.PostForm.hid_state.value + "|" + document.PostForm.state.options[i].value
}
}
if ( NumState == 0 ) {
window.alert('Enter your state, please!');
document.PostForm.state.focus();
return false;
}
else {
return true;
}
Please help! Thanks!!!