jquery EMAIL Validator - > just Email
i am looking for a jquery email validator -> simple and complete without using regex!
i found a powerful library like below : bassistance jquery-plugin-validatio but this library has many unnecessary features for me! besides i just need a function to give it my txtemail and check whether it is true or false. how can i do that with this plugin? my aspx codes are like below :<head runat="server">
<title></title>
<script src="Scripts/jquery-1.6.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script type="text/javascript">
$().ready(function () {
开发者_如何学JAVA $("#commentForm").validate();
});
</script>
<style type="text/css">
#commentForm
{
width: 500px;
}
</style>
</head>
<body>
<form runat="server" id="commentForm">
<fieldset>
<legend>Please provide your email address</legend>
<p>
<label for="cemail">
E-Mail (required)</label>
<asp:TextBox ID="TextBox1" runat="server" CssClass="required email"></asp:TextBox>
</p>
</fieldset>
</form>
</body>
</html>
or
i would be appreciate to introduce me a function in jquery that accept email string and return true or false!
i know there are some duplicate threads in stackoverflow about this issue , but one of them is about this plugin and the other one is about using RegEx(is RegEx Trustable and cross browser compatibility ?)
thanks in advance
You could use an established regular expression for validating email addresses. If wanting to do simple validation without the jQuery validate plugin then you might put the validation in a form submit
event handler
$(function () {
// regex from http://fightingforalostcause.net/misc/2006/compare-email-regex.php
var emailRegex = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
// capture the field to test
var textBoxToTest = $('input.required.email');
// set up a submit event handler
$('#commentForm').submit(function () {
return emailRegex.test(textBoxToTest.val());
});
});
If the input field value does not match against the email regex pattern, the .test()
call will return false, which will cancel the default action of the <form>
(i.e. prevent it from submitting) and also prevent bubbling the event any further up the DOM tree.
Now you could also dot his with the validate plugin by creating a validator using the regular expression and setting it up against the field then letting the validate plugin take care of preventing the form from submitting when the field is not valid. However, I see that you are using ASP.NET webforms so I would suggest using a RegularExpressionValidator
Control. Something like
<head runat="server">
<title></title>
<style type="text/css">
#commentForm
{
width: 500px;
}
</style>
</head>
<body>
<form runat="server" id="commentForm">
<fieldset>
<legend>Please provide your email address</legend>
<p>
<label for="cemail">
E-Mail (required)</label>
<asp:TextBox ID="TextBox1" runat="server" CssClass="required email"></asp:TextBox>
<asp:RegularExpressionValidator
ID="regexpName" runat="server"
ErrorMessage="Please enter a valid email address"
ControlToValidate="TextBox1"
ValidationExpression="/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i"
EnableClientScript="true" />
</p>
</fieldset>
</form>
</body>
</html>
The control will take care of both the client side and server side validation (you always always want to validate server side too), so it may be the easiest option for you, or you could use the validate plugin for the client side in conjunction with a CustomValidator
control to implement the server side validation.
精彩评论