• Viernes 29 de Marzo de 2024, 02:06

Autor Tema:  Validar Formulario  (Leído 2750 veces)

Aguante Kanario

  • Nuevo Miembro
  • *
  • Mensajes: 3
    • Ver Perfil
Validar Formulario
« en: Lunes 17 de Mayo de 2004, 23:12 »
0
Buenas tengo el sig. codigo:

<script LANGUAGE="JavaScript">
var defaultEmptyOK = false
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyzáéíóúñü"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚÑ"
var whitespace = " \t\n\r";
var phoneChars = "()-+ ";
var mMessage = "Error: no puede dejar este espacio vacio"
var pPrompt = "Error: ";
var pAlphanumeric = "ingrese un texto que contenga solo letras y/o numeros";
var pAlphabetic   = "ingrese un texto que contenga solo letras";
var pInteger = "ingrese un numero entero";
var pNumber = "ingrese un numero";
var pPhoneNumber = "ingrese un número de teléfono";
var pEmail = "ingrese una dirección de correo electrónico válida";
var pName = "ingrese un texto que contenga solo letras, numeros o espacios";
var pFecha = "ERROR: Fecha no válida, el formato es:  dd/mm/aaaa";

function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this = 0
   }
   return this
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


function isWhitespace (s)
{   var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {  
        var c = s.charAt(i);
        // si el caracter en que estoy no aparece en whitespace,
        // entonces retornar falso
        if (whitespace.indexOf© == -1) return false;
    }
    return true;
}


function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {   var c = s.charAt(i);
        if (bag.indexOf© == -1) returnString += c;
    }

    return returnString;
}


function stripCharsNotInBag (s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {  
        var c = s.charAt(i);
        if (bag.indexOf© != -1) returnString += c;
    }

    return returnString;
}


function stripWhitespace (s)
{   return stripCharsInBag (s, whitespace)
}

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}


function stripInitialWhitespace (s)
{   var i = 0;
    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    return s.substring (i, s.length);
}


function isLetter ©
{
    return( ( uppercaseLetters.indexOf( c ) != -1 ) ||
            ( lowercaseLetters.indexOf( c ) != -1 ) )
}


function isDigit ©
{   return ((c >= "0") && (c <= "9"))
}


function isLetterOrDigit ©
{   return (isLetter© || isDigit©)
}


function isInteger (s)
{   var i;
    if (isEmpty(s))
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
   
    for (i = 0; i < s.length; i++)
    {  
        var c = s.charAt(i);
        if( i != 0 ) {
            if (!isDigit©) return false;
        } else {
            if (!isDigit© && (c != "-") || (c == "+")) return false;
        }
    }
    return true;
}

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 (i = 0; i < s.length; i++)
    {  
        var c = s.charAt(i);
        if( i != 0 ) {
            if ( c == "." ) {
                if( !dotAppeared )
                    dotAppeared = true;
                else
                    return false;
            } else    
                if (!isDigit©) return false;
        } else {
            if ( c == "." ) {
                if( !dotAppeared )
                    dotAppeared = true;
                else
                    return false;
            } else    
                if (!isDigit© && (c != "-") || (c == "+")) return false;
        }
    }
    return true;
}


function isAlphabetic (s)
{   var i;

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

        if (!isLetter©)
        return false;
    }
    return true;
}
 
function isAlphanumeric (s)
{   var i;

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

    for (i = 0; i < s.length; i++)
    {  
        var c = s.charAt(i);

  if (! (isLetter© || isDigit© ) )
        return false;
    }

    return true;
}


function isName (s)
{
    if (isEmpty(s))
       if (isName.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);
   
    return( isAlphanumeric( stripCharsInBag( s, whitespace ) ) );
}


function isPhoneNumber (s)
{   var modString;
    if (isEmpty(s))
       if (isPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isPhoneNumber.arguments[1] == true);
    modString = stripCharsInBag( s, phoneChars );
    return (isInteger(modString))
}

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 i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

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

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

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

function isFecha (s)
{
   if (s)
   {  
      borrar =s;
      if ((s.substr(2,1) == "/") && (s.substr(5,1) == "/"))
      {      
         for (i=0; i<10; i++)
        {   
            if (((s.substr(i,1)<"0") || (s.substr(i,1)>"9")) && (i != 2) && (i != 5))
         {
               borrar = '';
               break;  
         }  
         }
        if (borrar)
        {
           a = s.substr(6,4);
          m = s.substr(3,2);
          d = s.substr(0,2);
          if((a < 1900) || (a > 2050) || (m < 1) || (m > 12) || (d < 1) || (d > 31))
             borrar = '';
          else
          {
             if((a%4 != 0) && (m == 2) && (d > 28))     
                borrar = ''; // Año no viciesto y es febrero y el dia es mayor a 28
            else   
            {
                if ((((m == 4) || (m == 6) || (m == 9) || (m==11)) && (d>30)) || ((m==2) && (d>29)))
                  borrar = '';                          
            }  // else
          } // fin else
         } // if (error)
      } // if ((s.substr(2,1) == "/") && (s.substr(5,1) == "/"))                      
     else
        borrar = '';
     if (borrar == '')
        return false;
      else
      return true;
   } // if (s)  
}


function statBar (s)
{   window.status = s
}

function warnEmpty (theField)
{   theField.focus()
    alert(mMessage)
    statBar(mMessage)
    return false
}

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    statBar(pPrompt + s)
    return false
}


function checkField (theField, theFunction, emptyOK, s)
{  
    var msg;
    if (checkField.arguments.length < 3) emptyOK = 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( theFunction == isFecha ) msg = pFecha;
    }
   
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;

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

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

}
</SCRIPT>


luego los llamo a traves de un requiere de php..

abajo del formulario2.. llamo esto:

<script language="JavaScript">
function verificar () {
    if( checkField( document.form2.rut_apoderado, isAlphanumeric, false ) &&
        checkField( document.form2.nombre_apoderado, isAlphabetic, false ) &&
        checkField( document.form2.a_pat_apoderado, isAlphabetic, false ) &&
        checkField( document.form2.a_mat_apoderado, isAlphabetic, false ) &&
        checkField( document.form2.fono_apoderado, isNumber, false ) &&
        checkField( document.form2.ciudad, isAlphanumeric, false ) &&
        checkField( document.form2.comuna, isAlphanumeric, false ) &&
        checkField( document.form2.direc_apoderado, isAlphanumeric, false ) )
        alert( "Todo verificado con exito" );
};
</script>


Bueno el problema q tengo es el sig. :

el alfanuemrico no me acepta los espacios, ej.

si quiero ingresar: pequeño Juan.
no va a pescar y tengo q ingresarlo junto .. pequeñojuan..

Como aceptar espacios??