Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    42
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Email Validation Help

    Let's start by saying that I stink at javascript. I've taken multiple different tutorials, some more than once, and I still can't get it. So, when I need a code, I search for it, and try different ones until I find something that works.

    I've been looking for a code for email validation for quite a long time, and I still can't find something that works with my code for validating the other parts of my contact form. I can get it to check for the presence of text, but not for the presence of an @ sign and period. I would like for someone to please help me figure this out.

    Right now I have the following code:

    Javascript

    Code:
    <!--
    
    function validate_form ( )
    {
        valid = true;
    
        if ( document.contact.firstnamecontact.value == "" )
        {
            alert ( "Please fill in the 'First Name' box." );
            valid = false;
        }
    
        if ( document.contact.lastnamecontact.value == "" )
        {
            alert ( "Please fill in the 'Last Name' box." );
            valid = false;
        }
    
        if ( document.contact.emailcontact.value == "" )
        {
            alert ( "Please fill in a valid email address." );
            valid = false;
        }
    
        if ( document.contact.subjectcontact.value == "" )
        {
    	alert ("Please fill in the 'Subject' box." );
    	valid = false;
        }
    
        if ( ( document.contact.topic[0].checked == false )
        && ( document.contact.topic[1].checked == false )
        && ( document.contact.topic[2].checked == false ) )
        {
            alert ( "Please choose your Topic of Inquiry" );
            valid = false;
        }
    
        if ( document.contact.messagecontact.value == "" )
        {
            alert ( "Please specify a question/comment." );
            valid = false;
        }
    
        return valid;
    }
    
    //-->
    HTML

    Code:
    <form name="contact" method="post" action="contactcompletion.php3" onsubmit="return validate_form ( );">
    
    <table>
    
    <tr>
    <td><b>First Name:&nbsp;</td><td><input type="text" name="firstnamecontact"/></td>
    </tr>
    
    <tr>
    <td><b>Last Name:&nbsp;</b></td><td><input type="text" name="lastnamecontact"/></td>
    </tr>
    
    <tr>
    <td><b>Email:&nbsp;</b></td><td><input type="text" name="emailcontact"/></td>
    </tr>
    
    <tr>
    <td><b>Subject:&nbsp;</b></td><td><input type="text" name="subjectcontact"/></td>
    </tr>
    
    </table>
    
    <br />
    <br />
    
    
    <h4>Topic of Inquiry (Choose One):</h4>
    
    <table>
    
    
    <tr>
    <td><input type="radio" name="topic" value="questionscontact" /><b>&nbsp;&nbsp;Questions</b></td>
    
    <td><input type="radio" name="topic" value="commentscontact" /><b>&nbsp;&nbsp;Comments</b></td>
    
    <td><input type="radio" name="topic" value="othercontact" /><b>&nbsp;&nbsp;Other</b></td>
    </tr>
    
    </table>
    
    <br />
    <br />
    
    <h4>Enter Your Questions or Comments:</h4>
    
    <br />
    
    <textarea name="messagecontact" rows="10" cols="70"></textarea>
    
    <br />
    <br />
    
    <p><input type="submit" name="send" value="Send" /></p>

    Thanks a bunch in advance for helping me!!
    Last edited by RomeosKlassicDJ; 07-17-2011 at 02:41 AM.

  • #2
    Regular Coder
    Join Date
    Nov 2009
    Posts
    247
    Thanks
    4
    Thanked 22 Times in 22 Posts
    RomeosKlassicDJ:

    How about scrapping that and using something that already works?

    http://www.freewarefiles.com/Plug-n-...ram_53638.html

  • #3
    New Coder
    Join Date
    Mar 2010
    Posts
    42
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Well, that's cool, but I'd kind of like to have it styled to my webpage--unless I can do this with it (if it generates an easy-to-adjust code).

  • #4
    Regular Coder
    Join Date
    Jul 2003
    Posts
    117
    Thanks
    0
    Thanked 17 Times in 17 Posts
    Is this the check you want?
    Code:
    if(document.contact.emailcontact.value.indexOf('@')< 0 || 
    document.contact.emailcontact.value.indexOf('.') < 0)
    {
    //error
    }
    See indexOf reference

    You can also use a ready-made Javascript Form Validation Script

  • Users who have thanked prasanthmj for this post:

    RomeosKlassicDJ (07-16-2011)

  • #5
    New Coder
    Join Date
    Mar 2010
    Posts
    42
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thank you SO much!!! I had to make a couple of modifications (I'll include the code so anyone else viewing this post in the future can use it) but it worked!!!!

    Javascript Modification:

    Code:
        if ( document.contact.emailcontact.value.indexOf('@')< 0 || 
        document.contact.emailcontact.value.indexOf('.') < 0)
        {
    	alert ( "Please fill in a valid email address." );
    	valid = false;
        }


  •  

    Posting Permissions

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