Web Analytics Made Easy -
StatCounter Embedded spaces in image name - CodingForum


No announcement yet.

Embedded spaces in image name

  • Filter
  • Time
  • Show
Clear All
new posts

  • Embedded spaces in image name

    I have a form that allows me to upload images to my site. I works fine. The upload is done using a Perl script. Now I am adding a validation script that checks a few things, one of which I do not know how to do.

    How do I determine if there are any embedded blanks in the image names? For example "my image name.jpg" would not be valid, but "my_image_name.jpg" would be OK.


  • #2
    This is what I have so far:

    var text = document.upload.elements.photo1.value;
    for(c=0;c<text.length;c++) {
    if(text.charAt(c)==' ') {
    var errMsg = "Image names cannot have embedded blanks!";

    Not the best way I guess, but one problem is that I do not want to invalidate the file name if the path contains embedded blanks. That is OK. Only if the file name itself has embedded blanks. So I will have to figure out how to find the last slash I guess?


    • #3
      Using split to get last piece

      Let me ask this another way. Is there a better way?
      And also, how can I put this into a function so that I can call it twenty times?

      var text = document.upload.elements.photo1.value;
      var spit = text.split("\\");
      var x;
      var count;
      for (x in spit)
      text = spit[x];
      for(c=0;c<text.length;c++) {
      if(text.charAt(c)==' ') {
      var errMsg = "Image names cannot have embedded blanks!";
      frm.elements.photo1.className = "inputreq";


      • #4
        Use a regular expression.

        PHP Code:
        var errMsg "";
        errStr "Image names can't contain spaces, line returns, tabs or null charactors!";
            for (var 
        //  The editor is rejecting /\s/ for some reason.  the next line look like this:
        //      if ((elmArray[i].type == typ)&&(elmArray[i].value.match(/\s/) != null)){ 
        if ((elmArray[i].type == typ)&&(elmArray[i].value.match(/\s/) != null)){ 
        errMsg errStr;
        elmArray[i].value def;
        //Pass in the entire elements array, validate elements of type of file, if invalid, default to "input required".
        takeNoSpaces(document.upload.elements"file""input required");
        // The above method can validate any number of input fields in your form.
        //  Change "file" to "text" to validate text fields
        if (errMsg != '')alert(errMsg); 
        Last edited by VortexCortex; Oct 5, 2006, 09:34 PM. Reason: vBulletin garbled my regular expression


        • #5
          A working example.

          O I C. Just the last part of the file name must be devoid of spaces.

          My previous post keeps spaces out of any part of the file. Here's an example with an improved regular expression that lets the file path have spaces, just not the final file name...
          		<title>No Space File Validation</title>
          		<script type="text/javascript"><!--
          var errMsg = "";
          var errStr = "Image names can't contain spaces, line returns, tabs or null characters!";
          function takeNoSpaces(elmArray, typ, def){
          	for (var i=elmArray.length-1;i>-1;i--){
          		if (elmArray[i].type == typ) {
          			if (elmArray[i].value.match(/(^|[\\])[^\\]+\s[^\\]+$/) != null){ 
          				errMsg = errStr;
          				if (typ != "file") elmArray[i].value = def; //cant change file upload contents with JS.
          function validate(){
          	//Pass in the entire elements array, validate elements of type of file, if invalid, default to "input required".
          	takeNoSpaces(document.upload.elements, "file", "input required");
          	// The above method can validate any number of input fields in your form.
          	//  Change "file" to "text" to validate text fields
          	if (errMsg != ''){
          		errMsg = '';
          		return false;
          	return true;
          		<form name="upload" method="post" onsubmit="return validate()" enctype="multipart/form-data">
          			<input type="file" name="image1" size="30">
          			<input type="file" name="image2" size="30">
          			<input type="submit" value="upload">


          • #6
            This works great when I tested it as a standalone, but I cant get it to work as part of my external script. I have other validations that I am doing. I have 20 input fields, and I think it is validating those that are empty.