Reputation: 137
How do you validate a Textbox field for valid email address using Jquery?
Please reply soon.
Many Thanks.
Upvotes: 1
Views: 5449
Reputation: 1968
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var emailaddressVal = $("#txtEmail").val();
if (emailaddressVal == '') {
$("#error").html("Please enter your email address.").css({ 'color': 'red' });
return;
}
else if (!emailReg.test(emailaddressVal)) {
$("#error").html("Enter a valid email address.").css({ 'color': 'red' });
return;
}
Upvotes: 1
Reputation: 3512
use a plugin like this one:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
then you can do somethign like this:
$("#myform").validate({
rules: {
field: {
required: true,
email: true
}
}
});
or this
$("#<%=txtEmail.ClientID %>").rules("add", {
required: true,
email: true,
messages: {
required: "Email is a required field.",
email: "Email - Invalid email address"
}
});
Upvotes: 1
Reputation: 331
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
Check this plug-in out.
Not really worth it if you only want one validation though, in my opinion. So you might be better off with some REGEX validation for it.
Upvotes: 0