Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Drop-down list in a form - I'd like it to be a required field

Featured Replies

Hi all,

 

For a form I'm creating I've been asked to include a drop-down list from which the user can select a County.

 

I would like it to be a required field and a message to pop up if it is left on 'Select a County' stating so.

 

Here's a form I created for a different website that demonstrates:

 

It's a PHP form.

 

And here is the code I have for my drop-down list:

 

                                <label>
                               <select name="County">
                                 <option>Select a County</option>
                                 <option>Derry</option>
                                 <option>Donegal</option>
                                 <option>Fermanagh</option>
                                 <option>Tyrone</option>
                               </select>
                               </label>

 

Thanks for reading!

 

Niall

You need to create a formCheck function which will verify that the user has completed all field on the form correctly. It is really very simple to do. Make sure that this function is called when the form is submitted.

 

function checkFrom(tform) {

//check the county

if (document.tform.County.selectedIndex == 0) {

alert("Please Select a County"); // this alert is displayed only if no selection made

return false;

}

tform.submit(); // submits the form

}

 

Make sure your submit button calls the checkForm function, something like this:

<input type="button" value="Submit" onClick="checkForm(this.form);">

 

Thats it.

 

Good Luck,

 

Ken

 

www.webdesigntips-foreveryone.com

 

*********************************************************

 

Hi all,

 

For a form I'm creating I've been asked to include a drop-down list from which the user can select a County.

 

I would like it to be a required field and a message to pop up if it is left on 'Select a County' stating so.

 

Here's a form I created for a different website that demonstrates:

 

It's a PHP form.

 

And here is the code I have for my drop-down list:

 

                                <label>
                               <select name="County">
                                 <option>Select a County</option>
                                 <option>Derry</option>
                                 <option>Donegal</option>
                                 <option>Fermanagh</option>
                                 <option>Tyrone</option>
                               </select>
                               </label>

 

Thanks for reading!

 

Niall

  • Author

You need to create a formCheck function which will verify that the user has completed all field on the form correctly. It is really very simple to do. Make sure that this function is called when the form is submitted.

 

function checkFrom(tform) {

//check the county

if (document.tform.County.selectedIndex == 0) {

alert("Please Select a County"); // this alert is displayed only if no selection made

return false;

}

tform.submit(); // submits the form

}

 

Make sure your submit button calls the checkForm function, something like this:

<input type="button" value="Submit" onClick="checkForm(this.form);">

 

Thats it.

 

Good Luck,

 

Ken

 

www.webdesigntips-foreveryone.com

 

*********************************************************

 

Thanks for your reply Ken (sorry I didn't reply sooner, but laptop had broken), unfortunately I still cannot get it to work.

 

I should've said that I want the drop-down list to accompany the other text fields (each prompting with a different message).

 

Here's a form I made with the drop-down list included.

 

onClick already has a function specified, how can I add checkForm(this.form);?

 

My client needs this for tomorrow so I hope you can further enlighten me soon!

  • Author

Sorry, I don't like to *bump* threads but if I can't get this working in the next couple of hours I'm in trouble.

I misnamed the function should have been checkForm(tform)

 

Normally what one does is put all Form checking logic within 1 function, like checkForm(tform).

That way when the user clicks the button to Submit the form all of the form elements will be checked within that one function.

 

On your button used to submit put an onclick to checkForm(tform).

Normally what I do is define the button is this way: <input type="button" onclick="checkForm(this.form)" value="Submit"> and let the function do the submit for me.

 

Hope this helps,

 

Ken

 

www.webdesigntips-foreveryone.com

  • Author

I misnamed the function should have been checkForm(tform)

 

Normally what one does is put all Form checking logic within 1 function, like checkForm(tform).

That way when the user clicks the button to Submit the form all of the form elements will be checked within that one function.

 

On your button used to submit put an onclick to checkForm(tform).

Normally what I do is define the button is this way: <input type="button" onclick="checkForm(this.form)" value="Submit"> and let the function do the submit for me.

 

Hope this helps,

 

Ken

 

www.webdesigntips-foreveryone.com

 

I still don't follow, I tried but can't seem to incorporate it into the form so that all fields (except for the optional one) are required.

 

Can you show me how the code should look as part of my existing form please?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.