<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programador ASP &#187; expresiones regulares</title>
	<atom:link href="http://programadorasp.com/tag/expresiones-regulares/feed/" rel="self" type="application/rss+xml" />
	<link>http://programadorasp.com</link>
	<description>Programación en Internet</description>
	<lastBuildDate>Fri, 03 Feb 2012 20:09:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Reemplazar carácteres con expresiones regulares en ASP para generar URL&#8217;s amigables</title>
		<link>http://programadorasp.com/generar-urls-amigables-asp-expresiones-regulares/</link>
		<comments>http://programadorasp.com/generar-urls-amigables-asp-expresiones-regulares/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 19:10:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Buscadores]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Programación]]></category>
		<category><![CDATA[acentos]]></category>
		<category><![CDATA[ansi]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[cadena de texto]]></category>
		<category><![CDATA[carácteres extraños]]></category>
		<category><![CDATA[comillas]]></category>
		<category><![CDATA[expresiones regulares]]></category>
		<category><![CDATA[iso-8859-1]]></category>
		<category><![CDATA[patrón expresión regular]]></category>
		<category><![CDATA[Pattern]]></category>
		<category><![CDATA[permalinks]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[reemplazar carácteres]]></category>
		<category><![CDATA[RegExp]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tildes]]></category>
		<category><![CDATA[URL friendly]]></category>
		<category><![CDATA[URL's amigables]]></category>
		<category><![CDATA[UTF-8]]></category>

		<guid isPermaLink="false">http://programadorasp.com/?p=503</guid>
		<description><![CDATA[Las expresiones regulares en ASP son muy útiles para reemplazar carácteres extraños en cadenas de texto y quitar acentos para por ejemplo generar url's amigables
Related posts:<ol>
<li><a href='http://programadorasp.com/codificacion-utf-8-incorrecta-php-leyendo-base-datos-mysql/' rel='bookmark' title='Codificación UTF-8 incorrecta en PHP leyendo una base de datos MySQL'>Codificación UTF-8 incorrecta en PHP leyendo una base de datos MySQL</a></li>
<li><a href='http://programadorasp.com/la-world-wide-web-y-el-html-cumplen-20-anos/' rel='bookmark' title='La World Wide Web y el HTML cumplen 20 años'>La World Wide Web y el HTML cumplen 20 años</a></li>
<li><a href='http://programadorasp.com/convertir-archivos-web-estaticos-dinamicos-introducir-codigo/' rel='bookmark' title='Interpretar páginas web estáticas como php, asp o cualquier lenguaje'>Interpretar páginas web estáticas como php, asp o cualquier lenguaje</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>En ocasiones en <strong>ASP</strong> hemos querido <strong>reemplazar carácteres</strong> de un texto o una cadena para eliminar los acentos y los carácteres extraños como paréntesis, puntos, comas, corchetes, ñ, ç,&#8230; Por ejemplo, para generar <strong>URL&#8217;s amigables</strong> para los buscadores (URL friendly o permalinks), necesitamos convertir las cadenas a texto plano <strong>sin carácteres extraños</strong>. Podemos hacer funciones de reemplazo de carácteres intentando tener en cuenta todos los carácteres extraños que nos podamos encontrar en una cadena, pero las <strong>expresiones regulares</strong> y sus herramientas de reemplazo nos pueden ayudar mucho. Se trata de utilizar el operador <strong>.replace</strong> del objeto de expresión regular en ASP, jugando con los <strong>patrones</strong> para las condiciones.</p>
<p>En la funcion siguiente, a partir de una <strong>cadena de texto</strong> dada se convierte a una cadena amigable para los buscadores, eliminando los carácteres extraños. En primer lugar, se eliminan los espacios a ambos lados de la cadena y se reemplazan o eliminan los carácteres que pueden dar problemas en bases de datos como las <strong>comillas</strong> simples y dobles, saltos de línea y espacios. Después se usan las <strong>expresiones regulares</strong> para eliminar de las vocales las tildes abiertas y cerradas, diéresis,&#8230; y para sustituir la ñ y la ç por n y c respectivamente. Es decir, los carácteres que <strong>cumplan el patrón</strong> de la expresión regular serán sustituidos por el carácter dado. Por último, se eliminan los carácteres restantes que <strong>no cumplan el patrón</strong> de URL amigable (letras minúsculas, números y guión).</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">function</span> convertirAURLfriendly<span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span>
  <span style="color: #990099; font-weight: bold;">if</span> <span style="color: #990099; font-weight: bold;">not</span> isNull<span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #990099; font-weight: bold;">then</span>
    <span style="color: #008000;">' Eliminamos los espacios a ambos lados de la cadena</span>
    strCadena <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #330066;">Trim</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #330066;">lCase</span><span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #008000;">' Reemplazamos carácteres especiales</span>
    strCadena <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #330066;">replace</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #330066;">replace</span><span style="color: #006600; font-weight:bold;">&#40;</span>strCadena,<span style="color: #cc0000;">&quot;'&quot;</span>,<span style="color: #cc0000;">&quot;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>,<span style="color: #cc0000;">&quot;&quot;</span><span style="color: #cc0000;">&quot;&quot;</span>,<span style="color: #cc0000;">&quot;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    strCadena <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #330066;">replace</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #330066;">replace</span><span style="color: #006600; font-weight:bold;">&#40;</span>strCadena,<span style="color: #cc0000;">&quot;&amp;quot;&quot;</span>,<span style="color: #cc0000;">&quot;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>,vbcrlf,<span style="color: #cc0000;">&quot;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    strCadena <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #330066;">replace</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #330066;">replace</span><span style="color: #006600; font-weight:bold;">&#40;</span>strCadena,<span style="color: #cc0000;">&quot;&lt;br&gt;&quot;</span>,<span style="color: #cc0000;">&quot;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>,<span style="color: #cc0000;">&quot; &quot;</span>,<span style="color: #cc0000;">&quot;-&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">set</span> expReg <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">New</span> RegExp
    <span style="color: #008000;">' Todas las ocurrencias</span>
    expReg.<span style="color: #990099; font-weight: bold;">Global</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">True</span>
    expReg.<span style="color: #9900cc;">Pattern</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;[àáâãäå]&quot;</span>
    strCadena <span style="color: #006600; font-weight: bold;">=</span> expReg.<span style="color: #330066;">Replace</span><span style="color: #006600; font-weight:bold;">&#40;</span>strCadena, <span style="color: #cc0000;">&quot;a&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    expReg.<span style="color: #9900cc;">Pattern</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;[èéêë]&quot;</span>
    strCadena <span style="color: #006600; font-weight: bold;">=</span> expReg.<span style="color: #330066;">Replace</span><span style="color: #006600; font-weight:bold;">&#40;</span>strCadena, <span style="color: #cc0000;">&quot;e&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    expReg.<span style="color: #9900cc;">Pattern</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;[ìíîï]&quot;</span>
    strCadena <span style="color: #006600; font-weight: bold;">=</span> expReg.<span style="color: #330066;">Replace</span><span style="color: #006600; font-weight:bold;">&#40;</span>strCadena, <span style="color: #cc0000;">&quot;i&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    expReg.<span style="color: #9900cc;">Pattern</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;[òóôõö]&quot;</span>
    strCadena <span style="color: #006600; font-weight: bold;">=</span> expReg.<span style="color: #330066;">Replace</span><span style="color: #006600; font-weight:bold;">&#40;</span>strCadena, <span style="color: #cc0000;">&quot;o&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    expReg.<span style="color: #9900cc;">Pattern</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;[ùúûü]&quot;</span>
    strCadena <span style="color: #006600; font-weight: bold;">=</span> expReg.<span style="color: #330066;">Replace</span><span style="color: #006600; font-weight:bold;">&#40;</span>strCadena, <span style="color: #cc0000;">&quot;u&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    expReg.<span style="color: #9900cc;">Pattern</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;[ñ]&quot;</span>
    strCadena <span style="color: #006600; font-weight: bold;">=</span> expReg.<span style="color: #330066;">Replace</span><span style="color: #006600; font-weight:bold;">&#40;</span>strCadena, <span style="color: #cc0000;">&quot;n&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    expReg.<span style="color: #9900cc;">Pattern</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;[ç]&quot;</span>
    strCadena <span style="color: #006600; font-weight: bold;">=</span> expReg.<span style="color: #330066;">Replace</span><span style="color: #006600; font-weight:bold;">&#40;</span>strCadena, <span style="color: #cc0000;">&quot;c&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #008000;">' Todo lo que no cumpla este patron</span>
    expReg.<span style="color: #9900cc;">Pattern</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;[^a-z0-9-]&quot;</span>
    strCadena <span style="color: #006600; font-weight: bold;">=</span> expReg.<span style="color: #330066;">Replace</span><span style="color: #006600; font-weight:bold;">&#40;</span>strCadena, <span style="color: #cc0000;">&quot;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">set</span> expReg <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">nothing</span>
    convertirAURLfriendly <span style="color: #006600; font-weight: bold;">=</span> left<span style="color: #006600; font-weight:bold;">&#40;</span>strCadena,<span style="color: #800000;">256</span><span style="color: #006600; font-weight:bold;">&#41;</span>
  <span style="color: #990099; font-weight: bold;">else</span>
    convertirAURLfriendly <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;&quot;</span>
  <span style="color: #990099; font-weight: bold;">end</span> <span style="color: #990099; font-weight: bold;">if</span>
<span style="color: #990099; font-weight: bold;">end</span> <span style="color: #0000ff; font-weight: bold;">function</span></pre></div></div>

<p>Hacer la conversión de la función anterior a otros lenguajes como <strong>PHP</strong> o <strong>Javascript</strong> es relativamente sencillo, en otros artículos de este blog se pueden encontrar ejemplos de expresiones regulares en Javascript. De hecho, la función anterior an ASP para generar URL&#8217;s amigables me funcionaba en archivos ANSI con codificación iso-8859-1, pero da problemas con los acentos y carácteres extraños cuando el archivo está en <strong>UTF-8</strong>. Así que después de darle muchas vueltas, convertí la función de ASP a Javascript y la llamaba antes de enviar el formulario, es decir generaba la <strong>URL amigable</strong> desde Javascript y lo almacenaba en una variable hidden que también envío con el formulario. La función en <strong>Javascript</strong> que funciona para UTF-8:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> convertirAURLfriendly<span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  strCadena <span style="color: #339933;">=</span> str<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>strCadena<span style="color: #339933;">!=</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    strCadena <span style="color: #339933;">=</span> strCadena.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Elimina espacios al principio y al final</span>
    strCadena <span style="color: #339933;">=</span> strCadena.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\s*|\s*$/g</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    strCadena <span style="color: #339933;">=</span> strCadena.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&amp;quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    strCadena <span style="color: #339933;">=</span> strCadena.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;-&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;-&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    strExpReg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/[àáâãäå]/gi</span><span style="color: #339933;">;</span>
    strCadena <span style="color: #339933;">=</span> strCadena.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>strExpReg<span style="color: #339933;">,</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    strExpReg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/[èéêë]/gi</span><span style="color: #339933;">;</span>
    strCadena <span style="color: #339933;">=</span> strCadena.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>strExpReg<span style="color: #339933;">,</span><span style="color: #3366CC;">'e'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    strExpReg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/[ìíîï]/gi</span><span style="color: #339933;">;</span>
    strCadena <span style="color: #339933;">=</span> strCadena.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>strExpReg<span style="color: #339933;">,</span><span style="color: #3366CC;">'i'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    strExpReg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/[òóôõö]/gi</span><span style="color: #339933;">;</span>
    strCadena <span style="color: #339933;">=</span> strCadena.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>strExpReg<span style="color: #339933;">,</span><span style="color: #3366CC;">'o'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    strExpReg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/[ùúûü]/gi</span><span style="color: #339933;">;</span>
    strCadena <span style="color: #339933;">=</span> strCadena.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>strExpReg<span style="color: #339933;">,</span><span style="color: #3366CC;">'u'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    strExpReg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/[ñ]/gi</span><span style="color: #339933;">;</span>
    strCadena <span style="color: #339933;">=</span> strCadena.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>strExpReg<span style="color: #339933;">,</span><span style="color: #3366CC;">'n'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    strExpReg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/[ç]/gi</span><span style="color: #339933;">;</span>
    strCadena <span style="color: #339933;">=</span> strCadena.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>strExpReg<span style="color: #339933;">,</span><span style="color: #3366CC;">'c'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Todo lo que no cumpla este patron</span>
    strExpReg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/[^a-z0-9-]/gi</span><span style="color: #339933;">;</span>
    strCadena <span style="color: #339933;">=</span> strCadena.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>strExpReg<span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> strCadena.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Related posts:<ol>
<li><a href='http://programadorasp.com/codificacion-utf-8-incorrecta-php-leyendo-base-datos-mysql/' rel='bookmark' title='Codificación UTF-8 incorrecta en PHP leyendo una base de datos MySQL'>Codificación UTF-8 incorrecta en PHP leyendo una base de datos MySQL</a></li>
<li><a href='http://programadorasp.com/la-world-wide-web-y-el-html-cumplen-20-anos/' rel='bookmark' title='La World Wide Web y el HTML cumplen 20 años'>La World Wide Web y el HTML cumplen 20 años</a></li>
<li><a href='http://programadorasp.com/convertir-archivos-web-estaticos-dinamicos-introducir-codigo/' rel='bookmark' title='Interpretar páginas web estáticas como php, asp o cualquier lenguaje'>Interpretar páginas web estáticas como php, asp o cualquier lenguaje</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://programadorasp.com/generar-urls-amigables-asp-expresiones-regulares/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eliminar saltos de línea en un textarea con Javascript</title>
		<link>http://programadorasp.com/eliminar-saltos-de-linea-en-un-textarea-con-javascript/</link>
		<comments>http://programadorasp.com/eliminar-saltos-de-linea-en-un-textarea-con-javascript/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 14:11:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programación]]></category>
		<category><![CDATA[expresiones regulares]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[saltos línea]]></category>

		<guid isPermaLink="false">http://www.programadorasp.com/?p=168</guid>
		<description><![CDATA[Alguna vez he tenido la necesidad de eliminar los saltos de línea en un campo de formulario de tipo textarea. Más aún, he tenido la necesidad de eliminar los saltos de línea del texto seleccionado dentro de un textarea. Con la siguiente función Javascript conseguimos localizar la porción de texto seleccionado y eliminar los saltos [...]
Related posts:<ol>
<li><a href='http://programadorasp.com/generar-urls-amigables-asp-expresiones-regulares/' rel='bookmark' title='Reemplazar carácteres con expresiones regulares en ASP para generar URL&#8217;s amigables'>Reemplazar carácteres con expresiones regulares en ASP para generar URL&#8217;s amigables</a></li>
<li><a href='http://programadorasp.com/validar-campos-y-parametros-con-expresiones-regulares-en-asp/' rel='bookmark' title='Validar campos y parámetros con expresiones regulares en ASP'>Validar campos y parámetros con expresiones regulares en ASP</a></li>
<li><a href='http://programadorasp.com/validar-fechas-con-expresiones-regulares-en-asp-y-javascrip/' rel='bookmark' title='Validar fechas con expresiones regulares en ASP y Javascript'>Validar fechas con expresiones regulares en ASP y Javascript</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>    Alguna vez he tenido la necesidad de eliminar los saltos de línea en un campo de formulario de tipo textarea. Más aún, he tenido la necesidad de eliminar los saltos de línea del texto seleccionado dentro de un textarea. Con la siguiente función Javascript conseguimos localizar la porción de texto seleccionado y eliminar los saltos de línea o line breaks (&#8220;\n&#8221;) y los retornos de carro o carrier return (&#8220;\r&#8221;). Para ello utilizamos funciones propias del lenguaje Javascript (createTextRange, duplicate, replace) y expresiones regulares.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> suprSaltos<span style="color: #009900;">&#40;</span>campo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>campo.<span style="color: #660066;">createTextRange</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  campo.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span>campo.<span style="color: #660066;">caretPos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  campo.<span style="color: #660066;">caretPos</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">selection</span>.<span style="color: #660066;">createRange</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">duplicate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>campo.<span style="color: #660066;">caretPos</span>.<span style="color: #660066;">text</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> sel <span style="color: #339933;">=</span> campo.<span style="color: #660066;">caretPos</span>.<span style="color: #660066;">text</span><span style="color: #339933;">;</span>
   <span style="color: #003366; font-weight: bold;">var</span> fin <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
   <span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>sel.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>sel.<span style="color: #660066;">length</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> sel.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    sel <span style="color: #339933;">=</span> sel.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> sel.<span style="color: #660066;">length</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    fin <span style="color: #339933;">+=</span> <span style="color: #3366CC;">' '</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   campo.<span style="color: #660066;">caretPos</span>.<span style="color: #660066;">text</span> <span style="color: #339933;">=</span> sel.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\\</span>n'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'g'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>. _
                          replace<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\\</span>r'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'g'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> fin<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> campo.<span style="color: #660066;">caretPos</span>.<span style="color: #660066;">text</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000066; font-weight: bold;">else</span> campo.<span style="color: #660066;">value</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Como en otras funciones, en esta función es necesario juntar la expresión en una única línea quitando los _ de final de línea porque de lo contrario no funcionará.</p>
<p><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fprogramadorasp.com%2Feliminar-saltos-de-linea-en-un-textarea-con-javascript%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fprogramadorasp.com%2Feliminar-saltos-de-linea-en-un-textarea-con-javascript%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><!--<![endif]--><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fprogramadorasp.com%2Feliminar-saltos-de-linea-en-un-textarea-con-javascript%2F&amp;counturl=http%3A%2F%2Fprogramadorasp.com%2Feliminar-saltos-de-linea-en-un-textarea-con-javascript%2F&amp;count=horizontal&amp;text=Eliminar%20saltos%20de%20l%C3%ADnea%20en%20un%20textarea%20con%20Javascript" scrolling="no" style="border:none;overflow:hidden;width:130px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fprogramadorasp.com%2Feliminar-saltos-de-linea-en-un-textarea-con-javascript%2F&amp;counturl=http%3A%2F%2Fprogramadorasp.com%2Feliminar-saltos-de-linea-en-un-textarea-con-javascript%2F&amp;count=horizontal&amp;text=Eliminar%20saltos%20de%20l%C3%ADnea%20en%20un%20textarea%20con%20Javascript" scrolling="no" style="border:none;overflow:hidden;width:130px;height:20px"></iframe><!--<![endif]--><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fprogramadorasp.com%2Feliminar-saltos-de-linea-en-un-textarea-con-javascript%2F&amp;size=medium&amp;count=true" scrolling="no" style="border:none;overflow:hidden;width:90px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fprogramadorasp.com%2Feliminar-saltos-de-linea-en-un-textarea-con-javascript%2F&amp;size=medium&amp;count=true" scrolling="no" style="border:none;overflow:hidden;width:90px;height:20px"></iframe><!--<![endif]--><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fprogramadorasp.com%2Feliminar-saltos-de-linea-en-un-textarea-con-javascript%2F&amp;title=Eliminar%20saltos%20de%20l%C3%ADnea%20en%20un%20textarea%20con%20Javascript" id="wpa2a_2">Compartir / Guardar</a></p><p>Related posts:<ol>
<li><a href='http://programadorasp.com/generar-urls-amigables-asp-expresiones-regulares/' rel='bookmark' title='Reemplazar carácteres con expresiones regulares en ASP para generar URL&#8217;s amigables'>Reemplazar carácteres con expresiones regulares en ASP para generar URL&#8217;s amigables</a></li>
<li><a href='http://programadorasp.com/validar-campos-y-parametros-con-expresiones-regulares-en-asp/' rel='bookmark' title='Validar campos y parámetros con expresiones regulares en ASP'>Validar campos y parámetros con expresiones regulares en ASP</a></li>
<li><a href='http://programadorasp.com/validar-fechas-con-expresiones-regulares-en-asp-y-javascrip/' rel='bookmark' title='Validar fechas con expresiones regulares en ASP y Javascript'>Validar fechas con expresiones regulares en ASP y Javascript</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://programadorasp.com/eliminar-saltos-de-linea-en-un-textarea-con-javascript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript</title>
		<link>http://programadorasp.com/validar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript/</link>
		<comments>http://programadorasp.com/validar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 14:15:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programación]]></category>
		<category><![CDATA[expresiones regulares]]></category>
		<category><![CDATA[validar campos]]></category>
		<category><![CDATA[validar URL]]></category>

		<guid isPermaLink="false">http://www.programadorasp.com/?p=125</guid>
		<description><![CDATA[Para validar direcciones de Internet simples (URL&#8217;s) de usuarios que introducen su página web en un campo de formulario podemos utilizar la siguiente función en Javascript: function ValidarUrl&#40;txtCampo&#41; strExpReg = /^http:\/\/[a-zA-Z0-9-_:/.?&#38;amp;=&#93;+$/; if &#40;!strExpReg.test&#40;txtCampoActual.value&#41;&#41;&#123; strMensaje = 'La página web no es válida,\ncontiene carácteres '; strMensaje += 'no válidos o no empieza por &#34;http://&#34;'; alert&#40;strMensaje&#41;; txtCampoActual.focus&#40;&#41;; return [...]
Related posts:<ol>
<li><a href='http://programadorasp.com/validar-campos-y-parametros-con-expresiones-regulares-en-asp/' rel='bookmark' title='Validar campos y parámetros con expresiones regulares en ASP'>Validar campos y parámetros con expresiones regulares en ASP</a></li>
<li><a href='http://programadorasp.com/validar-fechas-con-expresiones-regulares-en-asp-y-javascrip/' rel='bookmark' title='Validar fechas con expresiones regulares en ASP y Javascript'>Validar fechas con expresiones regulares en ASP y Javascript</a></li>
<li><a href='http://programadorasp.com/validar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript/' rel='bookmark' title='Validar direcciones de e-mail con expresiones regulares en ASP y Javascript'>Validar direcciones de e-mail con expresiones regulares en ASP y Javascript</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Para validar direcciones de Internet simples (URL&#8217;s) de usuarios que introducen su página web en un campo de formulario podemos utilizar la siguiente función en Javascript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> ValidarUrl<span style="color: #009900;">&#40;</span>txtCampo<span style="color: #009900;">&#41;</span>
  strExpReg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^http:\/\/[a-zA-Z0-9-_:/</span>.<span style="color: #339933;">?&amp;</span>amp<span style="color: #339933;">;=</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>strExpReg.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>txtCampoActual.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    strMensaje <span style="color: #339933;">=</span> <span style="color: #3366CC;">'La página web no es válida,<span style="color: #000099; font-weight: bold;">\n</span>contiene carácteres '</span><span style="color: #339933;">;</span>
    strMensaje <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'no válidos o no empieza por &quot;http://&quot;'</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>strMensaje<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    txtCampoActual.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
end <span style="color: #003366; font-weight: bold;">function</span></pre></div></div>

<p>En este caso, la validación se realiza del lado del cliente (al ser Javascript) y la función recibe como parámetro el campo del formulario donde irá la URL, mostrando un mensaje de error si la URL no es válida y devolviendo el foco a ese campo.</p>
<p>Obtener su versión en ASP y otros lenguajes es fácil adaptando la expresión regular a la sintaxis de cada lenguaje</p>
<p><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><!--<![endif]--><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript%2F&amp;counturl=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript%2F&amp;count=horizontal&amp;text=Validar%20direcciones%20de%20Internet%20o%20URL%26%238217%3Bs%20con%20expresiones%20regulares%20en%20Javascript" scrolling="no" style="border:none;overflow:hidden;width:130px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript%2F&amp;counturl=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript%2F&amp;count=horizontal&amp;text=Validar%20direcciones%20de%20Internet%20o%20URL%26%238217%3Bs%20con%20expresiones%20regulares%20en%20Javascript" scrolling="no" style="border:none;overflow:hidden;width:130px;height:20px"></iframe><!--<![endif]--><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript%2F&amp;size=medium&amp;count=true" scrolling="no" style="border:none;overflow:hidden;width:90px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript%2F&amp;size=medium&amp;count=true" scrolling="no" style="border:none;overflow:hidden;width:90px;height:20px"></iframe><!--<![endif]--><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript%2F&amp;title=Validar%20direcciones%20de%20Internet%20o%20URL%26%238217%3Bs%20con%20expresiones%20regulares%20en%20Javascript" id="wpa2a_4">Compartir / Guardar</a></p><p>Related posts:<ol>
<li><a href='http://programadorasp.com/validar-campos-y-parametros-con-expresiones-regulares-en-asp/' rel='bookmark' title='Validar campos y parámetros con expresiones regulares en ASP'>Validar campos y parámetros con expresiones regulares en ASP</a></li>
<li><a href='http://programadorasp.com/validar-fechas-con-expresiones-regulares-en-asp-y-javascrip/' rel='bookmark' title='Validar fechas con expresiones regulares en ASP y Javascript'>Validar fechas con expresiones regulares en ASP y Javascript</a></li>
<li><a href='http://programadorasp.com/validar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript/' rel='bookmark' title='Validar direcciones de e-mail con expresiones regulares en ASP y Javascript'>Validar direcciones de e-mail con expresiones regulares en ASP y Javascript</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://programadorasp.com/validar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Validar direcciones de e-mail con expresiones regulares en ASP y Javascript</title>
		<link>http://programadorasp.com/validar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript/</link>
		<comments>http://programadorasp.com/validar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 14:30:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programación]]></category>
		<category><![CDATA[expresiones regulares]]></category>
		<category><![CDATA[validar campos]]></category>
		<category><![CDATA[validar e-mail]]></category>

		<guid isPermaLink="false">http://www.programadorasp.com/?p=118</guid>
		<description><![CDATA[Para validar direcciones de e-mail podemos utilizar la siguiente función en ASP: function esValidoEmail&#40;cadena&#41; set expReg = New RegExp expReg.Pattern = &#34;^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@_ [_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*$&#34; esValidoEmail = expReg.Test&#40;cadena&#41; and len&#40;cadena&#41; &#38;lt; 256 set expReg = nothing end function Su versión en Javascript: function esValidoEmail&#40;cadena&#41; &#123; strExpReg = /^&#91;_a-zA-Z0-9-&#93;+&#40;\.&#91;_a-zA-Z0-9-&#93;+&#41;*@_ &#91;_a-zA-Z0-9-&#93;+&#40;\.&#91;a-zA-Z0-9-&#93;+&#41;*$/; return strExpReg.test&#40;cadena&#41;; &#125; Como en las otras funciones, [...]
Related posts:<ol>
<li><a href='http://programadorasp.com/validar-campos-y-parametros-con-expresiones-regulares-en-asp/' rel='bookmark' title='Validar campos y parámetros con expresiones regulares en ASP'>Validar campos y parámetros con expresiones regulares en ASP</a></li>
<li><a href='http://programadorasp.com/validar-fechas-con-expresiones-regulares-en-asp-y-javascrip/' rel='bookmark' title='Validar fechas con expresiones regulares en ASP y Javascript'>Validar fechas con expresiones regulares en ASP y Javascript</a></li>
<li><a href='http://programadorasp.com/validar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript/' rel='bookmark' title='Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript'>Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Para validar direcciones de e-mail podemos utilizar la siguiente función en ASP:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">function</span> esValidoEmail<span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span>
  <span style="color: #990099; font-weight: bold;">set</span> expReg <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">New</span> RegExp
  expReg.<span style="color: #9900cc;">Pattern</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@_
                     [_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*$&quot;</span>
  esValidoEmail <span style="color: #006600; font-weight: bold;">=</span> expReg.<span style="color: #9900cc;">Test</span><span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">and</span> <span style="color: #330066;">len</span><span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #006600; font-weight: bold;">&amp;</span>lt<span style="color: #006600; font-weight: bold;">;</span> <span style="color: #800000;">256</span>
  <span style="color: #990099; font-weight: bold;">set</span> expReg <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">nothing</span>
<span style="color: #990099; font-weight: bold;">end</span> <span style="color: #0000ff; font-weight: bold;">function</span></pre></div></div>

<p>Su versión en Javascript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> esValidoEmail<span style="color: #009900;">&#40;</span>cadena<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  strExpReg <span style="color: #339933;">=</span> <span style="color: #339933;">/^</span><span style="color: #009900;">&#91;</span>_a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>\.<span style="color: #009900;">&#91;</span>_a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*@</span>_
                <span style="color: #009900;">&#91;</span>_a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>\.<span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>$<span style="color: #339933;">/;</span>
  <span style="color: #000066; font-weight: bold;">return</span> strExpReg.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>cadena<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Como en las otras funciones, en esta función en Javascript sí que es necesario juntar la expresión en una única línea quitando los _ de final de línea porque de lo contrario no funcionará.</p>
<p><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><!--<![endif]--><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript%2F&amp;counturl=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript%2F&amp;count=horizontal&amp;text=Validar%20direcciones%20de%20e-mail%20con%20expresiones%20regulares%20en%20ASP%20y%20Javascript" scrolling="no" style="border:none;overflow:hidden;width:130px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript%2F&amp;counturl=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript%2F&amp;count=horizontal&amp;text=Validar%20direcciones%20de%20e-mail%20con%20expresiones%20regulares%20en%20ASP%20y%20Javascript" scrolling="no" style="border:none;overflow:hidden;width:130px;height:20px"></iframe><!--<![endif]--><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript%2F&amp;size=medium&amp;count=true" scrolling="no" style="border:none;overflow:hidden;width:90px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript%2F&amp;size=medium&amp;count=true" scrolling="no" style="border:none;overflow:hidden;width:90px;height:20px"></iframe><!--<![endif]--><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript%2F&amp;title=Validar%20direcciones%20de%20e-mail%20con%20expresiones%20regulares%20en%20ASP%20y%20Javascript" id="wpa2a_6">Compartir / Guardar</a></p><p>Related posts:<ol>
<li><a href='http://programadorasp.com/validar-campos-y-parametros-con-expresiones-regulares-en-asp/' rel='bookmark' title='Validar campos y parámetros con expresiones regulares en ASP'>Validar campos y parámetros con expresiones regulares en ASP</a></li>
<li><a href='http://programadorasp.com/validar-fechas-con-expresiones-regulares-en-asp-y-javascrip/' rel='bookmark' title='Validar fechas con expresiones regulares en ASP y Javascript'>Validar fechas con expresiones regulares en ASP y Javascript</a></li>
<li><a href='http://programadorasp.com/validar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript/' rel='bookmark' title='Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript'>Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://programadorasp.com/validar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validar fechas con expresiones regulares en ASP y Javascript</title>
		<link>http://programadorasp.com/validar-fechas-con-expresiones-regulares-en-asp-y-javascrip/</link>
		<comments>http://programadorasp.com/validar-fechas-con-expresiones-regulares-en-asp-y-javascrip/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 07:59:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programación]]></category>
		<category><![CDATA[expresiones regulares]]></category>
		<category><![CDATA[validar]]></category>
		<category><![CDATA[validar campos]]></category>
		<category><![CDATA[validar fechas]]></category>

		<guid isPermaLink="false">http://www.programadorasp.com/?p=104</guid>
		<description><![CDATA[Continuando con las expresiones regulares, para validar fechas se puede utilizar la siguiente función en ASP: function esValidoFecha&#40;cadena&#41; set expReg = New RegExp expReg.Pattern = &#34;^(((0[1-9]&#124;[12][0-9]&#124;3[01])([/])(0[13578]&#124;10&#124;12)([/])_ (\d{4}))&#124;(([0][1-9]&#124;[12][0-9]&#124;30)([/])(0[469]&#124;11)_ ([/])(\d{4}))&#124;((0[1-9]&#124;1[0-9]&#124;2[0-8])([/])(02)([/])_ (\d{4}))&#124;((29)(\.&#124;-&#124;\/)(02)([/])([02468][048]00))&#124;_ ((29)([/])(02)([/])([13579][26]00))&#124;((29)([/])(02)_ ([/])([0-9][0-9][0][48]))&#124;((29)([/])(02)([/])([0-9]_ [0-9][2468][048]))&#124;((29)([/])(02)([/])([0-9][0-9]_ [13579][26])))$&#34; esValidoFecha = expReg.Test&#40;cadena&#41; set expReg = nothing end function La expresión regular (que se puede poner en una única línea eliminando los [...]
Related posts:<ol>
<li><a href='http://programadorasp.com/validar-campos-y-parametros-con-expresiones-regulares-en-asp/' rel='bookmark' title='Validar campos y parámetros con expresiones regulares en ASP'>Validar campos y parámetros con expresiones regulares en ASP</a></li>
<li><a href='http://programadorasp.com/validar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript/' rel='bookmark' title='Validar direcciones de e-mail con expresiones regulares en ASP y Javascript'>Validar direcciones de e-mail con expresiones regulares en ASP y Javascript</a></li>
<li><a href='http://programadorasp.com/validar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript/' rel='bookmark' title='Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript'>Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Continuando con las expresiones regulares, para validar fechas se puede utilizar la siguiente función en ASP:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">function</span> esValidoFecha<span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span>
  <span style="color: #990099; font-weight: bold;">set</span> expReg <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">New</span> RegExp
  expReg.<span style="color: #9900cc;">Pattern</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;^(((0[1-9]|[12][0-9]|3[01])([/])(0[13578]|10|12)([/])_
                    (\d{4}))|(([0][1-9]|[12][0-9]|30)([/])(0[469]|11)_
                    ([/])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([/])(02)([/])_
                    (\d{4}))|((29)(\.|-|\/)(02)([/])([02468][048]00))|_
                    ((29)([/])(02)([/])([13579][26]00))|((29)([/])(02)_
                    ([/])([0-9][0-9][0][48]))|((29)([/])(02)([/])([0-9]_
                    [0-9][2468][048]))|((29)([/])(02)([/])([0-9][0-9]_
                    [13579][26])))$&quot;</span>
  esValidoFecha <span style="color: #006600; font-weight: bold;">=</span> expReg.<span style="color: #9900cc;">Test</span><span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span>
  <span style="color: #990099; font-weight: bold;">set</span> expReg <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">nothing</span>
<span style="color: #990099; font-weight: bold;">end</span> <span style="color: #0000ff; font-weight: bold;">function</span></pre></div></div>

<p>La expresión regular (que se puede poner en una única línea eliminando los _ finales) es un poco larga porque ha de tener en cuenta los días de los meses del año, los años bisiestos,&#8230; Para validar del lado del cliente (antes de enviar el formulario), se puede utilizar el mismo patrón en Javascript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> esValidoFecha<span style="color: #009900;">&#40;</span>cadena<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  strExpReg <span style="color: #339933;">=</span> <span style="color: #339933;">/^</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">12</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#91;</span>01<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">13578</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #CC0000;">10</span><span style="color: #339933;">|</span><span style="color: #CC0000;">12</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>_
                <span style="color: #009900;">&#40;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">12</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">469</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #CC0000;">11</span><span style="color: #009900;">&#41;</span>_
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">-</span><span style="color: #CC0000;">8</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>02<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>_
                <span style="color: #009900;">&#40;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">29</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>\.<span style="color: #339933;">|-|</span>\<span style="color: #339933;">/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>02<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>02468<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>048<span style="color: #009900;">&#93;</span>00<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span>_
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">29</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>02<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">13579</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">26</span><span style="color: #009900;">&#93;</span>00<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">29</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>02<span style="color: #009900;">&#41;</span>_
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">48</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">29</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>02<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span>_
                <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2468</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>048<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">29</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>02<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">-</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span>_
                <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">13579</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">26</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>$<span style="color: #339933;">/;</span>
  <span style="color: #000066; font-weight: bold;">return</span> strExpReg.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>cadena<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>En esta función en Javascript sí que es necesario juntar la expresión en una única línea quitando los _ porque de lo contrario no funcionará.</p>
<p><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-fechas-con-expresiones-regulares-en-asp-y-javascrip%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-fechas-con-expresiones-regulares-en-asp-y-javascrip%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><!--<![endif]--><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-fechas-con-expresiones-regulares-en-asp-y-javascrip%2F&amp;counturl=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-fechas-con-expresiones-regulares-en-asp-y-javascrip%2F&amp;count=horizontal&amp;text=Validar%20fechas%20con%20expresiones%20regulares%20en%20ASP%20y%20Javascript" scrolling="no" style="border:none;overflow:hidden;width:130px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-fechas-con-expresiones-regulares-en-asp-y-javascrip%2F&amp;counturl=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-fechas-con-expresiones-regulares-en-asp-y-javascrip%2F&amp;count=horizontal&amp;text=Validar%20fechas%20con%20expresiones%20regulares%20en%20ASP%20y%20Javascript" scrolling="no" style="border:none;overflow:hidden;width:130px;height:20px"></iframe><!--<![endif]--><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-fechas-con-expresiones-regulares-en-asp-y-javascrip%2F&amp;size=medium&amp;count=true" scrolling="no" style="border:none;overflow:hidden;width:90px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-fechas-con-expresiones-regulares-en-asp-y-javascrip%2F&amp;size=medium&amp;count=true" scrolling="no" style="border:none;overflow:hidden;width:90px;height:20px"></iframe><!--<![endif]--><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fprogramadorasp.com%2Fvalidar-fechas-con-expresiones-regulares-en-asp-y-javascrip%2F&amp;title=Validar%20fechas%20con%20expresiones%20regulares%20en%20ASP%20y%20Javascript" id="wpa2a_8">Compartir / Guardar</a></p><p>Related posts:<ol>
<li><a href='http://programadorasp.com/validar-campos-y-parametros-con-expresiones-regulares-en-asp/' rel='bookmark' title='Validar campos y parámetros con expresiones regulares en ASP'>Validar campos y parámetros con expresiones regulares en ASP</a></li>
<li><a href='http://programadorasp.com/validar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript/' rel='bookmark' title='Validar direcciones de e-mail con expresiones regulares en ASP y Javascript'>Validar direcciones de e-mail con expresiones regulares en ASP y Javascript</a></li>
<li><a href='http://programadorasp.com/validar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript/' rel='bookmark' title='Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript'>Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://programadorasp.com/validar-fechas-con-expresiones-regulares-en-asp-y-javascrip/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Validar campos y parámetros con expresiones regulares en ASP</title>
		<link>http://programadorasp.com/validar-campos-y-parametros-con-expresiones-regulares-en-asp/</link>
		<comments>http://programadorasp.com/validar-campos-y-parametros-con-expresiones-regulares-en-asp/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 07:12:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programación]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[expresiones regulares]]></category>
		<category><![CDATA[formularios web]]></category>
		<category><![CDATA[RegExLib]]></category>
		<category><![CDATA[sql inyectado]]></category>
		<category><![CDATA[validar campos]]></category>

		<guid isPermaLink="false">http://www.programadorasp.com/?p=69</guid>
		<description><![CDATA[Las expresiones regulares en programación son útiles para validar campos enviados y parámetros recibidos de formularios web en lenguaje ASP o Javascript.
Related posts:<ol>
<li><a href='http://programadorasp.com/validar-fechas-con-expresiones-regulares-en-asp-y-javascrip/' rel='bookmark' title='Validar fechas con expresiones regulares en ASP y Javascript'>Validar fechas con expresiones regulares en ASP y Javascript</a></li>
<li><a href='http://programadorasp.com/validar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript/' rel='bookmark' title='Validar direcciones de e-mail con expresiones regulares en ASP y Javascript'>Validar direcciones de e-mail con expresiones regulares en ASP y Javascript</a></li>
<li><a href='http://programadorasp.com/validar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript/' rel='bookmark' title='Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript'>Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Las <a href="http://es.wikipedia.org/wiki/Expresi%C3%B3n_regular" target="_blank">expresiones regulares</a> en programación son muy útiles para validar campos enviados y parámetros recibidos de formularios web, ya sean de contacto, usuario y contraseña o de cualquier otro tipo. Es cierto que las expresiones regulares pueden ser muy complejas, pero con algunos ejemplos sencillos y un poco de práctica podemos apreciar su verdadera potencia. En el mercado existen muchas soluciones predefinidas para no tener que ir validando manualmente los parámetros y &#8220;parseando&#8221; las entradas, pero hace tiempo que yo vengo utilizando algunas funciones propias que de forma sencilla evitan el tan temido <a href="http://es.wikipedia.org/wiki/Inyecci%C3%B3n_SQL" target="_blank">SQL inyectado</a>. En ASP es muy fácil utilizar expresiones regulares. Por ejemplo, para validar cadenas numéricas y alfanuméricas, utilizo estas dos funciones:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">function</span> esValidoNum<span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span>
  <span style="color: #990099; font-weight: bold;">set</span> expReg <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">New</span> RegExp
  expReg.<span style="color: #9900cc;">Pattern</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;^[0-9]+$&quot;</span>
  esValidoNum <span style="color: #006600; font-weight: bold;">=</span> expReg.<span style="color: #9900cc;">Test</span><span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">and</span> <span style="color: #330066;">len</span><span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #006600; font-weight: bold;">&amp;</span>lt<span style="color: #006600; font-weight: bold;">;</span> <span style="color: #800000;">6</span>
  <span style="color: #990099; font-weight: bold;">set</span> expReg <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">nothing</span>
<span style="color: #990099; font-weight: bold;">end</span> <span style="color: #0000ff; font-weight: bold;">function</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">function</span> esValidoAlfaNum<span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span>
  <span style="color: #990099; font-weight: bold;">set</span> expReg <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">New</span> RegExp
  expReg.<span style="color: #9900cc;">Pattern</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;^[a-zA-Z0-9-]+$&quot;</span>
  esValidoAlfaNum <span style="color: #006600; font-weight: bold;">=</span> expReg.<span style="color: #9900cc;">Test</span><span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">and</span> <span style="color: #330066;">len</span><span style="color: #006600; font-weight:bold;">&#40;</span>cadena<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #006600; font-weight: bold;">&amp;</span>lt<span style="color: #006600; font-weight: bold;">;</span> <span style="color: #800000;">256</span>
  <span style="color: #990099; font-weight: bold;">set</span> expReg <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">nothing</span>
<span style="color: #990099; font-weight: bold;">end</span> <span style="color: #0000ff; font-weight: bold;">function</span></pre></div></div>

<p>Como norma general, en primer lugar se declara la variable que tendrá la expresión regular y se le especifica el patrón a seguir mediante los diferentes símbolos:</p>
<p>^ : comienzo de la expresión regular<br />
$ : fin de la expresión regular<br />
+ : utilizado para especificar una o más apariciones de los símbolos precedentes<br />
[...] : rango</p>
<p>Así pues en el primer ejemplo anterior se validan expresiones numéricas, es decir, una o más repeticiones de números entre 0 y 9, y de longitud menor que 6 (len(cadena)&lt;6), mientras que en el segundo se validan cadenas alfanuméricas (sin acentos, ñ, ç,&#8230;), es decir, una o más repeticiones de letras minúsculas entre a y z, mayúsculas entre A y Z, números entre 0 y 9 y guiones (-), y de longitud menor de 256 carácteres (len(cadena)&lt;256).</p>
<p>Con estas simples pero potentes funciones podemos validar usuarios y contraseñas y parámetros sencillos pasados por GET o POST como id&#8217;s, cadenas simples,&#8230;</p>
<p>En todos los lenguajes existen las expresiones regulares y el uso es bastante similar. De hecho, en Javascript es muy fácil utilizar las funciones para validar los campos en el cliente antes de enviar el formulario web.</p>
<p>Se pueden encontrar expresiones regulares predefinidas para validar e-mails, teléfonos, fechas, horas,&#8230; en <a href="http://regexlib.com" target="_blank">RegExLib</a>, una web muy útil.</p>
<p>Related posts:<ol>
<li><a href='http://programadorasp.com/validar-fechas-con-expresiones-regulares-en-asp-y-javascrip/' rel='bookmark' title='Validar fechas con expresiones regulares en ASP y Javascript'>Validar fechas con expresiones regulares en ASP y Javascript</a></li>
<li><a href='http://programadorasp.com/validar-direcciones-de-e-mail-con-expresiones-regulares-en-asp-y-javascript/' rel='bookmark' title='Validar direcciones de e-mail con expresiones regulares en ASP y Javascript'>Validar direcciones de e-mail con expresiones regulares en ASP y Javascript</a></li>
<li><a href='http://programadorasp.com/validar-direcciones-de-internet-o-urls-con-expresiones-regulares-en-javascript/' rel='bookmark' title='Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript'>Validar direcciones de Internet o URL&#8217;s con expresiones regulares en Javascript</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://programadorasp.com/validar-campos-y-parametros-con-expresiones-regulares-en-asp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

