views:

96

answers:

3

I wrote the following contact form/validation script. How can I integrate email validation in it?:

<style type="text/css">
    div.contactForm { width:370px; margin:0 auto; }
    form.contactUs label { display:block; }
    form.contactUs input { margin-bottom:10px; }
    input.submit { margin-top:10px; }
    input.error { background:#FF9B9B; border:1px solid red; }
    div.errorMessage { color:#f00; padding:0 0 20px; }
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;

<script type="text/javascript">
    $(document).ready(function() {
        $('.errorMessage').hide();
        $('.submit').click(function() {

            var name = $('input.name').val();
            var email = $('input.email').val();
            var phone = $('input.phone').val();

            if (name == '') {
                $('input.name').addClass('error');
                $('input.name').keypress(function(){
                    $('input.name').removeClass('error');
                });
            }
            if (email == '') {
                $('input.email').addClass('error');
                $('input.email').keypress(function(){
                    $('input.email').removeClass('error');
                });
            }
            if (phone == '') {
                $('input.phone').addClass('error');
                $('input.phone').keypress(function(){
                    $('input.phone').removeClass('error');
                });
            }
            if (name == '' || email == '' || phone == '') {
                $('.errorMessage').fadeIn('medium');
                return false;
            }

        });
    });
</script>

<div class="contactForm">

<h2>Contact Us</h2>

<div class="errorMessage">Please enter all required fields.</div>

<form action="http://google.com" method="post" class="contactUs">

    <label>Name <em>(required)</em></label>
    <input type="text" name="name" class="name" size="30" />

    <label>Email <em>(valid email required)</em></label>
    <input type="text" name="email" class="email" size="30" />

    <label>Phone <em>(required)</em></label>
    <input type="text" name="phone" class="phone" size="30" />

    <label>Message</label>
    <textarea name="message" cols="40" rows="10"></textarea>

    <br />
    <input type="submit" name="submit" value="Submit" class="submit" />

</form>

</div><!--contactForm-->

EDIT

By validation I mean the entered value must contain a " @ " and a " . " somewhere in it.

Also I don't want to use any additional plugins.

A: 

I'm currently using ketchup for form validation. There's also bassassist's plugin. Check a plugin, it will save you hours of regex-writing code.

omgzor
A: 

You can use RegEx if you don't want to use any plugins.

var testEmail = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
if (testEmail.test(valueToTest))
    // Do whatever if it passes.
else
    // Do whatever if it fails.

This will get you most emails. An interesting read about validating emails with RegEx.

You can test the script here: http://jsfiddle.net/EFfCa/

Robert
Thank you, works great.
Sammy
No it doesn't. It invalidates perfectly valid, working emails. Please don't use this.
Randal Schwartz
See the link Randal, you have to decide at what point you call valid because you'd end up with a huge RegEx statement otherwise. As the poster said, he doesn't want to use plugins.
Robert
A: 

Change your input.email block as this:

            if (email == '') {
            $('input.email').addClass('error');
            $('input.email').keypress(function(){
                $('input.email').removeClass('error');
            });
            $('input.email').focusout(function(){
                $('input.email').filter(function(){
                    return this.value.match(/your email regex/);
                }).addClass('error');
            });
        }

Explaination: Add focusout action to input.email form element, validates the email string using RFC822 standard. If it can't pass then add the 'error' class.

I have not tested this on my browser but it should work for jQuery > 1.4.

EDIT: I've removed the regex, place your favorite one there.

marcofang
Please don't use this regex. It invalidates perfect valid, in-use emails.
Randal Schwartz
Thanks Randal, I've learned a lot from your suggestion.
marcofang