Validating e-mail from specific domains with RSV jquery plug-in
I'm trying to use Ben Keen's RSV Jquery plug-in to validate an e-mail address submitted through a registration form: users are only allowed to submit an e-mail address that belongs to a specific domain. I thought that using the plugin's reg_exp rule could be a solution, but I can't get it to work. I'm a jquery newbie, so I'm probably making some very silly mistake: can someone point me to the right direction?
Here's the code I'm trying to use, adapted from one of Keen's demo:
<script type="text/javascript">
// a custom onComplete handler to prevent form submits for the demo
function myOnComplete()
{
alert("The form validates! (normally, it would submit the form here).");
return false;
}
var rules = [];
// standard form fields
rules.push("reg_exp,reg_exp_field1,/@mydomain\.tld$/gi,Please enter your valid e-mail (e.g. \"@mydo开发者_如何学Cmain\.tld\")");
$(document).ready(function() {
$("#demo_form3").RSV({
onCompleteHandler: myOnComplete,
rules: rules
});
});
</script>
Using the code above, when I submit a valid e-mail address, that is an address ending with @mydomain.tld I always get an alert. What am I doing wrong?
I have written RSV for many lines of code. It's very powerful. However, you used it incorrectly.
- you should define a customized function instead of its default email-format validation method.
- you should put that "customized function" mentioned above into the validation array, but not put it in "myOnComplete" method, this method only called when all the validations are passed.
- in RSV, a customized function only should return "true"(when passed) or "an Array"(when failed). but in the example above, you implemented as "return false".
Whatever, you have solved your problem in another way. :-) that's good.
Never mind, after further researche I found a simpler solution: following the advice in this forum thread I used jqueryValidate instead of rsv plug-in and added custom validate method. I'm posting here my solution, maybe it could be useful for some other jquery newbie like me!
$(document).ready(function(){
$.validator.addMethod("gmail", function(value, element) {
return this.optional(element) || /^[\w-\.]+@(gmail\.com|yahoo\.com)+$/i.test(value);
}, "Email address must contain gmail or yahoo addresses.");
$("#register_member_form").validate();
});
精彩评论