-
-
Save fernandezpablo85/3105327 to your computer and use it in GitHub Desktop.
Carnaza
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#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