/    Sign up×
Community /Pin to ProfileBookmark

Selction OPtion

Hi

I’m trying to write code that will test and make sure the a selection option is not blank and if it is it gives an alert.

I use the below with text objects but having a hard time making same work with selction option

thank you for any help

[code]
if(isNaN(cFreq)){
alert(‘Loan Compounding Frequency Needed’)
document.lPmt.intComp.focus()
return;
}

[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceMar 13.2006 — You basically have to provide a dummy option as the first option of your SELECT. Then, use the following code to determine if the visitor has changed this selection:
<i>
</i>var sel = document.forms["formName"].elements["elementName"];
if (sel.selectedIndex &lt;= 0) {
alert("No option selected.");
sel.focus();
return false;
}

[code=html]
<form name="formName" action="">
<fieldset>
<select name="elementName" size="1">
<option value="">[make a selection]</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</fieldset>
</form>
[/code]
Copy linkTweet thisAlerts:
@prasanth_jvrsMar 13.2006 — Hi Hope this script helps.

<html>

<body>

<form name=f action="1.php">

Enter your name <input type=text name="username">

Enter your hobbies: <select name="hobbies">

<option value="">Select any option

<option value="cricket">Cricket

<option value="hockey">Hockey

<option value="chess">chess

</select>

<input type=button onclick="validate()" value="Submit">

</form>

</body>

</html>

<script>

function validate() {

var val=document.forms[0].username.value;

var seloptvalue=document.forms[0].hobbies[document.forms[0].hobbies.selectedIndex].value;

if(val==null || val==undefined || val.length==0 || val=="") {

alert("Enter name");

return false;

}

if(seloptvalue==null || seloptvalue==undefined || seloptvalue.length==0 || seloptvalue=="") {

alert("Select any option");

return false;

}

document.forms[0].submit();

}

</script>

Thanks

Prasanth J.V.R.S
×

Success!

Help @kproc 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...