Ver Mensaje Individual
  #6 (permalink)  
Antiguo 18/03/2006, 09:41
Avatar de uamistad
uamistad
 
Fecha de Ingreso: diciembre-2004
Ubicación: Cd. de México
Mensajes: 1.395
Antigüedad: 19 años, 4 meses
Puntos: 1
interesante ejercicio

Me gustó tu ejercicio y lo resolví.

Nada más que el estilo de la expresión regular es más bien al estilo PHP, porque el que he visto por aquí casi no lo manejo.

Pero funciona, chécala, =)

Código PHP:
//
//

<html><head><title>Expresiones Regulares</title></head>
<
body>

<
script>
    
//VARIABLE GLOBAL
    
var textoAnterior '';

    
//ESTA FUNCIÓN DEFINE LAS REGLAS DEL JUEGO
    
function cumpleReglas(simpleTexto)
        {
            
//la pasamos por una poderosa expresión regular
            
var expresion = new RegExp("^(|([0-9]{1,2}(\\.([0-9]{1,2})?)?))$");

            
//si pasa la prueba, es válida
            
if(expresion.test(simpleTexto))
                return 
true;
            return 
false;
        }
//end function checaReglas

    //ESTA FUNCIÓN REVISA QUE TODO LO QUE SE ESCRIBA ESTÉ EN ORDEN
    
function revisaCadena(textItem)
        {
            
//si comienza con un punto, le agregamos un cero
            
if(textItem.value.substring(0,1) == '.'
                
textItem.value '0' textItem.value;

            
//si no cumples las reglas, no te dejo escribir
            
if(!cumpleReglas(textItem.value))
                
textItem.value textoAnterior;
            else 
//todo en orden
                
textoAnterior textItem.value;
        }
//end function revisaCadena
</script>

<form action="#">
    <input type="text" onKeyUp="revisaCadena(this)">
</form>
</body></html> 
__________________
"Di no al Internet Explorer" -Proverbio Chino-