How to make combo box required field?
Moderator: General Moderators
How to make combo box required field?
I have a combo box with 6 options
Choose an option
1
2
3
4
5
and I want the user to rate it from 1 to 5, but when user selects option "Choose an option" I want an error message to be displayed saying that you have to select from 1 to 5. How do I force the user to select these options 1 to 5?
Choose an option
1
2
3
4
5
and I want the user to rate it from 1 to 5, but when user selects option "Choose an option" I want an error message to be displayed saying that you have to select from 1 to 5. How do I force the user to select these options 1 to 5?
Some simple javascript (call it in the onSubmit event of the form):
Code: Select all
if(document.forms['formName'].elements['fieldName'].selectedIndex == 0) {
alert('You must select an option between 1 and 5');
return false;
}This is the wrong way to do such a simple task.
Just change your form to something like this:
Just change your form to something like this:
Code: Select all
<select name="name_here">
<optgroup label="Choose an option">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</optgroup>
</select>That doesn't entirely work as expected becuase Choose an option isn't the initially selected valueOren wrote:This is the wrong way to do such a simple task.
Just change your form to something like this:Code: Select all
<select name="name_here"> <optgroup label="Choose an option"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </optgroup> </select>
why is the default value 1 though ? This way the user may decide not to rate it and I will get the default value 1 in my email not knowing if he rated it 1 or didnt bother to rate it, which is worse because previously at least I knew that the user did not rate it.Oren wrote:This is the wrong way to do such a simple task.
Just change your form to something like this:Code: Select all
<select name="name_here"> <optgroup label="Choose an option"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </optgroup> </select>
Neither would I... That's why I used italic style on the word 'wrong'. As you said, "It's a valid solution to the problem." and I agree, I just don't think this is the way to do it. <optgroup> exists for a reason, any good web developer knows that.GM wrote:I wouldn't call it "wrong".
If a user doesn't really care and doesn't really want to rate, he would just click on one of the options randomly anyway (even though he could just leave it without rating it).maciek4 wrote:This way the user may decide not to rate it and I will get the default value 1 in my email not knowing if he rated it 1 or didnt bother to rate it
Edit:
maciek4 wrote:How do I force the user to select these options 1 to 5
You are opposing yourself here! You asked for a solution to force the user to rate, when you do that the user would just choose something randomly in order to be able to go on.maciek4 wrote:This way the user may decide not to rate it and I will get the default value 1 in my email not knowing if he rated it 1 or didnt bother to rate it, which is worse because previously at least I knew that the user did not rate it.
- harrisonad
- Forum Contributor
- Posts: 288
- Joined: Fri Oct 15, 2004 4:58 am
- Location: Philippines
- Contact:
common kids, this is no time for squabbling...
make sure you gave a null value to "Choose an option" option.
Code: Select all
<form name="frm" onsubmit="if(frm.name_here.value){submit();}else{alert('error: blah blah blah');}">
....Code: Select all
....
<option value="">Choose an option</option>
....The situation is as follows. At the moment it is not required to give rating and therefore a lot of the users do not rate. I want to force them to rate it. If they choose to rate it randomly just to move on then so be it but I believe that many who do not bother to rate it, will rate it honestly when being forced to. So the option of allowing them to rate it if they chose to failed because more than 80% do not rate it.Oren wrote:Neither would I... That's why I used italic style on the word 'wrong'. As you said, "It's a valid solution to the problem." and I agree, I just don't think this is the way to do it. <optgroup> exists for a reason, any good web developer knows that.GM wrote:I wouldn't call it "wrong".
If a user doesn't really care and doesn't really want to rate, he would just click on one of the options randomly anyway (even though he could just leave it without rating it).maciek4 wrote:This way the user may decide not to rate it and I will get the default value 1 in my email not knowing if he rated it 1 or didnt bother to rate it
Edit:maciek4 wrote:How do I force the user to select these options 1 to 5You are opposing yourself here! You asked for a solution to force the user to rate, when you do that the user would just choose something randomly in order to be able to go on.maciek4 wrote:This way the user may decide not to rate it and I will get the default value 1 in my email not knowing if he rated it 1 or didnt bother to rate it, which is worse because previously at least I knew that the user did not rate it.
Dude, anyone is free to say what he thinks. If two don't agree, it doesn't mean this is a squabble.harrisonad wrote:common kids, this is no time for squabbling...
Well, that's another story - now it makes more sense to use GM's solution. You can also use PHP to print the error.maciek4 wrote:but I believe that many who do not bother to rate it, will rate it honestly when being forced to.
If you have PHP, then do your validation in your script, it not very hard to return a form. If you can't control it, then don't ever trust it! The more client side restriction you make will make you code less safe! I see people do this a lot, validate with JavaScript and then don't do it in there script. JavaScript like Flash is a toy for silly display logic, both have no place in protecting a script, db or system!
pif!
pif!
I disagree with parts of your response, and what's more, this problem is being blown out of all proportion. This is a simple simple javascript solution to make a field on a form more difficult to ignore.printf wrote:If you have PHP, then do your validation in your script, it not very hard to return a form. If you can't control it, then don't ever trust it! The more client side restriction you make will make you code less safe! I see people do this a lot, validate with JavaScript and then don't do it in there script. JavaScript like Flash is a toy for silly display logic, both have no place in protecting a script, db or system!
pif!
JavaScript is there to stop the user having to wait for a round trip to the server. It is a courtesy to the end user to put a minimum client side validation on the form. No-one was ever suggesting using only client-side scripting to protect the field. What javascript provides in this case is a prominent warning to the user (an alert), with a clear message of what he needs to do BEFORE the form is submitted to the server.
I'm not suggesting for one minute that you substitute server-side validation with client-side validation. In this particular case, where maciek has stated that he wants to make the field obbligatory, the easiest way to do this is to put a client-side validation on it. If the user then selects randomly, that's their problem. As for server-side validation, in this case it is enough to check that the field contains either 1, 2, 3, 4, or 5.
Another thing is I have an onclick event for the rate field to be activated .Like this:
When an error occurs the field is blocked again and the checkbox is still checked. I dont want to have it blocked when the checkbox is checked. I would like it to be unblocked after the error occurs.
Code: Select all
<INPUT TYPE="checkbox" NAME="need[friend]" value="friend" onclick="javascript:document.order.importance.disabled=false"<? echo isset($need['friend']) ? 'checked' : ''; ?> > social reasons<br>Code: Select all
<select disabled="true" name="importance">
<option value="0" >-</option>
<option value="1" >1</option>
<option value="2" >2</option>
<option value="3" >3</option>
<option value="4" >4</option>
<option value="5" >5</option>
</select>