Ver Mensaje Individual
  #3 (permalink)  
Antiguo 07/07/2008, 19:08
guybrush78
 
Fecha de Ingreso: febrero-2005
Ubicación: Cádiz
Mensajes: 50
Antigüedad: 20 años
Puntos: 2
De acuerdo Respuesta: Validar cadenas como numeros en formulario

y un poquito mas de codigo, ahora estan las que comprueban que es un numero:

Código PHP:
// ---------------------------------------------------------------------- //
//                          NUMEROS                                       //
// ---------------------------------------------------------------------- //

// s es un numero entero (con o sin signo)
function isInteger (s)
{   var 
i;
    if (
isEmpty(s)) 
       if (
isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (
isInteger.arguments[1] == true);
    
    for (
0s.lengthi++)
    {   
        var 
s.charAt(i);
        if( 
!= ) {
            if (!
isDigit(c)) return false;
        } else { 
            if (!
isDigit(c) && (!= "-") || (== "+")) return false;
        }
    }
    return 
true;
}

// s es un numero (entero o flotante, con o sin signo)
function isNumber (s)
{   var 
i;
    var 
dotAppeared;
    
dotAppeared false;
    if (
isEmpty(s)) 
       if (
isNumber.arguments.length == 1) return defaultEmptyOK;
       else return (
isNumber.arguments[1] == true);
    
    for (
0s.lengthi++)
    {   
        var 
s.charAt(i);
        if( 
!= ) {
            if ( 
== "." ) {
                if( !
dotAppeared )
                    
dotAppeared true;
                else
                    return 
false;
            } else     
                if (!
isDigit(c)) return false;
        } else { 
            if ( 
== "." ) {
                if( !
dotAppeared )
                    
dotAppeared true;
                else
                    return 
false;
            } else     
                if (!
isDigit(c) && (!= "-") || (== "+")) return false;
        }
    }
    return 
true;
}

// ---------------------------------------------------------------------- //
//                        STRINGS SIMPLES                                 //
// ---------------------------------------------------------------------- //

// s tiene solo letras
function isAlphabetic (s)
{   var 
i;

    if (
isEmpty(s)) 
       if (
isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (
isAlphabetic.arguments[1] == true);
    for (
0s.lengthi++)
    {   
        
// Check that current character is letter.
        
var s.charAt(i);

        if (!
isLetter(c))
        return 
false;
    }
    return 
true;
}


// s tiene solo letras y numeros
function isAlphanumeric (s)
{   var 
i;

    if (
isEmpty(s)) 
       if (
isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (
isAlphanumeric.arguments[1] == true);

    for (
0s.lengthi++)
    {   
        var 
s.charAt(i);
        if (! (
isLetter(c) || isDigit(c) ) )
        return 
false;
    }

    return 
true;
}

// s tiene solo letras, numeros o espacios en blanco
function isName (s)
{
    if (
isEmpty(s)) 
       if (
isName.arguments.length == 1) return defaultEmptyOK;
       else return (
isAlphanumeric.arguments[1] == true);
    
    return( 
isAlphanumericstripCharsInBagswhitespace ) ) );
}

// ---------------------------------------------------------------------- //
//                           FONO o EMAIL                                 //
// ---------------------------------------------------------------------- //

// s es numero de telefono valido
function isPhoneNumber (s)
{   var 
modString;
    if (
isEmpty(s)) 
       if (
isPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (
isPhoneNumber.arguments[1] == true);
    
modString stripCharsInBagsphoneChars );
    return (
isInteger(modString))
}

// s es una direccion de correo valida
function isEmail (s)
{
    if (
isEmpty(s)) 
       if (
isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (
isEmail.arguments[1] == true);
    if (
isWhitespace(s)) return false;
    var 
1;
    var 
sLength s.length;
    while ((
sLength) && (s.charAt(i) != "@"))
    { 
i++
    }

    if ((
>= sLength) || (s.charAt(i) != "@")) return false;
    else 
+= 2;

    while ((
sLength) && (s.charAt(i) != "."))
    { 
i++
    }

    if ((
>= sLength 1) || (s.charAt(i) != ".")) return false;
    else return 
true;
}

function 
isNice(s)
{
        var 
1;
        var 
sLength s.length;
        var 
1;
        while(
i<sLength) {
                if( (
s.charAt(i) == "\"") || (s.charAt(i) == "'" ) ) 0;
                
i++;
        }
        return 
b;
}

// ---------------------------------------------------------------------- //
//                  FUNCIONES PARA RECLAMARLE AL USUARIO                  //
// ---------------------------------------------------------------------- //

// pone el string s en la barra de estado
function statBar (s)
{   
window.status s
}

// notificar que el campo theField esta vacio
function warnEmpty (theField)
{   
theField.focus()
    
alert(mMessage)
    
statBar(mMessage)
    return 
false
}

// notificar que el campo theField es invalido
function warnInvalid (theFields)
{   
theField.focus()
    
theField.select()
    
alert(s)
    
statBar(pPrompt s)
    return 
false
}

// el corazon de todo: checkField
function checkField (theFieldtheFunctionemptyOKs)
{   
    var 
msg;
    if (
checkField.arguments.length 3emptyOK defaultEmptyOK;
    if (
checkField.arguments.length == 4) {
        
msg s;
    } else {
        if( 
theFunction == isAlphabetic msg pAlphabetic;
        if( 
theFunction == isAlphanumeric msg pAlphanumeric;
        if( 
theFunction == isInteger msg pInteger;
        if( 
theFunction == isNumber msg pNumber;
        if( 
theFunction == isEmail msg pEmail;
        if( 
theFunction == isPhoneNumber msg pPhoneNumber;
        if( 
theFunction == isName msg pName;
    }
    
    if ((
emptyOK == true) && (isEmpty(theField.value))) return true;

    if ((
emptyOK == false) && (isEmpty(theField.value))) 
        return 
warnEmpty(theField);

    if ( 
checkNiceness && !isNice(theField.value))
        return 
warnInvalid(theFieldpNice);

    if (
theFunction(theField.value) == true
        return 
true;
    else
        return 
warnInvalid(theField,msg);