SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    Pragmatic Programmer halfasleeps's Avatar
    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
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    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} 
    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.

    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.

  3. #3
    Pragmatic Programmer halfasleeps's Avatar
    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>
    and my form tag looks like this
    HTML Code:
    <form onsubmit="validateOnSubmit()" action="" method="post" name="form1" id="form1">
    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.
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  4. #4
    SitePoint Wizard
    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">

  5. #5
    Pragmatic Programmer halfasleeps's Avatar
    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!
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    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>
    edit: damn.

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *hug*

  8. #8
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    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 )

  9. #9
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    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:
    thanks for your help, there was a lot to read on that page and all of the information together was confusing me. The way jimfraser added the returns into my code made it very easy to understand.

    thanks to both of you!
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  10. #10
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    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.

  11. #11
    SitePoint Wizard
    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

  12. #12
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •