Page 1 of 1
How to make combo box required field?
Posted: Fri Jun 16, 2006 3:42 am
by maciek4
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?
Posted: Fri Jun 16, 2006 3:47 am
by GM
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;
}
Posted: Fri Jun 16, 2006 4:00 am
by Oren
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>
Posted: Fri Jun 16, 2006 4:06 am
by JayBird
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>
That doesn't entirely work as expected becuase
Choose an option isn't the initially selected value
Posted: Fri Jun 16, 2006 4:11 am
by GM
I wouldn't call it "wrong".
He states that he wants an error to appear, and your way would mean that you don't get to see the "Select an option" text until you've clicked on the field to open it.
It's a valid solution to the problem.
Posted: Fri Jun 16, 2006 4:12 am
by maciek4
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>
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.
Posted: Fri Jun 16, 2006 5:11 am
by Oren
GM wrote:I wouldn't call it "wrong".
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.
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
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).
Edit:
maciek4 wrote:How do I force the user to select these options 1 to 5
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.
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.
Posted: Fri Jun 16, 2006 5:51 am
by harrisonad
common kids, this is no time for squabbling...
Code: Select all
<form name="frm" onsubmit="if(frm.name_here.value){submit();}else{alert('error: blah blah blah');}">
....
make sure you gave a null value to "Choose an option" option.
Code: Select all
....
<option value="">Choose an option</option>
....
Posted: Fri Jun 16, 2006 6:04 am
by maciek4
Oren wrote:GM wrote:I wouldn't call it "wrong".
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.
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
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).
Edit:
maciek4 wrote:How do I force the user to select these options 1 to 5
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.
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.
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.
Posted: Fri Jun 16, 2006 7:56 am
by Oren
harrisonad wrote:common kids, this is no time for squabbling...
Dude, anyone is free to say what he thinks. If two don't agree, it doesn't mean this is a squabble.
maciek4 wrote:but I believe that many who do not bother to rate it, will rate it honestly when being forced to.
Well, that's another story - now it makes more sense to use
GM's solution. You can also use PHP to print the error.
Posted: Fri Jun 16, 2006 8:09 am
by printf
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!
Posted: Fri Jun 16, 2006 8:26 am
by GM
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!
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.
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.
Posted: Fri Jun 16, 2006 8:48 am
by maciek4
Another thing is I have an onclick event for the rate field to be activated .Like this:
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>
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.