
28/09/2006, 09:16
|
| | Fecha de Ingreso: octubre-2004
Mensajes: 230
Antigüedad: 20 años, 4 meses Puntos: 0 | |
Problemilla validando un form con checkFileUpload Hola vereis tengo un form para subir datos y una imagen a una base de datos, bien, esto me lo hace pero quiero validar unos campos y tb que la imagen que suban tenga unas dimensiones especificas y que no supere un peso, pues lo que me pasa esque consigo que me salgan los mensajes de "Falta el titulo" y los de "La imagen tiene que tener x anchura" y el de "La imagen es demasiado pesada tiene que ser de x kb" pero cuando le doy al boton de aceptar estos mensajes se lo pasa por el forro y me la inserta en la db.......  hay os pongo el código a ver si alguien me dice algo
Código:
function checkFileUpload(form,extensions,requireUpload,sizeLimit,minWidth,minHeight,maxWidth,maxHeight,saveWidth,saveHeight) { //v2.0
document.MM_returnValue = true;
if (extensions != '') var re = new RegExp("\.(" + extensions.replace(/,/gi,"|").replace(/s/gi,"") + ")$","i");
for (var i = 0; i<form.elements.length; i++) {
field = form.elements[i];
if (field.type.toUpperCase() != 'FILE') continue;
if (field.value == '') {
if (requireUpload) {alert('Archivo requerido!');document.MM_returnValue = false;field.focus();break;}
} else {
if(extensions != '' && !re.test(field.value)) {
alert('Este tipo de archivo no esta permitido.\nSolo las siguientes extensiones estan permitidas ' + extensions + '.\nSelecciona otro archivo.');
document.MM_returnValue = false;
field.focus();break;
}
document.PU_uploadForm = form;
re = new RegExp(".(gif|jpg|jpeg)$","i");
if(re.test(field.value) && (sizeLimit != '' || minWidth != '' || minHeight != '' || maxWidth != '' || maxHeight != '' || saveWidth != '' || saveHeight != '')) {
checkImageDimensions(field,sizeLimit,minWidth,minHeight,maxWidth,maxHeight,saveWidth,saveHeight);
} } }
}
function showImageDimensions(fieldImg) { //v2.0
var isNS6 = (!document.all && document.getElementById ? true : false);
var img = (fieldImg && !isNS6 ? fieldImg : this);
if ((img.minWidth != '' && img.minWidth > img.width) || (img.minHeight != '' && img.minHeight > img.height)) {
alert('La anchura de la imagen elegida es demasiado pequeña!\nDebería ser al menos ' + img.minWidth + ' x ' + img.minHeight); false;}
if ((img.maxWidth != '' && img.width > img.maxWidth) || (img.maxHeight != '' && img.height > img.maxHeight)) {
alert('La anchura de la imagen elegida es demasiado grande!\nDebería ser de un máximo de ' + img.maxWidth); false;}
if (img.sizeLimit != '' && img.fileSize > img.sizeLimit) {
alert('La imagen elegida es demasiado pesada!\nDebería ser de un máximo de ' + (img.sizeLimit/1024) + ' KBytes'); false;}
if (img.saveWidth != '') document.PU_uploadForm[img.saveWidth].value = img.width;
if (img.saveHeight != '') document.PU_uploadForm[img.saveHeight].value = img.height;
document.MM_returnValue = true;
}
function checkImageDimensions(field,sizeL,minW,minH,maxW,maxH,saveW,saveH) { //v2.0
if (!document.layers) {
var isNS6 = (!document.all && document.getElementById ? true : false);
document.MM_returnValue = false; var imgURL = 'file:///' + field.value.replace(/\\/gi,'/');
if (!field.gp_img || (field.gp_img && field.gp_img.src != imgURL) || isNS6) {field.gp_img = new Image();
with (field) {gp_img.sizeLimit = sizeL*1024; gp_img.minWidth = minW; gp_img.minHeight = minH; gp_img.maxWidth = maxW; gp_img.maxHeight = maxH;
gp_img.saveWidth = saveW; gp_img.saveHeight = saveH; gp_img.onload = showImageDimensions; gp_img.src = imgURL; }
} else showImageDimensions(field.gp_img);}
}
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') {
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' se requiere.\n'; }
} if (errors) alert('Han ocurrido los siguientes errores:\n'+errors);
document.MM_returnValue = (errors == '');
}
Código HTML: <FORM name="form1" id="form1" method="post" enctype="multipart/form-data" action="<%=MM_editAction%>" onSubmit="checkFileUpload(this,'GIF,JPG,JPEG',false,'25','','','140','','','');MM_validateForm('titulo','','R');return document.MM_returnValue" >
<input type="hidden" name="id" value="<%= cnuevo %>">
<table width="100%" border="0" align="left" cellpadding="0" cellspacing="0">
<tr style="padding-left:10">
<td height="4" colspan="5" valign="top"></td>
</tr>
<tr style="padding-left:10">
<td height="15" valign="top" bgcolor="#d8eec5" class="e_azul">Titulo</td>
<td height="15" valign="top" colspan="4">
<input type="text" name="titulo" size="60">
</td>
</tr>
<tr style="padding-left:10">
<td width="90" height="15" bgcolor="#d8eec5" class="e_azul" valign="top">Fecha</td>
<td width="166" height="15" valign="top">
<p><input name="fecha" type="text" size="10" maxlength="10">
(dd/mm/aaaa)</p>
</td>
</tr>
<tr style="padding-left:10">
<td width="90" height="15" bgcolor="#d8eec5" class="e_azul" valign="top">Estado</td>
<td width="166" height="15" valign="top">
<select name="estado" class="menutitle2">
<option value=" ">Activa</option>
<option value="si">De Baja</option>
</select>
</td>
</tr>
<tr style="padding-left:10">
<td width="90" height="15" bgcolor="#d8eec5" class="e_azul" valign="top">Scroll</td>
<td width="166" height="15" valign="top">
<select name="scro" class="menutitle2">
<option value="si">Si</option>
<option value="no">No</option>
</select>
</td>
</tr>
<tr style="padding-left:10">
<td width="90" height="15" bgcolor="#d8eec5" class="e_azul" valign="top">Imagen:</td>
<td width="166" height="15" valign="top">
<input name="image" type="file" id="file">
</td>
</tr>
<tr style="padding-left:10">
<td width="90" valign="top" bgcolor="#d8eec5" class="e_azul">Cuerpo</td>
<td colspan="4" valign="top">
<textarea name="cuerpo" cols="80" rows="8"></textarea>
</td>
<tr></tr>
<td colspan="2" valign="top" class="e_azul">
<div align="center">
<input name="Action" type="submit" class="input50" value="Insertar" >
<input name="Submit2" type="reset" class="input90" value="Reestablecer">
</div></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><div align="center"><a href="javascript:history.go(-1);" class="azul"><strong>Volver</strong></a></div></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="true">
</FORM> |