/    Sign up×
Community /Pin to ProfileBookmark

Validate Form to prevent gmail, yahoo and hotmail addresses

Hi, would like to have a form like this one where a user cannot enter gmail, yahoo or hotmail addresses in the email filed.

[url]http://pages.emptoris.com/GatedForm.htm[/url] … 20_2009_Q2

Have managed to make it work using this code:

<script type=”text/javascript”>
$(document).ready(function() {

$(‘#btn-submit’).click(function() {

$(“.error”).remove();
var hasError = false;
var emailReg = /^([w-.]+@([w-]+.)+[w-]{2,4})?$/;
var emailblockReg =
/^([w-.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)([w-]+.)+[w-]{2,4})?$/;

var emailaddressVal = $(“#UserEmail”).val();
if(emailaddressVal == ”) {
$(“#UserEmail”).after(‘<span class=”error”>Please enter your email address.</span>’);
hasError = true;
}

else if(!emailReg.test(emailaddressVal)) {
$(“#UserEmail”).after(‘<span class=”error”>Enter a valid email address.</span>’);
hasError = true;
}

else if(!emailblockReg.test(emailaddressVal)) {
$(“#UserEmail”).after(‘<span class=”error”>No yahoo, gmail or hotmail emails.</span>’);
hasError = true
}

if(hasError == true) { return false; }

});

});
</script>

My main problem is that i need to integrate it with this other validator but I dont know how, so that it works with the form i have already.

if ($id==3) return “var RegExp=/^((([a-z]|[0-9]|!|#|$|%|&|’|*|+||/|=|?|^|_|`|{|||}|~)+(.([a-z]|[0-9]|!|#|$|%|&|’|*|+|-|/|=|?|^|_|`|{|||}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|){0,61}([a-z]|[0-9]).))*([a-z]|[0-9])([a-z]|[0-9]|){0,61}([a-z]|[0-9]).)[w]{2,4}|(((([0-9]){1,3}.){3}([0-9]){1,3}))|([((([0-9]){1,3}.){3}([0-9]){1,3})])))$/; if (this.value!=” && !RegExp.test(this.value.toLowerCase())) show_message$uid(this,’$msg’,alert_mode);”; //e-mail

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@acestuffJun 18.2011 — No-one is ever going to look through all that regex - regex is a pain to follow at the best of times but that regex returned if id equals 3 takes it to the next level! Why it's returning js code as a string I have no idea!

I'd personally go for a simple solution:
[CODE]function isBlockedEmail(str) {
var blocked = ["gmail.com", "hotmail.com", "yahoo.com"];
for(var i = 0; i< blocked.length; i++) {
if(str.indexOf(blocked[i]) != -1) {
return true;
}
}
return false;
}[/CODE]

Then shove the email through that regex filter to validate it.

To be perfectly honest, I'd question what you're doing trying to prevent gmail, yahoo and hotmail accounts. From the start, you've stopped probably 70&#37; of people from entering their email address. And for what reason - are people with these accounts spammers? If so I'm a 3x spammer! I'd be more concerned with temporary email accounts...


PS: You're going to have to do a server-side filter as well. Never trust that your client-side filter will work. If a website was stopping me enter a gmail/yahoo/hotmail account and it's only defence was a client-side filter, it wouldn't stop me!
×

Success!

Help @mkagwe spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...