John Detlefs
John Detlefs

Reputation: 3

Email validation not working

I'm using this script on http://info.cfgt.com.au/diploma-of-management/sq1/

<script type="text/javascript">

jQuery(document).ready(function() {
function validateEmail(sEmail) {
var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (filter.test(sEmail)) {
    return true;
}
else {
    return false;
}
}
});

jQuery(document).ready(function() {
jQuery('form').submit(function() {
    var sEmail = jQuery('#inf_field_Email').val();

    if (validateEmail(sEmail)) {
        alert('Email is valid');
        e.preventDefault();
    }
    else {
        alert('Invalid Email Address');
        e.preventDefault();
    }
});
});
</script>

No matter what I do, the form submits, and redirects to the default infusionsoft error page.

I can't for the life of me figure out what i've missed, and i'm at my 7th hour.

Any ideas?

Cheers,

John Detlefs

EDIT:

I'm now using the below, which kind of works:

<script>
jQuery(document).ready(function() {
jQuery('form').submit(function() {
email_address = jQuery('#inf_field_Email');           
email_regex = /^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.[a-z]{2,6}$/i;
if(!email_regex.test(email_address.val())){
alert('Please enter a valid email');
return false;
}else{
alert('All Good!');
return true;
}
});
});
</script>

The only issue that I have now is that it will accept anything@anything and doesn't seem to need the ".domain" at all.

Upvotes: 0

Views: 222

Answers (2)

Barmar
Barmar

Reputation: 780889

When you have multiple jQuery(document).ready(function() blocks, functions defined in one cannot be called from another. See Can you have multiple $(document).ready(function(){ ... }); sections?

So try combining them into one big function and see if that solves it.

Also, have you considered using the jQuery Validate plugin?

Upvotes: 2

Ashirvad
Ashirvad

Reputation: 2377

I checked your page . There are few errors in your page..

First .Your jQuery is not found.

http://info.cfgt.com.au/wp-content/plugins/jquery.validity.1.2.0/jquery.js"

Because of that it is not recognising $ as a function . So your $(function()) giving error.

Regarding email part

change the regex to

var filter = /^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.[a-z]{2,6}$/i;

Upvotes: 0

Related Questions