Skip to content

Instantly share code, notes, and snippets.

@fernandezpablo85
Forked from anonymous/CarnazaModeOn
Created July 13, 2012 14:55
Show Gist options
  • Save fernandezpablo85/3105327 to your computer and use it in GitHub Desktop.
Save fernandezpablo85/3105327 to your computer and use it in GitHub Desktop.
Carnaza
$('#enviarEmail').click(function () {
$.ajax({
url: '@Url.Action("SetEmail","Email")',
type: 'POST',
dataType: "json",
data: { text: $('#email').val() },
success: function (data) {
alert('Gracias por suscribirse!');
$('#email').val('');
},
error: function () {
alert('Formato incorrecto');
}
});
[HttpPost]
public ActionResult SetEmail(string text)
{
if (validarEmail(text))
{
var email = new EmailAddress();
email.Address = text;
repository.Insert(email);
repository.SaveContextChanges();
return Json(new { Data = "data" });
}
throw new Exception();
}
private static bool validarEmail(string email)
{
string expresion = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
if (Regex.IsMatch(email, expresion))
{
if (Regex.Replace(email, expresion, String.Empty).Length == 0)
{ return true; }
else
{ return false; }
}
else
{ return false; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment