SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
Thread: required form fields
-
Feb 2, 2007, 09:16 #1
- Join Date
- Feb 2006
- Location
- Altoona, PA. USA
- Posts
- 1,945
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
required form fields
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
-
Feb 2, 2007, 09:36 #2
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Well, the sending of the email has to be controlled on the server side, so assuming you're using PHP, you do
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.
-
Feb 2, 2007, 09:53 #3
- Join Date
- Feb 2006
- Location
- Altoona, PA. USA
- Posts
- 1,945
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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:
Code:<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>
HTML Code:<form onsubmit="validateOnSubmit()" action="" method="post" name="form1" id="form1">
-
Feb 2, 2007, 10:02 #4
- Join Date
- Nov 2004
- Location
- Nelson BC
- Posts
- 2,310
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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">
-
Feb 2, 2007, 10:06 #5
- Join Date
- Feb 2006
- Location
- Altoona, PA. USA
- Posts
- 1,945
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
@jimfraser
thanks that solved it.....i was pretty close.. just needed those returns!
thanks!
-
Feb 2, 2007, 10:06 #6
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
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:
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>
-
Feb 2, 2007, 10:23 #7
- Join Date
- Nov 2004
- Location
- Nelson BC
- Posts
- 2,310
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
*hug*
-
Feb 2, 2007, 11:00 #8
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
You Chileans take everything from us!
(I'm Peruvian, where pisco is from)
-
Feb 2, 2007, 11:02 #9
- Join Date
- Feb 2006
- Location
- Altoona, PA. USA
- Posts
- 1,945
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Feb 2, 2007, 11:06 #10
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Yeah, posting some code is always the best way.
-
Feb 2, 2007, 12:27 #11
- Join Date
- Nov 2004
- Location
- Nelson BC
- Posts
- 2,310
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Feb 3, 2007, 07:08 #12
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
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