on an email form the users email address and message needs to be required, how do I make it so it wont send the email unless they are filled in?
Thanks
| SitePoint Sponsor |





on an email form the users email address and message needs to be required, how do I make it so it wont send the email unless they are filled in?
Thanks

Well, the sending of the email has to be controlled on the server side, so assuming you're using PHP, you do
But I assume you mean you want the user to be told to fill it in properly without making the trip to the server. However, you still can't trust this and you must still check what the user has given you on the server side.PHP Code:if (empty($message) || empty($emailaddress)) {// return form to user to fill in properly}
You have to check whether the input fields have a value that is empty. You can make it more complex and check if the email is in the valid 'me@here.tld' format, but this is best done on the server side. This should be able to help you. However, you can also put the onsubmit bit from the form in your external JS file in the <head>, to make your HTML a bit cleaner.





thanks for your reply
first of all yes I am using PHP and PHPMailer to send the email but yes I want to check before i send to the server.
I have a javascript function like this:
and my form tag looks like thisCode:<SCRIPT TYPE="text/javascript"> function validateOnSubmit() { if(!document.forms.form1.email.value){ alert('Your email address is required'); } if(!document.forms.form1.question.value){ alert('You need to ask a question before you can send'); } }; </SCRIPT>
right now if i leave the email or question blank it does alert me telling me to fill them in but it still goes ahead and sends the email as well.HTML Code:<form onsubmit="validateOnSubmit()" action="" method="post" name="form1" id="form1">





Code:<SCRIPT TYPE="text/javascript"> function validateOnSubmit() { if(!document.forms.form1.email.value){ alert('Your email address is required'); return false; } if(!document.forms.form1.question.value){ alert('You need to ask a question before you can send'); return false; } return true; }; </SCRIPT> <form onsubmit="return validateOnSubmit();" action="" method="post" name="form1" id="form1">





@jimfraser
thanks that solved it.....i was pretty close.. just needed those returns!
thanks!

If you had read the link I posted, you would have noticed your function is lacking some return values. If the value is empty, you make it return false, if there is a value, then make it return true:
edit: damn.Code:<SCRIPT TYPE="text/javascript"> function validateOnSubmit() { if(!document.forms.form1.email.value){ alert('Your email address is required'); return false; } if(!document.forms.form1.question.value){ alert('You need to ask a question before you can send'); return false; } return true; }; </SCRIPT>





*hug*

You Chileans take everything from us!
(I'm Peruvian, where pisco is from)






Yeah, posting some code is always the best way.![]()





Lol @ pisco wars, both countries claim its origin, nobody really knows (although I suspect you are correct). The chileans are good at swiping stuff when noone's looking like Bolivia.
Pisco SOUR however is a British invention

Really? I didn't know that. Patriotism aside, I do think pisco is originally from Peru, from the wine-making region of Pisco. I think Peruvians are just bitter because Chile took the initiative to market it abroad.
Bookmarks