VALIDAD ENTRADAS NUMÉRICAS
Aquí está el código del guión:
<SCRIPT LANGUAGE="JavaScript">
// Este es un comentario.
// El primer condicional asegura que el campo no este vacio.
// El segundo condicional verifica si la cadena recibida (str) es o no un número
// en caso de que str NO sea un número entonces isNaN(str) devuelve un valor verdadero
// El tercero nos asegura que la entrada sea un numero entre 50 o 1000.
// En caso negativo mandamos una alerta al usuario de que la entrada no ,,es valida.
function checkNum (str, min, max) {
if (str == "") {
alert("Teclee un numero en el campo.")
return false
}
if (isNaN(str)) {
alert("Teclee un numero en el campo.")
return false
}
var num = 0 + str
if (num < min || num > max) {
alert("El numero debe estar entre 50 y 1000.")
return false
}
return true
}
function thanks() {
alert("Gracias por retirar efectivo.")
}
<!-- end -->
</SCRIPT>
Este es el código de la forma:
<P><FORM><INPUT TYPE="text" NAME="num" VALUE="" SIZE=5
onchange="if (!checkNum(this.value,50,1000))
{this.focus();this.select();} else {thanks()}">
<INPUT TYPE="button" VALUE="Envie"></FORM>
VALIDAR ENTRADAS DE TEXTO
Aquí está el código del guión:
<SCRIPT
LANGUAGE="JavaScript"><!--
function runTest(form, button) {
Ret = false;
if (button.name == "1") Ret = testBox1(form);
if (button.name == "2") Ret = testBox2(form);
if (button.name == "3") Ret = testBox3(form);
if (button.name == "4") Ret = testBox4(form);
if (Ret)
alert ("Successful input!");
}
function testBox1(form) {
Ctrl = form.inputbox1;
if (Ctrl.value == "" || Ctrl.value.indexOf ('@', 0) == -1) {
validatePrompt (Ctrl, "Enter a valid email address")
return (false);
} else
return (true);
}
function testBox2(form) {
Ctrl = form.inputbox2;
if (Ctrl.value.length != 5) {
validatePrompt (Ctrl, "Please provide five characters")
return (false);
} else
return (true);
}
function testBox3(form) {
Ctrl = form.inputbox3;
if (Ctrl.value.length < 3) {
validatePrompt (Ctrl, "Please provide at least three characters")
return (false);
} else
return (true);
}
function testBox4(form) {
Ctrl = form.inputbox4;
if (Ctrl.value == "") {
validatePrompt (Ctrl, "Please provide a value for this box")
return (false);
} else
return (true);
}
function runSubmit (form, button) {
if (!testBox1(form)) return;
if (!testBox2(form)) return;
if (!testBox3(form)) return;
if (!testBox4(form)) return;
alert ("All entries verified OK!");
//document.test.submit(); // un-comment to submit form
return;
}
function validatePrompt (Ctrl, PromptStr) {
alert (PromptStr)
Ctrl.focus();
return;
}
function loadDoc() {
// initial focus; use if needed
//document.test.inputbox1.focus ();
return;
}
//--></SCRIPT>
Este es el código de la forma:
<INPUT TYPE="text" NAME="inputbox1" VALUE=""
SIZE=30><INPUT TYPE="button" NAME="1" VALUE="Pruebe"
onClick="runTest(this.form, this)">
EJERCICIO DE APLICACION 1
Combinación de PHP. JAVASCRIPT para validar datos numéricos de entrada y crear una tabla de x filas por y columnas, VER RESULTADO , los códigos son los siguientes:
<html>
<head>
<title>CREA TABLAS</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
function checkNum (str, min, max) {
if (str == "") {
alert("Teclee un numero en el campo.")
return false
}
if (isNaN(str)) {
alert("Teclee un numero en el campo.")
return false
}
var num = 0 + str
if (num < min || num > max) {
alert("El numero debe estar entre 1 y 20.")
return false
}
return true
}
function thanks() {
alert("Gracias.")
}
</SCRIPT>
</head>
<! Se pone fondo a la página >
<body background="fondo1.jpg">
<?
$paso="S";
if (!$Submit) {
$paso = "N";
}
if (($Filas=="" or $Columnas=="") and $paso=="S") {
$paso="N";
echo "Faltan datos";
}
if ($paso=="N") {
?>
<form name="form1" method="post" action="">
<p align="center"> </p>
<p align="center"><strong><font color="#990000">FORMULARIO PARA GENERAR TABLA</font></strong></p>
<div align="center">
<table width="55%" border="1" cellpadding="0" cellspacing="0" bordercolor="#0099FF">
<tr>
<td width="58%"><p align="right">Introduzca Título:<br>
</p>
</td>
<td width="42%"> <div align="left">
<input name="Titulo" type="text" id="Titulo">
</div></td>
</tr>
<tr>
<td>Introduzca Nº de Filas <font size="-1">(máx 30):</font></td>
<td> <div align="left">
<input name="Filas" type="text" id="Filas" onChange="if (!checkNum(this.value,1,30)) {this.focus();this.select();}" size="5">
<font color="#FF0000">*</font></div></td>
</tr>
<tr>
<td>Introduzca Nº de Columnas <font size="-1">(máx 20):</font></td>
<td> <div align="left">
<input name="Columnas" type="text" id="Columnas" onChange="if (!checkNum(this.value,1,20)) {this.focus();this.select();}" size="5">
<font color="#FF0000">*</font></div></td>
</tr>
</table>
<br>
<table width="42%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="43%"><div align="center">
<input type="submit" name="Submit" value="Enviar" >
</div></td>
<td width="57%"><div align="center">
<input name="Limpiar" type="reset" id="Limpiar" value="Limpiar">
</div></td>
</tr>
</table>
<p><font color="#FF0000">*</font> <font color="#FF0000" size="-1">indispensable llenar</font></p>
</div>
<p> </p>
</form>
<?
} else {
?>
<p align="center"><strong><font color="#990000" size="+2">TABLA : <? echo "$Titulo"; ?></font></strong></p>
<?
// se crea tabla centrada
echo "<div align='center'>";
echo "<table border=1>";
for($i=0;$i<$Filas;$i++) // $Filas es la variable dada en el formulario como número de filas
{
echo "<tr>";
for($j=0 ; $j<$Columnas ; $j++) // para cada fila realiza el proceso de crear las celdas
//con el dato de número de columnas.
echo "<td div align='center'><font color='#FF0000' size='-1'>".($i+1)." - ".($j+1)."</font></div></td>";
echo "</tr>";
}
echo "</table>";
}
?>
<p> </p>
</body>
</html>
EJERCICIO DE APLICACION 2
Validar correo y dato numéricos de entrada utilizando solo PHP, en especial el comando eregi VER RESULTADO , los códigos son los siguientes:
<HTML>
<HEAD>
<TITLE>Validación de tipos de datos
texto.</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</HEAD>
<BODY BGCOLOR="#FFFFFF" background="../../fondo1.jpg" TEXT="#3B5926" >
<?
$paso="S";
if (!$Enviar) {
$paso = "N";
}
if (($email=="" or $valor=="" or $valor < 10000 or $valor > 1000000) and $paso=="S") {
$paso="N";
echo "datos vacios o valor no permitido";
}
if (!$email=="") {
function email_valido($email) {
if (eregi("^[_\.0-9a-z-]+@[0-9a-z\._\-]+\.[a-z]{2,4}$", $email)) return true;
else return false;
}
if(!email_valido($email)) {
$paso="N";
echo "email no válida".$email;
}
}
if ($paso=="N") {
?>
<FORM action="" method="POST">
<P>
<div align="center">
<TABLE BORDER=1 CELLPADDING=1 WIDTH="80%">
<TR>
<TD>
<CENTER>
<TABLE BORDER=0 CELLPADDING=1 WIDTH=100%>
<TR>
<TD bgcolor=#D0CDE5> <P> Teclee su dirección de correo
</TD>
<TD bgcolor=#E6FFB3><input type="text" name="email" value=""
size=30>
</TD>
</TR>
<TR>
<TD bgcolor=#D0CDE5> <P>Valor entre 10000 y 1000000: </TD>
<TD bgcolor=#E6FFB3> <P>
<input type="text" name="valor" value="" size=10>
</TD>
</TR>
<TR>
<TD colspan="2" bgcolor=#000000> <P align="center"><br>
<INPUT NAME="Enviar" TYPE="submit" id="Enviar" VALUE="Enviar">
<P> </TD>
</TR>
</TABLE>
</CENTER>
</TD>
</TR>
</TABLE>
<p> </p></div>
</FORM>
<?
} else {
?>
<div align="center">
<p> </p><table width="50%" border="0">
<tr>
<td width="67%"><font color="#990000" size="+1">El valor registrado fue de:</font></td>
<td width="33%"> <font color="#990000" size="+1">
<?
echo "$".$valor;
?>
</font></td>
</tr>
</table>
</div>
<?
}
?>
</BODY>
</HTML>