Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Old 11-06-2004, 06:11 AM Required field
Spud In Disguise's Avatar
Super Spam Talker

Posts: 808
Location: UK... where else?
Trades: 0
Hello there. I have three fields in a feedback form that I would like to be required fields (so the form cannot be submitted without filling them in). It's really annoying me because I have marked them as needing to be filled in but people still don't bother (if you new the circumstances you would think it was really stupid too).

I have read through all my WD books but cannot find anything.

All I want is when the rest button is pressed it pops up an alert box saying something like: "please fill in al required fields".

Please help, and also to do this will I need to make any changes to the code of the submit button or just the fields?
__________________

Please login or register to view this content. Registration is FREE
- Fansite

Please login or register to view this content. Registration is FREE
- Blog
Spud In Disguise is offline
Reply With Quote
View Public Profile Visit Spud In Disguise's homepage!
 
 
Register now for full access!
Old 11-06-2004, 06:56 AM
sdcdesign.co.uk's Avatar
Extreme Talker

Posts: 198
Location: High Wycombe, Buckinghamshire, London
Trades: 0
hello there,

ok, add this script to your head section:

Code:
<script language="JavaScript">

<!--

function validate_form ( )
{
    valid = true;

    if ( document.form.firstname.value == "" )
    {
        alert ( "Please fill in the 'Your Name' box." );
        valid = false;
    }

    return valid;
}

//-->

</script>

then this will be how your form looks:

Code:
<form name="form" method="post" action="nextpage" onSubmit="return validate_form ( );">


<h1>Please Enter Your Name</h1>

<p>Your Name: <input type="text" name="firstname"></p>
<p><input type="submit" name="send" value="Send Details"></p>

</form>
Firstly, don't change anything in the initial form tag, but you can chage the method and the action!

When adding more fields, you must add this to the javascript:

Code:
    if ( document.form.firstname.value == "" )
    {
        alert ( "Please fill in the 'Your Name' box." );
        valid = false;
    }
So, when you add an <input type="text" name="fieldname"> and you want to validate it, you need to add the javascript above. If you want to do more with it, just search on google.


Hope i can help and reply if u need any more help!

Regards,

Sean
__________________
[ Insert witty, yet highly intelligent signature here ]
sdcdesign.co.uk is offline
Reply With Quote
View Public Profile Visit sdcdesign.co.uk's homepage!
 
Old 11-06-2004, 09:25 AM
Skilled Talker

Posts: 65
Trades: 0
while this is a good solution, it wont work if somebody has javascript disabled (which is true for more and more surfers nowadays).
In general it's better to verify forms server-side if you don't mind modifying your current php/perl script. A simple
Code:
if ($requiredformfield != '') {
//submit form
}
else
{
//error message
}
should do
__________________

Please login or register to view this content. Registration is FREE
mxlabs is offline
Reply With Quote
View Public Profile Visit mxlabs's homepage!
 
Old 11-06-2004, 12:16 PM
Spud In Disguise's Avatar
Super Spam Talker

Posts: 808
Location: UK... where else?
Trades: 0
I cannot do that since my code is hosted by bravenet.

Also with the first example:

I have three fields named "businessname", "businesscategory" and "businessnotes". How do I use the 1st example to make all these required?
__________________

Please login or register to view this content. Registration is FREE
- Fansite

Please login or register to view this content. Registration is FREE
- Blog

Last edited by Spud In Disguise; 11-06-2004 at 12:45 PM..
Spud In Disguise is offline
Reply With Quote
View Public Profile Visit Spud In Disguise's homepage!
 
Old 11-06-2004, 01:39 PM
sdcdesign.co.uk's Avatar
Extreme Talker

Posts: 198
Location: High Wycombe, Buckinghamshire, London
Trades: 0
hey, this would go in the head section...

Code:
<script language="JavaScript">

<!--

function validate_form ( )
{
    valid = true;

    if ( document.form.businessname.value == "" )
    {
        alert ( "Please fill in your Business name." );
        valid = false;
    }

    if ( document.form.businesscategory.value == "" )
    {
        alert ( "Please fill in your business Category." );
        valid = false;
    }

    if ( document.form.businessnotes.value == "" )
    {
        alert ( "Please fill in your Business Notes" );
        valid = false;
    }

    return valid;
}

//-->

</script>
And this would be your form line:

Code:
<form name="form" method="post" action="nextpage" onSubmit="return validate_form ( );">
then you can put all the field names etc. in.

Let me know how it goes!

Regards,

Sean
__________________
[ Insert witty, yet highly intelligent signature here ]
sdcdesign.co.uk is offline
Reply With Quote
View Public Profile Visit sdcdesign.co.uk's homepage!
 
Old 11-06-2004, 02:40 PM
Spud In Disguise's Avatar
Super Spam Talker

Posts: 808
Location: UK... where else?
Trades: 0
Thanks, I'll let you know how it went via PM
__________________

Please login or register to view this content. Registration is FREE
- Fansite

Please login or register to view this content. Registration is FREE
- Blog
Spud In Disguise is offline
Reply With Quote
View Public Profile Visit Spud In Disguise's homepage!
 
Reply     « Reply to Required field
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24110 seconds with 12 queries