Sunday, January 27, 2013

Common Regular Expressions

.NET Regular Expression for Email

<asp:RegularExpressionValidator ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ID="regexEmail" runat="server" ControlToValidate="txtEmail" ValidationGroup="validateFields" ErrorMessage="*"></asp:RegularExpressionValidator>



JavaScript Regular Expression for Email
var astemail = document.getElementById("astemail");

//valida email
var objemail = document.getElementById("email");
var email = objemail.value;

//var regexpemail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
var regexpemail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!regexpemail.test(email))
{
astemail.style.color = "red";
astemail.style.visibility = "visible";
//return false;
}
else {
astemail.style.color = "white";
astemail.style.visibility = "hidden";
}

JQuery Regular Expression for Email

var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(!emailReg.test(jQuery.trim($(this).val())))
{
//todo
}



Regular expressions with PowerShell
Regular Expressions with Windows PowerShell

http://powershell.com/cs/blogs/ebook/archive/2009/03/30/chapter-13-text-and-regular-expressions.aspx
(requires registering, but its worth it!)

Testers
http://www.regular-expressions.info/javascriptexample.html

http://regexpal.com/

No comments:

Post a Comment