August 27, 200917 yr 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
August 28, 200917 yr 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
August 31, 200917 yr 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!
September 1, 200917 yr 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.
September 1, 200917 yr 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
September 1, 200917 yr 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