Link to home
Start Free TrialLog in
Avatar of dereksheahan
dereksheahan

asked on

Javascript Validator

Hi All,
Does anyone know of a good Javascipt validating script that I can get my hands on for free. I only need simple checks like character length, correct file type entered, etc. I've tried downloading the  TMT validator from massimocorner.com but can't get it working on my pages. Has anyone come across this validator before? If so I could post up what I'm doing if that would help. Otherwise I would greatly appreciate it if someone could point me in the direction of a good script that is easy to implement.
Thanks,
D
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

Here is simple javascript file selector I use,
Easy to customize, I have added a preview functionality for image files,


<html>
<HEAD>
<script type="text/javascript">
// valid file types
var fileTypes=["bmp","gif","png","jpg","jpeg"];

// width to resize large images to
var maxWidth=100;
// height to resize large images to
var maxHeight=100;
// the id of the preview image tag
var outImage="previewField";
// what to display when the image is not valid
var defaultPic="spacer.gif";

function preview(what){
  var source=what.value;
  var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
  for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break;
  globalPic=new Image();
  if (i<fileTypes.length) globalPic.src=source;
  else {
    globalPic.src=defaultPic;
    alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+fileTypes.join(", "));
  }
  setTimeout("applyChanges()",200);
}
var globalPic;
function applyChanges(){
  var field=document.getElementById(outImage);
  var x=parseInt(globalPic.width);
  var y=parseInt(globalPic.height);
  if (x>maxWidth) {
    y*=maxWidth/x;
    x=maxWidth;
  }
  if (y>maxHeight) {
    x*=maxHeight/y;
    y=maxHeight;
  }
  field.style.display=(x<1 || y<1)?"none":"";
  field.src=globalPic.src;
  field.width=x;
  field.height=y;
}
</script>
</head>


<body>

<div align="center" style="line-height: 1.9em;">
Test it by locating a valid file on your hard drive:
<br>
<input type="file" id="picField" size="40" onchange="preview(this)">
<div style="border: solid black 1; width: 100px; height="100">
<img alt="Graphic will preview here" id="previewField" src="">
<div>
</body>
</html>
Avatar of dereksheahan
dereksheahan

ASKER

Hi pravinasar,
Not really sure if the above is suitable for what I need. I mostly just have textfields where I want to limit what user can enter, such as just numbers, or 13 digits only, must be a valid email address. As i mentioned above, I tried to download a script but couldn't get it to work. If anyone knew of a good generic script it might be more suitable than doing each field myself?
Thanks,
D
ASKER CERTIFIED SOLUTION
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial